Skip to content

Commit

Permalink
Forbid non-ASCII characters in JS files (#3053)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed May 4, 2021
1 parent 676c775 commit 777c7e9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.yml
Expand Up @@ -20,6 +20,7 @@ rules:
# See './resources/eslint-internal-rules/README.md'
##############################################################################

internal-rules/only-ascii: error
internal-rules/no-dir-import: error

##############################################################################
Expand Down Expand Up @@ -681,6 +682,7 @@ overrides:
no-console: off
- files: 'benchmark/**'
rules:
internal-rules/only-ascii: [error, { allowEmoji: true }]
node/no-sync: off
node/no-missing-require: off
import/no-nodejs-modules: off
Expand All @@ -689,6 +691,7 @@ overrides:
no-await-in-loop: off
- files: 'resources/**'
rules:
internal-rules/only-ascii: [error, { allowEmoji: true }]
node/no-unpublished-require: off
node/no-sync: off
import/no-extraneous-dependencies: [error, { devDependencies: true }]
Expand Down
2 changes: 1 addition & 1 deletion benchmark/benchmark.js
Expand Up @@ -249,7 +249,7 @@ async function runBenchmarks(benchmarks, benchmarkProjects) {

if (i === 0) {
const { name } = await sampleModule(modulePath);
console.log('⏱ ' + name);
console.log('⏱ ' + name);
}

try {
Expand Down
2 changes: 2 additions & 0 deletions resources/eslint-internal-rules/index.js
@@ -1,9 +1,11 @@
'use strict';

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

module.exports = {
rules: {
'only-ascii': onlyASCII,
'no-dir-import': noDirImport,
},
};
39 changes: 39 additions & 0 deletions resources/eslint-internal-rules/only-ascii.js
@@ -0,0 +1,39 @@
'use strict';

module.exports = {
meta: {
schema: [
{
type: 'object',
properties: {
allowEmoji: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
},
create: onlyASCII,
};

function onlyASCII(context) {
const regExp =
context.options[0]?.allowEmoji === true
? /[^\p{ASCII}\p{Emoji}]+/gu
: /\P{ASCII}+/gu;

return {
Program() {
const sourceCode = context.getSourceCode();
const text = sourceCode.getText();

for (const match of text.matchAll(regExp)) {
context.report({
loc: sourceCode.getLocFromIndex(match.index),
message: `Non-ASCII character "${match[0]}" found.`,
});
}
},
};
}
6 changes: 5 additions & 1 deletion resources/eslint-internal-rules/package.json
@@ -1,4 +1,8 @@
{
"name": "eslint-plugin-graphql-internal",
"version": "0.0.0"
"version": "0.0.0",
"private": true,
"engines": {
"node": ">= 14.0.0"
}
}
6 changes: 1 addition & 5 deletions src/language/__tests__/lexer-test.js
Expand Up @@ -706,7 +706,7 @@ describe('Lexer', () => {
message: 'Syntax Error: Invalid number, expected digit but got: "_".',
locations: [{ line: 1, column: 2 }],
});
expectSyntaxError('').to.deep.equal({
expectSyntaxError('1\u00DF').to.deep.equal({
message: 'Syntax Error: Cannot parse the unexpected character "\\u00DF".',
locations: [{ line: 1, column: 2 }],
});
Expand All @@ -718,10 +718,6 @@ describe('Lexer', () => {
message: 'Syntax Error: Invalid number, expected digit but got: "_".',
locations: [{ line: 1, column: 6 }],
});
expectSyntaxError('1ß').to.deep.equal({
message: 'Syntax Error: Cannot parse the unexpected character "\\u00DF".',
locations: [{ line: 1, column: 2 }],
});
});

it('lexes punctuation', () => {
Expand Down

0 comments on commit 777c7e9

Please sign in to comment.