mirror of
https://github.com/nodejs/node.git
synced 2024-11-24 03:07:54 +01:00
3c1069bb06
Refs: https://eslint.org/docs/latest/use/migrate-to-9.0.0 PR-URL: https://github.com/nodejs/node/pull/52889 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
58 lines
1.4 KiB
JavaScript
58 lines
1.4 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 = {
|
||
create(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),
|
||
});
|
||
};
|
||
|
||
return {
|
||
Program: (node) => reportIfError(node, context.sourceCode),
|
||
};
|
||
},
|
||
};
|