diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index d8e34f85b5759d..b9fc11338e6dca 100644 --- a/lib/.eslintrc.yaml +++ b/lib/.eslintrc.yaml @@ -3,3 +3,4 @@ rules: require-buffer: 2 buffer-constructor: 2 no-let-in-for-declaration: 2 + only-ascii-characters: 2 diff --git a/lib/console.js b/lib/console.js index 7ec9c846329cce..328b0e5ec73070 100644 --- a/lib/console.js +++ b/lib/console.js @@ -72,7 +72,7 @@ function createWriteErrorHandler(stream) { // If there was an error, it will be emitted on `stream` as // an `error` event. Adding a `once` listener will keep that error // from becoming an uncaught exception, but since the handler is - // removed after the event, non-console.* writes won’t be affected. + // removed after the event, non-console.* writes won't be affected. stream.once('error', noop); } }; @@ -90,7 +90,7 @@ function write(ignoreErrors, stream, string, errorhandler) { stream.write(string, errorhandler); } catch (e) { - // Sorry, there’s no proper way to pass along the error here. + // Sorry, there's no proper way to pass along the error here. } finally { stream.removeListener('error', noop); } diff --git a/lib/timers.js b/lib/timers.js index 115c3c82963530..3cc15fdc6042b9 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -32,6 +32,8 @@ const kOnTimeout = TimerWrap.kOnTimeout | 0; const TIMEOUT_MAX = 2147483647; // 2^31-1 +/* eslint-disable only-ascii-characters */ + // HOW and WHY the timers implementation works the way it does. // // Timers are crucial to Node.js. Internally, any TCP I/O connection creates a @@ -105,6 +107,8 @@ const TIMEOUT_MAX = 2147483647; // 2^31-1 // However, these operations combined have shown to be trivial in comparison to // other alternative timers architectures. +/* eslint-enable only-ascii-characters */ + // Object maps containing linked lists of timers, keyed and sorted by their // duration in milliseconds. diff --git a/tools/eslint-rules/only-ascii-characters.js b/tools/eslint-rules/only-ascii-characters.js new file mode 100644 index 00000000000000..c5f53f27a60122 --- /dev/null +++ b/tools/eslint-rules/only-ascii-characters.js @@ -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); + }); + } + }; + } +};