Skip to content

Commit bc22bf0

Browse files
committed
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: nodejs#18043 Fixes: nodejs#11209 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Teddy Katz <[email protected]>
1 parent 0a4c79b commit bc22bf0

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

lib/.eslintrc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ rules:
66
buffer-constructor: error
77
no-let-in-for-declaration: error
88
lowercase-name-for-primitive: error
9+
non-ascii-character: error

lib/internal/process.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function setupConfig(_source) {
150150
};
151151
Object.defineProperty(Intl, 'v8BreakIterator', des);
152152
}
153-
// Dont let icu_data_dir leak through.
153+
// Don't let icu_data_dir leak through.
154154
delete process.icu_data_dir;
155155
}
156156

lib/repl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ function REPLServer(prompt,
324324
let previouslyInRawMode;
325325
if (self.breakEvalOnSigint) {
326326
// Start the SIGINT watchdog before entering raw mode so that a very
327-
// quick Ctrl+C doesnt lead to aborting the process completely.
327+
// quick Ctrl+C doesn't lead to aborting the process completely.
328328
utilBinding.startSigintWatchdog();
329329
previouslyInRawMode = self._setRawMode(false);
330330
}

lib/timers.js

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const TIMEOUT_MAX = 2147483647; // 2^31-1
4343
// TimerWrap C++ handle, which makes the call after the duration to process the
4444
// list it is attached to.
4545
//
46+
/* eslint-disable non-ascii-character */
4647
//
4748
// ╔════ > Object Map
4849
// ║
@@ -64,6 +65,7 @@ const TIMEOUT_MAX = 2147483647; // 2^31-1
6465
// ║
6566
// ╚════ > Linked List
6667
//
68+
/* eslint-enable non-ascii-character */
6769
//
6870
// With this, virtually constant-time insertion (append), removal, and timeout
6971
// is possible in the JavaScript layer. Any one list of timers is able to be
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @fileOverview Any non-ASCII characters in lib/ will increase the size
3+
* of the compiled node binary. This linter rule ensures that
4+
* any such character is reported.
5+
* @author Sarat Addepalli <[email protected]>
6+
*/
7+
8+
'use strict';
9+
10+
//------------------------------------------------------------------------------
11+
// Rule Definition
12+
//------------------------------------------------------------------------------
13+
14+
const nonAsciiRegexPattern = /[^\r\n\x20-\x7e]/;
15+
const suggestions = {
16+
'’': '\'',
17+
'‛': '\'',
18+
'‘': '\'',
19+
'“': '"',
20+
'‟': '"',
21+
'”': '"',
22+
'«': '"',
23+
'»': '"',
24+
'—': '-'
25+
};
26+
27+
module.exports = (context) => {
28+
29+
const reportIfError = (node, sourceCode) => {
30+
31+
const matches = sourceCode.text.match(nonAsciiRegexPattern);
32+
33+
if (!matches) return;
34+
35+
const offendingCharacter = matches[0];
36+
const offendingCharacterPosition = matches.index;
37+
const suggestion = suggestions[offendingCharacter];
38+
39+
let message = `Non-ASCII character '${offendingCharacter}' detected.`;
40+
41+
message = suggestion ?
42+
`${message} Consider replacing with: ${suggestion}` :
43+
message;
44+
45+
context.report({
46+
node,
47+
message,
48+
loc: sourceCode.getLocFromIndex(offendingCharacterPosition),
49+
fix: (fixer) => {
50+
return fixer.replaceText(
51+
node,
52+
suggestion ? `${suggestion}` : ''
53+
);
54+
}
55+
});
56+
};
57+
58+
return {
59+
Program: (node) => reportIfError(node, context.getSourceCode())
60+
};
61+
};

0 commit comments

Comments
 (0)