0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 07:27:22 +01:00
nodejs/tools/eslint-rules/non-ascii-character.js
Sarat Addepalli c45afe8198
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>
2018-02-04 16:55:13 +01:00

62 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @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())
};
};