ota-meshi / eslint-plugin-yml

This ESLint plugin provides linting rules for YAML.
https://ota-meshi.github.io/eslint-plugin-yml/
MIT License
121 stars 13 forks source link

'no-trailing-spaces' sets ignoreComments: false in YAML files #277

Open martinkruusement opened 1 year ago

martinkruusement commented 1 year ago
overrides: [{
    extends: ['plugin:yml/standard'],
    files: ['*.yaml', '*.yml'],
    parser: 'yaml-eslint-parser',
    rules: {
      'no-trailing-spaces': ['error', { ignoreComments: true }]
    }
}]

Switching between error and warn always gives expected message types, but ignoreComments is always treated as false even if it's set to true in root rules or in the override.

This only happens in YAML files - ignoring trailing whitespace works in other files processed with other parsers.

Removing the rule from overrides still respects the root rule configuration, but will not ignore comments.

Full configuration below:

module.exports = {
  root: true,
  env: {
    es2022: true,
    node: true,
    browser: true,
    'vue/setup-compiler-macros': true
  },
  settings: {
    'import/resolver': {
      alias: {
        map: [['@', './src']],
        extensions: ['.cjs', '.js', '.ts', '.vue']
      }
    },
    'vue-i18n': {
      localeDir: './src/i18n/locales/*.yml',
      messageSyntaxVersion: '^9.0.0'
    }
  },
  extends: [
    'plugin:@intlify/vue-i18n/recommended',
    'plugin:vue/vue3-recommended',
    'eslint:recommended',
    '@vue/standard',
    'plugin:vue-scoped-css/all',
    'plugin:sonarjs/recommended'
  ],
  rules: {
    'sonarjs/cognitive-complexity': 'warn',
    'sonarjs/no-collapsible-if': 'off',
    'sonarjs/prefer-immediate-return': 'off',
    'sonarjs/prefer-single-boolean-return': 'off',
    'sonarjs/no-duplicate-string': ['error', { threshold: 5 }],
    'import/no-unresolved': 'error',
    'no-trailing-spaces': ['error', { ignoreComments: true }], // <-- This works, except for yaml files
    'vue/match-component-file-name': ['error', {
      extensions: ['vue', 'js'],
      shouldMatchCase: true
    }],
    'vue/multi-word-component-names': 'off',
    'vue/max-attributes-per-line': ['error', {
      singleline: { max: 7 },
      multiline: { max: 1 }
    }],
    'vue/html-self-closing': ['error', {
      html: {
        void: 'always',
        normal: 'any',
        component: 'always'
      },
      svg: 'always',
      math: 'always'
    }],
    'vue/require-default-prop': 'off',
    'vue/singleline-html-element-content-newline': 'off',
    'vue/max-len': ['warn', {
      code: 180,
      template: 180,
      ignoreComments: true
    }],
    '@intlify/vue-i18n/no-raw-text': ['error', {
      ignoreText: [':', '%', '(', ')', '%', '%)', '.', ',', ', ', '"', '+', ' | ', '© ', '×']
    }]
  },
  overrides: [{
    extends: ['plugin:yml/standard'],
    files: ['*.yaml', '*.yml'],
    parser: 'yaml-eslint-parser',
    rules: {
      'no-trailing-spaces': ['warn', { ignoreComments: true }] // <-- correctly sets type to Warning, but ignoreComments is always false
    }
  }, {
    extends: ['standard', 'plugin:jsonc/recommended-with-jsonc'],
    files: ['*.json', '*.json5', '*.jsonc'],
    parser: 'jsonc-eslint-parser'
  }, {
    files: ['*.stories.@(ts|tsx|js|jsx|mjs|cjs)'],
    extends: [
      'plugin:vue/vue3-recommended',
      'eslint:recommended',
      '@vue/standard',
      'plugin:storybook/recommended']
  },
  { files: ['*.js'], parserOptions: { sourceType: 'module' } },
  { files: ['*.mjs'], parserOptions: { sourceType: 'module' } },
  { files: ['*.cjs'], parserOptions: { sourceType: 'script' } }
  ]
}
ota-meshi commented 1 year ago

Thank you for posting this issue!

In order to make the YAML comment work as an ESLint comment directive, the parser builds the AST by treating the YAML comment as a block comment (/* ... */). Therefore, the option seems to have no effect because the rule interprets # foo as /* foo */.

To avoid this we need to add a new yml/no-trailing-spaces rule to this plugin.