diff options
author | Sarat Addepalli <sarat.addepalli@paytm.com> | 2018-01-08 23:16:27 +0530 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-02-04 16:55:13 +0100 |
commit | c45afe819840d2915823df55f24574f0da139dcc (patch) | |
tree | 343645a38221355d22c10bbffe6f5472d56963f6 /tools | |
parent | 591a8adeae9a4a366db87320d5ef373ceaa5c966 (diff) | |
download | android-node-v8-c45afe819840d2915823df55f24574f0da139dcc.tar.gz android-node-v8-c45afe819840d2915823df55f24574f0da139dcc.tar.bz2 android-node-v8-c45afe819840d2915823df55f24574f0da139dcc.zip |
tools: non-Ascii linter for /lib only
Non-ASCII characters in /lib get compiled into the node binary,
and may bloat the binary size unnecessarily. A linter rule may
help prevent this.
PR-URL: https://github.com/nodejs/node/pull/18043
Fixes: https://github.com/nodejs/node/issues/11209
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/eslint-rules/non-ascii-character.js | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/tools/eslint-rules/non-ascii-character.js b/tools/eslint-rules/non-ascii-character.js new file mode 100644 index 0000000000..e67aac7cd9 --- /dev/null +++ b/tools/eslint-rules/non-ascii-character.js @@ -0,0 +1,61 @@ +/** + * @fileOverview Any non-ASCII characters in lib/ will increase the size + * of the compiled node binary. This linter rule ensures that + * any such character is reported. + * @author Sarat Addepalli <sarat.addepalli@gmail.com> + */ + +'use strict'; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +const nonAsciiRegexPattern = /[^\r\n\x20-\x7e]/; +const suggestions = { + '’': '\'', + '‛': '\'', + '‘': '\'', + '“': '"', + '‟': '"', + '”': '"', + '«': '"', + '»': '"', + '—': '-' +}; + +module.exports = (context) => { + + const reportIfError = (node, sourceCode) => { + + const matches = sourceCode.text.match(nonAsciiRegexPattern); + + if (!matches) return; + + const offendingCharacter = matches[0]; + const offendingCharacterPosition = matches.index; + const suggestion = suggestions[offendingCharacter]; + + let message = `Non-ASCII character '${offendingCharacter}' detected.`; + + message = suggestion ? + `${message} Consider replacing with: ${suggestion}` : + message; + + context.report({ + node, + message, + loc: sourceCode.getLocFromIndex(offendingCharacterPosition), + fix: (fixer) => { + return fixer.replaceText( + node, + suggestion ? `${suggestion}` : '' + ); + } + }); + }; + + return { + Program: (node) => reportIfError(node, context.getSourceCode()) + }; +}; |