mirror of
https://github.com/nodejs/node.git
synced 2024-11-30 07:27:22 +01:00
c45afe8198
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>
62 lines
1.5 KiB
JavaScript
62 lines
1.5 KiB
JavaScript
/**
|
||
* @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())
|
||
};
|
||
};
|