0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 12:10:08 +01:00
nodejs/tools/eslint-rules/non-ascii-character.js
cjihrig ee0b44fd93
tools: add meta.fixable to fixable lint rules
This commit adds the meta.fixable property to all fixable ESLint
rules. This is required as of ESLint 7.6.0.

PR-URL: https://github.com/nodejs/node/pull/34589
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2020-08-03 08:48:32 -04:00

66 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())
};
};
module.exports.meta = {
fixable: 'code'
};