Skip to content

Commit f58b798

Browse files
committed
Forbid non-ASCII characters in JS files
1 parent 676c775 commit f58b798

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

.eslintrc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ rules:
2020
# See './resources/eslint-internal-rules/README.md'
2121
##############################################################################
2222

23+
internal-rules/only-ascii: error
2324
internal-rules/no-dir-import: error
2425

2526
##############################################################################
@@ -681,6 +682,7 @@ overrides:
681682
no-console: off
682683
- files: 'benchmark/**'
683684
rules:
685+
internal-rules/only-ascii: [error, { allowEmoji: true }]
684686
node/no-sync: off
685687
node/no-missing-require: off
686688
import/no-nodejs-modules: off
@@ -689,6 +691,7 @@ overrides:
689691
no-await-in-loop: off
690692
- files: 'resources/**'
691693
rules:
694+
internal-rules/only-ascii: [error, { allowEmoji: true }]
692695
node/no-unpublished-require: off
693696
node/no-sync: off
694697
import/no-extraneous-dependencies: [error, { devDependencies: true }]

benchmark/benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ async function runBenchmarks(benchmarks, benchmarkProjects) {
249249

250250
if (i === 0) {
251251
const { name } = await sampleModule(modulePath);
252-
console.log('⏱ ' + name);
252+
console.log('⏱ ' + name);
253253
}
254254

255255
try {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict';
22

3+
const onlyASCII = require('./only-ascii');
34
const noDirImport = require('./no-dir-import');
45

56
module.exports = {
67
rules: {
8+
'only-ascii': onlyASCII,
79
'no-dir-import': noDirImport,
810
},
911
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
module.exports = {
4+
meta: {
5+
schema: [
6+
{
7+
type: 'object',
8+
properties: {
9+
allowEmoji: {
10+
type: 'boolean',
11+
},
12+
},
13+
additionalProperties: false,
14+
},
15+
],
16+
},
17+
create: onlyASCII,
18+
};
19+
20+
function onlyASCII(context) {
21+
const regExp = context.options[0]?.allowEmoji === true
22+
? /[^\p{ASCII}\p{Emoji}]+/ug
23+
: /\P{ASCII}+/ug;
24+
25+
return {
26+
Program() {
27+
const sourceCode = context.getSourceCode();
28+
const text = sourceCode.getText();
29+
30+
for (const match of text.matchAll(regExp)) {
31+
context.report({
32+
loc: sourceCode.getLocFromIndex(match.index),
33+
message: `Non-ASCII character "${match[0]}" found.`,
34+
});
35+
}
36+
},
37+
};
38+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
22
"name": "eslint-plugin-graphql-internal",
3-
"version": "0.0.0"
3+
"version": "0.0.0",
4+
"private": true,
5+
"engines": {
6+
"node": ">= 14.0.0"
7+
}
48
}

src/language/__tests__/lexer-test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ describe('Lexer', () => {
706706
message: 'Syntax Error: Invalid number, expected digit but got: "_".',
707707
locations: [{ line: 1, column: 2 }],
708708
});
709-
expectSyntaxError('').to.deep.equal({
709+
expectSyntaxError('1\u00DF').to.deep.equal({
710710
message: 'Syntax Error: Cannot parse the unexpected character "\\u00DF".',
711711
locations: [{ line: 1, column: 2 }],
712712
});
@@ -718,10 +718,6 @@ describe('Lexer', () => {
718718
message: 'Syntax Error: Invalid number, expected digit but got: "_".',
719719
locations: [{ line: 1, column: 6 }],
720720
});
721-
expectSyntaxError('1ß').to.deep.equal({
722-
message: 'Syntax Error: Cannot parse the unexpected character "\\u00DF".',
723-
locations: [{ line: 1, column: 2 }],
724-
});
725721
});
726722

727723
it('lexes punctuation', () => {

0 commit comments

Comments
 (0)