Skip to content
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

fix(eslint-plugin): [ban-ts-comment] counts graphemes instead of String.prototype.length #5704

Merged
merged 9 commits into from
Jan 24, 2023
1 change: 1 addition & 0 deletions packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@types/marked": "*",
"@types/prettier": "*",
"chalk": "^5.0.1",
"grapheme-splitter": "^1.0.4",
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
"json-schema": "*",
"marked": "^4.0.15",
"prettier": "*",
Expand Down
19 changes: 18 additions & 1 deletion packages/eslint-plugin/src/rules/ban-ts-comment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import { AST_TOKEN_TYPES } from '@typescript-eslint/utils';
import GraphemeSplitter from 'grapheme-splitter';

import * as util from '../util';

let splitter: GraphemeSplitter;
function isASCII(value: string): boolean {
return /^[\u0020-\u007f]*$/u.test(value);
}
function getStringLength(value: string): number {
if (isASCII(value)) {
return value.length;
}
if (!splitter) {
splitter = new GraphemeSplitter();
}
sosukesuzuki marked this conversation as resolved.
Show resolved Hide resolved
return splitter.countGraphemes(value);
}

type DirectiveConfig =
| boolean
| 'allow-with-description'
Expand Down Expand Up @@ -147,7 +162,9 @@ export default util.createRule<[Options], MessageIds>({
minimumDescriptionLength = defaultMinimumDescriptionLength,
} = options;
const format = descriptionFormats.get(fullDirective);
if (description.trim().length < minimumDescriptionLength) {
if (
getStringLength(description.trim()) < minimumDescriptionLength
) {
context.report({
data: { directive, minimumDescriptionLength },
node: comment,
Expand Down
64 changes: 64 additions & 0 deletions packages/eslint-plugin/tests/rules/ban-ts-comment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,22 @@ if (false) {
},
],
},
{
code: noFormat`// @ts-expect-error 👨‍👩‍👧‍👦`,
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
options: [
{
'ts-expect-error': 'allow-with-description',
},
],
errors: [
{
data: { directive: 'expect-error', minimumDescriptionLength: 3 },
messageId: 'tsDirectiveCommentRequiresDescription',
line: 1,
column: 1,
},
],
},
],
});

Expand Down Expand Up @@ -460,6 +476,22 @@ if (false) {
},
],
},
{
code: noFormat`// @ts-ignore 👨‍👩‍👧‍👦`,
options: [
{
'ts-ignore': 'allow-with-description',
},
],
errors: [
{
data: { directive: 'ignore', minimumDescriptionLength: 3 },
messageId: 'tsDirectiveCommentRequiresDescription',
line: 1,
column: 1,
},
],
},
],
});

Expand Down Expand Up @@ -668,6 +700,22 @@ if (false) {
},
],
},
{
code: noFormat`// @ts-nocheck 👨‍👩‍👧‍👦`,
options: [
{
'ts-nocheck': 'allow-with-description',
},
],
errors: [
{
data: { directive: 'nocheck', minimumDescriptionLength: 3 },
messageId: 'tsDirectiveCommentRequiresDescription',
line: 1,
column: 1,
},
],
},
],
});

Expand Down Expand Up @@ -863,5 +911,21 @@ if (false) {
},
],
},
{
code: noFormat`// @ts-check 👨‍👩‍👧‍👦`,
options: [
{
'ts-check': 'allow-with-description',
},
],
errors: [
{
data: { directive: 'check', minimumDescriptionLength: 3 },
messageId: 'tsDirectiveCommentRequiresDescription',
line: 1,
column: 1,
},
],
},
],
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8066,6 +8066,11 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.6
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.9.tgz#041b05df45755e587a24942279b9d113146e1c96"
integrity sha512-NtNxqUcXgpW2iMrfqSfR73Glt39K+BLwWsPs94yR63v45T0Wbej7eRmL5cWfwEgqXnmjQp3zaJTshdRW/qC2ZQ==

grapheme-splitter@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/grapheme-splitter/-/grapheme-splitter-1.0.4.tgz#9cf3a665c6247479896834af35cf1dbb4400767e"
integrity sha512-bzh50DW9kTPM00T8y4o8vQg89Di9oLJVLW/KaOGIXJWP/iqCN6WKYkbNOF04vFLJhwcpYUh9ydh/+5vpOqV4YQ==
bradzacher marked this conversation as resolved.
Show resolved Hide resolved

gray-matter@^4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/gray-matter/-/gray-matter-4.0.3.tgz#e893c064825de73ea1f5f7d88c7a9f7274288798"
Expand Down