0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-28 06:28:40 +01:00
nodejs/tools/eslint-rules/non-ascii-character.js
Rich Trott 896e5af1fd tools: remove fixer for non-ascii-character ESLint custom rule
The fixer for non-ascii-character does not typidally do the right thing.
It removes the entire node, not the offending character. I discovered
this when it removed the entire contents of a file and I wasn't sure
which auto-fix rule was doing it.

This commit adds a minimal test for the rule. The tests require that
auto-fix results be supplied, so if someone wants to re-add auto-fixing
to the rule, we'll have tests that it does the right thing.

PR-URL: https://github.com/nodejs/node/pull/38413
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2021-04-28 10:35:32 -07:00

56 lines
1.4 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),
});
};
return {
Program: (node) => reportIfError(node, context.getSourceCode())
};
};