Skip to content

Commit

Permalink
Extract to shared helper
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg committed Jan 24, 2023
1 parent e483407 commit 0184de9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 32 deletions.
18 changes: 2 additions & 16 deletions packages/eslint-plugin/src/rules/ban-ts-comment.ts
@@ -1,22 +1,7 @@
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;
}

splitter ??= new GraphemeSplitter();

return splitter.countGraphemes(value);
}

type DirectiveConfig =
| boolean
| 'allow-with-description'
Expand Down Expand Up @@ -163,7 +148,8 @@ export default util.createRule<[Options], MessageIds>({
} = options;
const format = descriptionFormats.get(fullDirective);
if (
getStringLength(description.trim()) < minimumDescriptionLength
util.getStringLength(description.trim()) <
minimumDescriptionLength
) {
context.report({
data: { directive, minimumDescriptionLength },
Expand Down
19 changes: 3 additions & 16 deletions packages/eslint-plugin/src/rules/key-spacing.ts
@@ -1,24 +1,9 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import GraphemeSplitter from 'grapheme-splitter';

import * as util from '../util';
import { getESLintCoreRule } from '../util/getESLintCoreRule';

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;
}

splitter ??= new GraphemeSplitter();

return splitter.countGraphemes(value);
}

const baseRule = getESLintCoreRule('key-spacing');

export type Options = util.InferOptionsTypeFromRule<typeof baseRule>;
Expand Down Expand Up @@ -55,7 +40,9 @@ export default util.createRule<Options, MessageIds>({
*/
function adjustedColumn(position: TSESTree.Position): number {
const line = position.line - 1; // position.line is 1-indexed
return getStringLength(sourceCode.lines[line].slice(0, position.column));
return util.getStringLength(
sourceCode.lines[line].slice(0, position.column),
);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions packages/eslint-plugin/src/util/getStringLength.ts
@@ -0,0 +1,17 @@
import GraphemeSplitter from 'grapheme-splitter';

let splitter: GraphemeSplitter;

function isASCII(value: string): boolean {
return /^[\u0020-\u007f]*$/u.test(value);
}

export function getStringLength(value: string): number {
if (isASCII(value)) {
return value.length;
}

splitter ??= new GraphemeSplitter();

return splitter.countGraphemes(value);
}
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/util/index.ts
Expand Up @@ -5,6 +5,7 @@ export * from './collectUnusedVariables';
export * from './createRule';
export * from './getFunctionHeadLoc';
export * from './getOperatorPrecedence';
export * from './getStringLength';
export * from './getThisExpression';
export * from './getWrappingFixer';
export * from './isNodeEqual';
Expand Down

0 comments on commit 0184de9

Please sign in to comment.