Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
martinkruusement opened this issue Oct 24, 2023 · 1 comment
Open
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@martinkruusement
Copy link

martinkruusement commented Oct 24, 2023

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
Copy link
Owner

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.

@ota-meshi ota-meshi added enhancement New feature or request good first issue Good for newcomers labels Oct 25, 2023
@ota-meshi ota-meshi added the help wanted Extra attention is needed label Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants