1.13.2 • Published 8 days ago

eslint-plugin-yml v1.13.2

Weekly downloads
2,564
License
MIT
Repository
github
Last release
8 days ago

Introduction

eslint-plugin-yml is ESLint plugin provides linting rules for YAML.

NPM license NPM version NPM downloads NPM downloads NPM downloads NPM downloads NPM downloads Build Status Coverage Status

:name_badge: Features

This ESLint plugin provides linting rules for YAML.

  • You can use ESLint to lint YAML.
  • You can find out the problem with your YAML files.
  • You can apply consistent code styles to your YAML files.
  • Supports Vue SFC custom blocks such as <i18n lang="yaml">.
    Requirements vue-eslint-parser v7.3.0 and above.
  • Supports ESLint directives. e.g. # eslint-disable-next-line
  • You can check your code in real-time using the ESLint editor integrations.

You can check on the Online DEMO.

:question: How is it different from other YAML plugins?

Plugins that do not use AST

e.g. eslint-plugin-yaml

These plugins use the processor to parse and return the results independently, without providing the ESLint engine with AST and source code text.

Plugins don't provide AST, so you can't use directive comments (e.g. # eslint-disable).
Plugins don't provide source code text, so you can't use it with plugins and rules that use text (e.g. eslint-plugin-prettier, eol-last).

eslint-plugin-yml works by providing AST and source code text to ESLint.

:book: Documentation

See documents.

:cd: Installation

npm install --save-dev eslint eslint-plugin-yml

Requirements

  • ESLint v6.0.0 and above
  • Node.js v14.17.x, v16.x and above

:book: Usage

Configuration

New (ESLint>=v9) Config (Flat Config)

Use eslint.config.js file to configure rules. See also: https://eslint.org/docs/latest/use/configure/configuration-files-new.

Example eslint.config.js:

import eslintPluginYml from 'eslint-plugin-yml';
export default [
  // add more generic rule sets here, such as:
  // js.configs.recommended,
  ...eslintPluginYml.configs['flat/recommended'],
  {
    rules: {
      // override/add rules settings here, such as:
    // 'yml/rule-name': 'error'
    }
  }
];

This plugin provides configs:

  • *.configs['flat/base'] ... Configuration to enable correct YAML parsing.
  • *.configs['flat/recommended'] ... Above, plus rules to prevent errors or unintended behavior.
  • *.configs['flat/standard'] ... Above, plus rules to enforce the common stylistic conventions.
  • *.configs['flat/prettier'] ... Turn off rules that may conflict with Prettier.

See the rule list to get the rules that this plugin provides.

Legacy Config (ESLint<v9)

Use .eslintrc.* file to configure rules. See also: https://eslint.org/docs/latest/use/configure/.

Example .eslintrc.js:

module.exports = {
  extends: [
    // add more generic rulesets here, such as:
    // 'eslint:recommended',
    "plugin:yml/standard",
  ],
  rules: {
    // override/add rules settings here, such as:
    // 'yml/rule-name': 'error'
  },
};

This plugin provides configs:

  • plugin:yml/base ... Configuration to enable correct YAML parsing.
  • plugin:yml/recommended ... Above, plus rules to prevent errors or unintended behavior.
  • plugin:yml/standard ... Above, plus rules to enforce the common stylistic conventions.
  • plugin:yml/prettier ... Turn off rules that may conflict with Prettier.

See the rule list to get the rules that this plugin provides.

Parser Configuration

If you have specified a parser, you need to configure a parser for .yaml.

For example, if you are using the "@babel/eslint-parser", configure it as follows:

module.exports = {
  // ...
  extends: ["plugin:yml/standard"],
  // ...
  parser: "@babel/eslint-parser",
  // Add an `overrides` section to add a parser configuration for YAML.
  overrides: [
    {
      files: ["*.yaml", "*.yml"],
      parser: "yaml-eslint-parser",
    },
  ],
  // ...
};

Parser Options

The following parser options for yaml-eslint-parser are available by specifying them in parserOptions in the ESLint configuration file.

module.exports = {
  // ...
  overrides: [
    {
      files: ["*.yaml", "*.yml"],
      parser: "yaml-eslint-parser",
      // Options used with yaml-eslint-parser.
      parserOptions: {
        defaultYAMLVersion: "1.2",
      },
    },
  ],
  // ...
};

See also https://github.com/ota-meshi/yaml-eslint-parser#readme.

Running ESLint from the command line

If you want to run eslint from the command line, make sure you include the .yaml extension using the --ext option or a glob pattern, because ESLint targets only .js files by default.

Examples:

eslint --ext .js,.yaml,.yml src
eslint "src/**/*.{js,yaml,yml}"

:computer: Editor Integrations

Visual Studio Code

Use the dbaeumer.vscode-eslint extension that Microsoft provides officially.

You have to configure the eslint.validate option of the extension to check .yaml files, because the extension targets only *.js or *.jsx files by default.

Example .vscode/settings.json:

{
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "yaml",
    "github-actions-workflow" // for GitHub Actions workflow files
  ]
}

JetBrains WebStorm IDEs

In any of the JetBrains IDEs you can configure the linting scope. Following the steps in their help document, you can add YAML files to the scope like so:

  1. Open the Settings/Preferences dialog, go to Languages and Frameworks | JavaScript | Code Quality Tools | ESLint, and select Automatic ESLint configuration or Manual ESLint configuration.
  2. In the Run for files field, update the pattern that defines the set of files to be linted to include YAML files as well:
{**/*,*}.{js,ts,jsx,tsx,html,vue,yaml,yml}
                                 ^^^^ ^^^

:white_check_mark: Rules

The --fix option on the command line automatically fixes problems reported by rules which have a wrench :wrench: below.
The rules with the following star :star: are included in the config.

YAML Rules

Rule IDDescriptionFixableRECOMMENDEDSTANDARD
yml/block-mapping-colon-indicator-newlineenforce consistent line breaks after : indicator:wrench:
yml/block-mapping-question-indicator-newlineenforce consistent line breaks after ? indicator:wrench::star:
yml/block-mappingrequire or disallow block style mappings.:wrench::star:
yml/block-sequence-hyphen-indicator-newlineenforce consistent line breaks after - indicator:wrench::star:
yml/block-sequencerequire or disallow block style sequences.:wrench::star:
yml/file-extensionenforce YAML file extension
yml/indentenforce consistent indentation:wrench::star:
yml/key-name-casingenforce naming convention to key names
yml/no-empty-documentdisallow empty document:star::star:
yml/no-empty-keydisallow empty mapping keys:star::star:
yml/no-empty-mapping-valuedisallow empty mapping values:star::star:
yml/no-empty-sequence-entrydisallow empty sequence entries:star::star:
yml/no-tab-indentdisallow tabs for indentation.:star::star:
yml/no-trailing-zerosdisallow trailing zeros for floats:wrench:
yml/plain-scalarrequire or disallow plain style scalar.:wrench::star:
yml/quotesenforce the consistent use of either double, or single quotes:wrench::star:
yml/require-string-keydisallow mapping keys other than strings
yml/sort-keysrequire mapping keys to be sorted:wrench:
yml/sort-sequence-valuesrequire sequence values to be sorted:wrench:
yml/vue-custom-block/no-parsing-errordisallow parsing errors in Vue custom blocks:star::star:

Extension Rules

Rule IDDescriptionFixableRECOMMENDEDSTANDARD
yml/flow-mapping-curly-newlineenforce consistent line breaks inside braces:wrench::star:
yml/flow-mapping-curly-spacingenforce consistent spacing inside braces:wrench::star:
yml/flow-sequence-bracket-newlineenforce linebreaks after opening and before closing flow sequence brackets:wrench::star:
yml/flow-sequence-bracket-spacingenforce consistent spacing inside flow sequence brackets:wrench::star:
yml/key-spacingenforce consistent spacing between keys and values in mapping pairs:wrench::star:
yml/no-irregular-whitespacedisallow irregular whitespace:star::star:
yml/no-multiple-empty-linesdisallow multiple empty lines:wrench:
yml/spaced-commentenforce consistent spacing after the # in a comment:wrench::star:

:rocket: To Do More Verification

Verify using JSON Schema

You can verify using JSON Schema by checking and installing eslint-plugin-json-schema-validator.

Verify the Vue I18n message resource files

You can verify the message files by checking and installing @intlify/eslint-plugin-vue-i18n.

:traffic_light: Semantic Versioning Policy

eslint-plugin-yml follows Semantic Versioning and ESLint's Semantic Versioning Policy.

:beers: Contributing

Welcome contributing!

Please use GitHub's Issues/PRs.

Development Tools

  • npm test runs tests and measures coverage.
  • npm run update runs in order to update readme and recommended configuration.

Working With Rules

This plugin uses yaml-eslint-parser for the parser. Check here to find out about AST.

:couple: Related Packages

:lock: License

See the LICENSE file for license rights and limitations (MIT).

@yysmx/eslint-config@yysmx/eslint-config-basic@lvyunlong/eslint-config@lvyunlong/eslint-config-basic@jcamp/eslint-config-basic@jiqi/eslint-config@jiqi/eslint-config-basic@jiqi/eslint-config-prettier@kaivanwong/eslint-config-base@rjqh/eslint-config-basic@rain-star/eslint-config-basic@cyenoch/eslint-config@asasinmode/eslint-config@obvcloud/eslint-config-preset-basic@denverzh/eslint-config@denverzh/eslint-config-base@nexhome/restrictions@praburangki/eslint-config-basic@zguolee/eslint-config-basic@liuxy0551/eslint-config-dtstack-basic@nplus-cumt/eslint-config-basic@ventjs/eslint-config-basic@ixfe/eslint-config-prettier@ixfe/eslint-config-basic@ventjs/eslint-config-prettier@silence94/eslint-config-basic@jady/eslint-config@caryhu/eslint-config-basic@fellwork/eslint-config-core@fellwork/eslint-config-foreman@easy-going/eslint-config-base@renya/eslint-config-core@progressivestudio/eslint-config-base@chris-zhu/eslint-config-basic@sasaiweb/eslint-config-basic@sasaiweb/eslint-config-prettier@infinitebrahmanuniverse/nolb-eslint-plugin-y@kriszu/eslint-config-basic@soybeanjs/eslint-config-basic@trpl/eslint-config-common@asasinmode/eslint-config-basic@tgarif/eslint-config-basiceslint-config-soybeanjs-baseeslint-config-myna@okno/eslint-config@everything-registry/sub-chunk-1615@sunwise/eslint-config-basic@sunwise/eslint-config-prettier@lvjiaxuan/eslint-config-js@ifmini/eslint-config@ifshizuku/eslint-config-basic@ventsislavnikolov/eslint-config@daopk/eslint-config-yamllionconfighorae-config@thenic/eslint-config@thenic/eslint-config-basic@valinaa/eslint-config@totominc/eslint-config-vue@totominc/eslint-config-react@toryz/eslint-config-basic@vancats/eslint-config@vancats/eslint-config-base@unoco/eslint-config@unoco/eslint-config-basic@unonu/eslint-config@unonu/eslint-config-basic@vbs/eslint-plugin@vecmat/eslint-config-basic@snickbit/eslint-config@snowball-tech/eslint-config@songwuk/eslint-config@songwuk/eslint-config-basic@spear-ai/eslint-config@relaxed/eslint-config-basic@relaxed/eslint-config-prettier@realive/eslint-config-base@ubermanu/eslint-config@ubiquitous8/eslint-config@ubiquitous8/eslint-config-basic@theguild/eslint-config@thenbe/eslint-config@timotteaa/eslint-config@timotteaa/eslint-config-basic@timotteaa/eslint-config-entry@unyu/eslint-config@unyu/eslint-config-basic@tioks/eslint-config@tioks/eslint-config-basic@useskit/eslint-config@useskit/eslint-config-basic@toolboxes/eslint-config-basic@uselessjs/eslint-config-base@tm2js/eslint-config@tresjs/eslint-config-base@vexip-ui/eslint-config@tripteki/eslint-config@vituity/eslint-config@vituity/eslint-config-basic@tsofist/web-buddy
1.13.2

8 days ago

1.13.1

10 days ago

1.13.0

11 days ago

1.12.2

2 months ago

1.12.1

2 months ago

1.12.0

2 months ago

1.11.0

4 months ago

1.9.0

7 months ago

1.10.0

6 months ago

1.8.0

10 months ago

1.7.0

11 months ago

1.6.0

11 months ago

1.5.0

1 year ago

1.4.0

1 year ago

1.3.0

1 year ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

0.15.0

2 years ago

0.13.0

2 years ago

0.14.0

2 years ago

0.11.0

2 years ago

0.12.0

2 years ago

0.10.1

3 years ago

0.10.0

3 years ago

0.9.0

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.2

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago

0.0.2

4 years ago

0.0.1

4 years ago

0.0.0

4 years ago