2018-01-08 17:46:27 +00:00
|
|
|
|
/**
|
|
|
|
|
* @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 = {
|
|
|
|
|
'’': '\'',
|
|
|
|
|
'‛': '\'',
|
|
|
|
|
'‘': '\'',
|
|
|
|
|
'“': '"',
|
|
|
|
|
'‟': '"',
|
|
|
|
|
'”': '"',
|
|
|
|
|
'«': '"',
|
|
|
|
|
'»': '"',
|
2022-12-18 16:39:39 +00:00
|
|
|
|
'—': '-',
|
2018-01-08 17:46:27 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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 {
|
2022-12-18 16:39:39 +00:00
|
|
|
|
Program: (node) => reportIfError(node, context.getSourceCode()),
|
2018-01-08 17:46:27 +00:00
|
|
|
|
};
|
|
|
|
|
};
|