How can I write a ESLint rule for “linebreak-style”, changing depending on Windows or Unix?

拟墨画扇 提交于 2019-12-02 15:49:40
vitorbal

The eslint configuration file can be a regular .js file (ie, not JSON, but full JS with logic) that exports the configuration object.

That means you could change the configuration of the linebreak-style rule depending on your current environment (or any other JS logic you can think of).

For example, to use a different linebreak-style configuration when your node environment is 'prod':

module.exports = {
    "root": true,
    "parserOptions": {
        "sourceType": "module",
        "ecmaVersion": 6
    },
    "rules": {
        // windows linebreaks when not in production environment
        "linebreak-style": ["error", process.env.NODE_ENV === 'prod' ? "unix" : "windows"]
    }
};

Example usage:

$ NODE_ENV=prod node_modules/.bin/eslint src/test.js

src/test.js
  1:25  error  Expected linebreaks to be 'CRLF' but found 'LF'  linebreak-style
  2:30  error  Expected linebreaks to be 'CRLF' but found 'LF'  linebreak-style
  3:36  error  Expected linebreaks to be 'CRLF' but found 'LF'  linebreak-style
  4:26  error  Expected linebreaks to be 'CRLF' but found 'LF'  linebreak-style
  5:17  error  Expected linebreaks to be 'CRLF' but found 'LF'  linebreak-style
  6:50  error  Expected linebreaks to be 'CRLF' but found 'LF'  linebreak-style
  7:62  error  Expected linebreaks to be 'CRLF' but found 'LF'  linebreak-style
  8:21  error  Expected linebreaks to be 'CRLF' but found 'LF'  linebreak-style

✖ 8 problems (8 errors, 0 warnings)

$ NODE_ENV=dev node_modules/.bin/eslint src/test.js
$ # no errors
Stu

I spent time trying to find how to shut off the linkbreak-style and lost it due to reverting some of my code I thought others my like to have this as well.

In the .eslintrc file you can also set linebreak-style to 0 which shuts off the linebreak feature:

module.exports = {
  extends: 'google',
  quotes: [2, 'single'],
  globals: {
    SwaggerEditor: false
  },
  env: {
    browser: true
  },
  rules:{
    "linebreak-style": 0   // <----------
  }
};

.eslintc for Windows visualstudio code

{
  "env": {
    "node": true
  },
  "rules":{
    "linebreak-style": 0
  }
}

If you're using Vs Code on Windows, go to your ".eslintrc.json" file (or '.js' depending on which option you chose when setting up your ESLint); this file will usually be found in the root folder of your project; and under rules add the linebreak option to use Windows CRLF as follows:

"rules": {
    "linebreak-style": ["error", "windows"]
}

Save the file and when you go back to your JavaScript file, all those pesky red lines will disappear.

The location of the config file required to alter ESLint rules for linebreak-style may change depending on whether you want to alter local, project or global settings, it searches for local first which overrides those further up the tree, so alter at the top of the tree to propagate down for global

I used airbnb style and my global settings were located here: node_modules/eslint-config-airbnb-base/rules/style.js:

If you are unsure on the location of the file you can always search for a list of files that contain text relating to the settings, on Linux to find all files with linebreak settings navigate to the folder where ESLint was installed and use:

grep -r linebreak

In your .eslintrc.js:

"rules": {
  "linebreak-style": ["error", (process.platform === "win32" ? "windows" : "unix")], // https://stackoverflow.com/q/39114446/2771889
}

See also: How do I determine the current operating system with Node.js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!