javascript - How can I write a ESLint rule for "linebreak-style", changing depending on Windows or Unix? -
as know, linebreaks (new line) used in windows carriage returns (cr) followed line feed (lf) i.e. (crlf) whereas, linux , unix use simple line feed (lf)
now, in case, build server uses supports linux , unix format so, below rule working on build server:
linebreak-style: ["error", "unix"]
but doing development on windows , need update rule on each git pull/git push below,
linebreak-style: ["error", "windows"]
so, there way write generic linebreak-style rule support both environments, linux/unix , windows?
note: using ecmascript6[js], webstorm[ide] development
any solutions/suggestions highly appreciated. thanks!
the eslint configuration file can a regular .js
file (ie, not json, full js logic) exports configuration object.
that means change configuration of linebreak-style
rule depending on current environment (or other js logic can think of).
for example, use different linebreak-style
configuration when node environment '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 'crlf' found 'lf' linebreak-style 2:30 error expected linebreaks 'crlf' found 'lf' linebreak-style 3:36 error expected linebreaks 'crlf' found 'lf' linebreak-style 4:26 error expected linebreaks 'crlf' found 'lf' linebreak-style 5:17 error expected linebreaks 'crlf' found 'lf' linebreak-style 6:50 error expected linebreaks 'crlf' found 'lf' linebreak-style 7:62 error expected linebreaks 'crlf' found 'lf' linebreak-style 8:21 error expected linebreaks 'crlf' found 'lf' linebreak-style ✖ 8 problems (8 errors, 0 warnings) $ node_env=dev node_modules/.bin/eslint src/test.js $ # no errors
Comments
Post a Comment