Skip to content

Forbid non-ASCII characters in JS files #3053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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