Skip to content

Commit

Permalink
tools: non-Ascii linter for /lib only
Browse files Browse the repository at this point in the history
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
SirR4T committed Mar 29, 2018
1 parent 0a4c79b commit bc22bf0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ rules:
buffer-constructor: error
no-let-in-for-declaration: error
lowercase-name-for-primitive: error
non-ascii-character: error
2 changes: 1 addition & 1 deletion lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function setupConfig(_source) {
};
Object.defineProperty(Intl, 'v8BreakIterator', des);
}
// Don’t let icu_data_dir leak through.
// Don't let icu_data_dir leak through.
delete process.icu_data_dir;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ function REPLServer(prompt,
let previouslyInRawMode;
if (self.breakEvalOnSigint) {
// Start the SIGINT watchdog before entering raw mode so that a very
// quick Ctrl+C doesn’t lead to aborting the process completely.
// quick Ctrl+C doesn't lead to aborting the process completely.
utilBinding.startSigintWatchdog();
previouslyInRawMode = self._setRawMode(false);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const TIMEOUT_MAX = 2147483647; // 2^31-1
// TimerWrap C++ handle, which makes the call after the duration to process the
// list it is attached to.
//
/* eslint-disable non-ascii-character */
//
// ╔════ > Object Map
// β•‘
Expand All @@ -64,6 +65,7 @@ const TIMEOUT_MAX = 2147483647; // 2^31-1
// β•‘
// β•šβ•β•β•β• > Linked List
//
/* eslint-enable non-ascii-character */
//
// With this, virtually constant-time insertion (append), removal, and timeout
// is possible in the JavaScript layer. Any one list of timers is able to be
Expand Down
61 changes: 61 additions & 0 deletions tools/eslint-rules/non-ascii-character.js
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())
};
};

0 comments on commit bc22bf0

Please sign in to comment.