Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tools: fix non-ascii linter detection
the linter should detect not just literals, but also source code
and all comments too.
  • Loading branch information
Sarat Addepalli
Sarat Addepalli committed Jan 19, 2018
commit 1399bf5f2befe6d78f674fd49ffa20f844c9530d
22 changes: 16 additions & 6 deletions tools/eslint-rules/non-ascii-character.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
// Rule Definition
//------------------------------------------------------------------------------

const nonAsciiSelector = 'Literal[value=/[^\n\x20-\x7e]/]';
const nonAsciiRegexPattern = new RegExp(/[^\n\x20-\x7e]/);
const nonAsciiRegexPattern = new RegExp(/[^\r\n\x20-\x7e]/);
const suggestions = {
'’': '\'',
'‛': '\'',
Expand All @@ -27,10 +26,14 @@ const suggestions = {

module.exports = (context) => {

const reportError = (node) => {
const reportIfError = (node, token) => {

const nodeValue = node.value;
const offendingCharacter = nodeValue.match(nonAsciiRegexPattern)[0];
const { value } = token;
const matches = value.match(nonAsciiRegexPattern);

if (!matches) return;

const offendingCharacter = matches[0];
const suggestion = suggestions[offendingCharacter];

let message = `Non-ASCII character '${offendingCharacter}' detected.`;
Expand All @@ -52,6 +55,13 @@ module.exports = (context) => {
};

return {
[nonAsciiSelector]: (node) => reportError(node, context)
Program: (node) => {
const source = context.getSourceCode();
const sourceTokens = source.getTokens(node);
const commentTokens = source.getAllComments();
const tokens = sourceTokens.concat(commentTokens);

tokens.forEach((token) => reportIfError(node, token));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to match on source.text rather than each individual token, to ensure that non-ascii whitespace is detected (which would not be part of any token or comment).

}
};
};