-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools: add ASCII only lint rule for lib/
Detects if files in lib/ contain non-ASCII characters and raises a linting error. Also removes non-ASCII characters from lib/console.js comments Fixes: #11209
- Loading branch information
Showing
4 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ rules: | |
require-buffer: 2 | ||
buffer-constructor: 2 | ||
no-let-in-for-declaration: 2 | ||
only-ascii-characters: 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* @fileoverview Prohibit the use of non-ascii characters | ||
* @author Kalon Hinds | ||
*/ | ||
|
||
/* eslint no-control-regex:0 */ | ||
|
||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
const nonAsciiPattern = new RegExp('([^\x00-\x7F])', 'g'); | ||
const suggestions = { | ||
'’': '\'', | ||
'—': '-' | ||
}; | ||
const reportError = ({line, column, character}, node, context) => { | ||
const suggestion = suggestions[character]; | ||
|
||
let message = `Non-ASCII character ${character} detected.`; | ||
|
||
message = suggestion ? | ||
`${message} Consider replacing with: ${suggestion}` : message; | ||
|
||
context.report({ | ||
node, | ||
message, | ||
loc: { | ||
line, | ||
column | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
create: (context) => { | ||
return { | ||
Program: (node) => { | ||
const source = context.getSourceCode(); | ||
const sourceTokens = source.getTokens(node); | ||
const commentTokens = source.getAllComments(); | ||
const tokens = sourceTokens.concat(commentTokens); | ||
|
||
tokens.forEach((token) => { | ||
const { value } = token; | ||
const matches = value.match(nonAsciiPattern); | ||
|
||
if (!matches) return; | ||
|
||
const { loc } = token; | ||
const character = matches[0]; | ||
const column = loc.start.column + value.indexOf(character); | ||
|
||
reportError({ | ||
line: loc.start.line, | ||
column, | ||
character | ||
}, node, context); | ||
}); | ||
} | ||
}; | ||
} | ||
}; |