-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools: non-Ascii linter for /lib only
Non-ASCII characters in /lib get compiled into the node binary, and may bloat the binary size unnecessarily. A linter rule may help prevent this. PR-URL: #18043 Fixes: #11209 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
- Loading branch information
Showing
8 changed files
with
71 additions
and
5 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
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
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
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,61 @@ | ||
/** | ||
* @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()) | ||
}; | ||
}; |