From 2753d5684dab3b6299b814a63c3374f366ef869b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=A3=A8=EB=B0=80LuMir?= Date: Wed, 19 Nov 2025 23:19:30 +0900 Subject: [PATCH 1/2] refactor(eslint-plugin-mark): refactor and update docs in `code-lang-shorthand` --- .../src/rules/code-lang-shorthand.js | 10 +++++----- .../src/rules/code-lang-shorthand.test.js | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/eslint-plugin-mark/src/rules/code-lang-shorthand.js b/packages/eslint-plugin-mark/src/rules/code-lang-shorthand.js index 25f221c8..268f5866 100644 --- a/packages/eslint-plugin-mark/src/rules/code-lang-shorthand.js +++ b/packages/eslint-plugin-mark/src/rules/code-lang-shorthand.js @@ -20,7 +20,7 @@ import { URL_RULE_DOCS } from '../core/constants.js'; /** * @import { RuleModule } from '../core/types.js'; - * @typedef {[{ ignores: string[], override: Record }]} RuleOptions + * @typedef {[{ allow: string[], override: Record }]} RuleOptions * @typedef {'codeLangShorthand'} MessageIds */ @@ -123,7 +123,7 @@ export default { { type: 'object', properties: { - ignores: { + allow: { type: 'array', items: { enum: Object.keys(langShorthandMap), @@ -143,7 +143,7 @@ export default { defaultOptions: [ { - ignores: [], + allow: [], override: {}, }, ], @@ -160,14 +160,14 @@ export default { create(context) { return { code(node) { - const [{ ignores, override }] = context.options; + const [{ allow, override }] = context.options; const langShorthandMapMerged = Object.fromEntries( Object.entries({ ...langShorthandMap, ...override, // `override` option handling. }) .map(([key, value]) => [key.toLowerCase(), value.toLowerCase()]) // Normalize keys and values. - .filter(([key]) => !ignores.includes(key)), // `ignores` option handling. + .filter(([key]) => !allow.includes(key)), // `ignores` option handling. ); const langShorthand = langShorthandMapMerged[node.lang?.toLowerCase()]; // Normalize lang. diff --git a/packages/eslint-plugin-mark/src/rules/code-lang-shorthand.test.js b/packages/eslint-plugin-mark/src/rules/code-lang-shorthand.test.js index b1aec561..0962d9c9 100644 --- a/packages/eslint-plugin-mark/src/rules/code-lang-shorthand.test.js +++ b/packages/eslint-plugin-mark/src/rules/code-lang-shorthand.test.js @@ -30,7 +30,7 @@ const foo = 'bar'; \`\`\``, }, - // Options - ignores + // Options - allow { name: 'Ignored lang identifier', code: `\`\`\`javascript @@ -38,7 +38,7 @@ const foo = 'bar'; \`\`\``, options: [ { - ignores: ['javascript'], + allow: ['javascript'], }, ], }, @@ -53,7 +53,7 @@ const foo = 'bar'; \`\`\``, options: [ { - ignores: ['javascript', 'typescript'], + allow: ['javascript', 'typescript'], }, ], }, From 2dda37f5b34607114bac09241780c1ef2e2b8567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=A3=A8=EB=B0=80LuMir?= Date: Sat, 22 Nov 2025 14:01:52 +0900 Subject: [PATCH 2/2] wip --- .../src/rules/code-lang-shorthand.js | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/packages/eslint-plugin-mark/src/rules/code-lang-shorthand.js b/packages/eslint-plugin-mark/src/rules/code-lang-shorthand.js index 268f5866..4df21e88 100644 --- a/packages/eslint-plugin-mark/src/rules/code-lang-shorthand.js +++ b/packages/eslint-plugin-mark/src/rules/code-lang-shorthand.js @@ -6,8 +6,6 @@ * @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/entries */ -// @ts-nocheck -- TODO - // -------------------------------------------------------------------------------- // Import // -------------------------------------------------------------------------------- @@ -15,7 +13,7 @@ import { URL_RULE_DOCS } from '../core/constants.js'; // -------------------------------------------------------------------------------- -// Typedefs +// Typedef // -------------------------------------------------------------------------------- /** @@ -25,7 +23,7 @@ import { URL_RULE_DOCS } from '../core/constants.js'; */ // -------------------------------------------------------------------------------- -// Helpers +// Helper // -------------------------------------------------------------------------------- /** @satisfies {Record} */ @@ -158,36 +156,44 @@ export default { }, create(context) { + const { sourceCode } = context; + const [{ allow, override }] = context.options; + + const langShorthandMapMerged = Object.fromEntries( + Object.entries({ + ...langShorthandMap, + ...override, // `override` option handling. + }) + .map(([key, value]) => [key.toLowerCase(), value.toLowerCase()]) // Normalize keys and values. + .filter(([key]) => !allow.includes(key)), // `allow` option handling. + ); + return { code(node) { - const [{ allow, override }] = context.options; - const langShorthandMapMerged = Object.fromEntries( - Object.entries({ - ...langShorthandMap, - ...override, // `override` option handling. - }) - .map(([key, value]) => [key.toLowerCase(), value.toLowerCase()]) // Normalize keys and values. - .filter(([key]) => !allow.includes(key)), // `ignores` option handling. - ); - const langShorthand = langShorthandMapMerged[node.lang?.toLowerCase()]; // Normalize lang. + if (node.lang === null || node.lang === undefined) { + return; + } + + const langShorthand = langShorthandMapMerged[node.lang.toLowerCase()]; // Normalize lang. - if (langShorthand === undefined) return; + if (langShorthand === undefined) { + return; + } - const match = context.sourceCode.getText(node).match(node.lang); + const [nodeStartOffset] = sourceCode.getRange(node); + const match = sourceCode.getText(node).match(node.lang); - const matchIndexStart = match.index; - const matchIndexEnd = matchIndexStart + match[0].length; + if (match === null) { + return; + } + + const startOffset = nodeStartOffset + match.index; + const endOffset = startOffset + match[0].length; context.report({ loc: { - start: { - line: node.position.start.line, - column: node.position.start.column + matchIndexStart, - }, - end: { - line: node.position.start.line, - column: node.position.start.column + matchIndexEnd, - }, + start: sourceCode.getLocFromIndex(startOffset), + end: sourceCode.getLocFromIndex(endOffset), }, data: { @@ -198,13 +204,7 @@ export default { messageId: 'codeLangShorthand', fix(fixer) { - return fixer.replaceTextRange( - [ - node.position.start.offset + matchIndexStart, - node.position.start.offset + matchIndexEnd, - ], - langShorthand, - ); + return fixer.replaceTextRange([startOffset, endOffset], langShorthand); }, }); },