Skip to content

Commit e00bb16

Browse files
SirR4TMylesBorins
authored andcommitted
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 <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Teddy Katz <[email protected]>
1 parent be5c293 commit e00bb16

File tree

8 files changed

+71
-5
lines changed

8 files changed

+71
-5
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/console.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function createWriteErrorHandler(stream) {
8484
// If there was an error, it will be emitted on `stream` as
8585
// an `error` event. Adding a `once` listener will keep that error
8686
// from becoming an uncaught exception, but since the handler is
87-
// removed after the event, non-console.* writes wont be affected.
87+
// removed after the event, non-console.* writes won't be affected.
8888
// we are only adding noop if there is no one else listening for 'error'
8989
if (stream.listenerCount('error') === 0) {
9090
stream.on('error', noop);
@@ -125,7 +125,7 @@ function write(ignoreErrors, stream, string, errorhandler, groupIndent) {
125125
// even in edge cases such as low stack space.
126126
if (e.message === MAX_STACK_MESSAGE && e.name === 'RangeError')
127127
throw e;
128-
// Sorry, theres no proper way to pass along the error here.
128+
// Sorry, there's no proper way to pass along the error here.
129129
} finally {
130130
stream.removeListener('error', noop);
131131
}

lib/internal/http2/core.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1876,7 +1876,7 @@ function processRespondWithFD(self, fd, headers, offset = 0, length = -1,
18761876
return;
18771877
}
18781878
// exact length of the file doesn't matter here, since the
1879-
// stream is closing anyway just use 1 to signify that
1879+
// stream is closing anyway - just use 1 to signify that
18801880
// a write does exist
18811881
trackWriteState(self, 1);
18821882
}

lib/internal/test/unicode.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
// This module exists entirely for regression testing purposes.
44
// See `test/parallel/test-internal-unicode.js`.
55

6+
/* eslint-disable non-ascii-character */
67
module.exports = '✓';
8+
/* eslint-enable non-ascii-character */

lib/stream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ try {
4545
try {
4646
Stream._isUint8Array = process.binding('util').isUint8Array;
4747
} catch (e) {
48-
// This throws for Node < 4.2.0 because theres no util binding and
48+
// This throws for Node < 4.2.0 because there's no util binding and
4949
// returns undefined for Node < 7.4.0.
5050
}
5151
}

lib/timers.js

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ const TIMEOUT_MAX = 2 ** 31 - 1;
9090
// TimerWrap C++ handle, which makes the call after the duration to process the
9191
// list it is attached to.
9292
//
93+
/* eslint-disable non-ascii-character */
9394
//
9495
// ╔════ > Object Map
9596
// ║
@@ -111,6 +112,7 @@ const TIMEOUT_MAX = 2 ** 31 - 1;
111112
// ║
112113
// ╚════ > Linked List
113114
//
115+
/* eslint-enable non-ascii-character */
114116
//
115117
// With this, virtually constant-time insertion (append), removal, and timeout
116118
// is possible in the JavaScript layer. Any one list of timers is able to be

lib/zlib.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ Zlib.prototype.flush = function flush(kind, callback) {
374374
this._scheduledFlushFlag = maxFlush(kind, this._scheduledFlushFlag);
375375

376376
// If a callback was passed, always register a new `drain` + flush handler,
377-
// mostly because thats simpler and flush callbacks piling up is a rare
377+
// mostly because that's simpler and flush callbacks piling up is a rare
378378
// thing anyway.
379379
if (!alreadyHadFlushScheduled || callback) {
380380
const drainHandler = () => this.flush(this._scheduledFlushFlag, callback);
+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)