Skip to content

Commit

Permalink
tools: add ASCII only lint rule for lib/
Browse files Browse the repository at this point in the history
Detects if files in lib/ contain non-ASCII characters and
raises a linting error. Also removes non-ASCII characters
from lib/console.js comments

Fixes: #11209
  • Loading branch information
hkal committed Mar 24, 2017
1 parent 348cc80 commit e7b2a19
Show file tree
Hide file tree
Showing 4 changed files with 72 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 @@ -3,3 +3,4 @@ rules:
require-buffer: 2
buffer-constructor: 2
no-let-in-for-declaration: 2
only-ascii-characters: 2
4 changes: 2 additions & 2 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 wont be affected.
// removed after the event, non-console.* writes won't be affected.
stream.once('error', noop);
}
};
Expand All @@ -90,7 +90,7 @@ function write(ignoreErrors, stream, string, errorhandler) {

stream.write(string, errorhandler);
} catch (e) {
// Sorry, theres 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);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
65 changes: 65 additions & 0 deletions tools/eslint-rules/only-ascii-characters.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
};
}
};

0 comments on commit e7b2a19

Please sign in to comment.