Skip to content
Draft
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
72 changes: 36 additions & 36 deletions packages/eslint-plugin-mark/src/rules/code-lang-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,24 @@
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
*/

// @ts-nocheck -- TODO

// --------------------------------------------------------------------------------
// Import
// --------------------------------------------------------------------------------

import { URL_RULE_DOCS } from '../core/constants.js';

// --------------------------------------------------------------------------------
// Typedefs
// Typedef
// --------------------------------------------------------------------------------

/**
* @import { RuleModule } from '../core/types.js';
* @typedef {[{ ignores: string[], override: Record<string, string> }]} RuleOptions
* @typedef {[{ allow: string[], override: Record<string, string> }]} RuleOptions
* @typedef {'codeLangShorthand'} MessageIds
*/

// --------------------------------------------------------------------------------
// Helpers
// Helper
// --------------------------------------------------------------------------------

/** @satisfies {Record<string, string>} */
Expand Down Expand Up @@ -123,7 +121,7 @@ export default {
{
type: 'object',
properties: {
ignores: {
allow: {
type: 'array',
items: {
enum: Object.keys(langShorthandMap),
Expand All @@ -143,7 +141,7 @@ export default {

defaultOptions: [
{
ignores: [],
allow: [],
override: {},
},
],
Expand All @@ -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 [{ ignores, 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.
);
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: {
Expand All @@ -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);
},
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ const foo = 'bar';
\`\`\``,
},

// Options - ignores
// Options - allow
{
name: 'Ignored lang identifier',
code: `\`\`\`javascript
const foo = 'bar';
\`\`\``,
options: [
{
ignores: ['javascript'],
allow: ['javascript'],
},
],
},
Expand All @@ -53,7 +53,7 @@ const foo = 'bar';
\`\`\``,
options: [
{
ignores: ['javascript', 'typescript'],
allow: ['javascript', 'typescript'],
},
],
},
Expand Down
Loading