|
| 1 | +/** |
| 2 | + * @fileoverview Rule to enforce the use of shorthand for code block language identifiers. |
| 3 | + * @author 루밀LuMir(lumirlumir) |
| 4 | + * @see https://shiki.style/languages#bundled-languages |
| 5 | + * @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries |
| 6 | + * @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/entries |
| 7 | + */ |
| 8 | + |
| 9 | +// -------------------------------------------------------------------------------- |
| 10 | +// Import |
| 11 | +// -------------------------------------------------------------------------------- |
| 12 | + |
| 13 | +import { getFileName } from '../../core/helpers/index.js'; |
| 14 | + |
| 15 | +// -------------------------------------------------------------------------------- |
| 16 | +// Typedefs |
| 17 | +// -------------------------------------------------------------------------------- |
| 18 | + |
| 19 | +/** |
| 20 | + * @typedef {import("@eslint/markdown").RuleModule} RuleModule |
| 21 | + * @typedef {import("mdast").Code} Code |
| 22 | + */ |
| 23 | + |
| 24 | +// -------------------------------------------------------------------------------- |
| 25 | +// Helpers |
| 26 | +// -------------------------------------------------------------------------------- |
| 27 | + |
| 28 | +/** @type {Record<string, string>} */ |
| 29 | +const langShorthandMap = Object.freeze({ |
| 30 | + asciidoc: 'adoc', |
| 31 | + batch: 'bat', |
| 32 | + berry: 'be', |
| 33 | + bsl: '1c', |
| 34 | + cadence: 'cdc', |
| 35 | + clojure: 'clj', |
| 36 | + codeql: 'ql', |
| 37 | + coffeescript: 'coffee', |
| 38 | + 'common-lisp': 'lisp', |
| 39 | + csharp: 'c#', |
| 40 | + cypher: 'cql', |
| 41 | + dockerfile: 'docker', |
| 42 | + 'emacs-lisp': 'elisp', |
| 43 | + erlang: 'erl', |
| 44 | + fluent: 'ftl', |
| 45 | + 'fortran-fixed-form': 'f', |
| 46 | + fsharp: 'f#', |
| 47 | + 'glimmer-js': 'gjs', |
| 48 | + 'glimmer-ts': 'gts', |
| 49 | + graphql: 'gql', |
| 50 | + handlebars: 'hbs', |
| 51 | + haskell: 'hs', |
| 52 | + properties: 'ini', |
| 53 | + javascript: 'js', |
| 54 | + jssm: 'fsl', |
| 55 | + julia: 'jl', |
| 56 | + kotlin: 'kt', |
| 57 | + kusto: 'kql', |
| 58 | + lean4: 'lean', |
| 59 | + makefile: 'make', |
| 60 | + markdown: 'md', |
| 61 | + mermaid: 'mmd', |
| 62 | + mipsasm: 'mips', |
| 63 | + narrat: 'nar', |
| 64 | + nextflow: 'nf', |
| 65 | + nushell: 'nu', |
| 66 | + 'objective-c': 'objc', |
| 67 | + pot: 'po', |
| 68 | + potx: 'po', |
| 69 | + powershell: 'ps', |
| 70 | + protobuf: 'proto', |
| 71 | + jade: 'pug', |
| 72 | + python: 'py', |
| 73 | + perl6: 'raku', |
| 74 | + regexp: 'regex', |
| 75 | + ruby: 'rb', |
| 76 | + rust: 'rs', |
| 77 | + '1c-query': 'sdbl', |
| 78 | + shaderlab: 'shader', |
| 79 | + shellscript: 'sh', |
| 80 | + shell: 'sh', |
| 81 | + bash: 'sh', |
| 82 | + zsh: 'sh', |
| 83 | + shellsession: 'console', |
| 84 | + 'closure-templates': 'soy', |
| 85 | + splunk: 'spl', |
| 86 | + stylus: 'styl', |
| 87 | + talonscript: 'talon', |
| 88 | + terraform: 'tf', |
| 89 | + text: 'txt', |
| 90 | + typescript: 'ts', |
| 91 | + typespec: 'tsp', |
| 92 | + typst: 'typ', |
| 93 | + vimscript: 'vim', |
| 94 | + vyper: 'vy', |
| 95 | + wikitext: 'wiki', |
| 96 | + mediawiki: 'wiki', |
| 97 | + wolfram: 'wl', |
| 98 | + yaml: 'yml', |
| 99 | +}); |
| 100 | + |
| 101 | +// -------------------------------------------------------------------------------- |
| 102 | +// Rule Definition |
| 103 | +// -------------------------------------------------------------------------------- |
| 104 | + |
| 105 | +/** @type {RuleModule} */ |
| 106 | +export default { |
| 107 | + meta: { |
| 108 | + type: 'problem', |
| 109 | + |
| 110 | + docs: { |
| 111 | + // @ts-ignore -- TODO: https://github.com/eslint/eslint/issues/19521, https://github.com/eslint/eslint/issues/19523 |
| 112 | + name: getFileName(import.meta.url), |
| 113 | + recommended: true, |
| 114 | + description: 'Enforce the use of shorthand for code block language identifiers', |
| 115 | + url: 'https://github.com/lumirlumir/npm-eslint-plugin-mark', |
| 116 | + }, |
| 117 | + |
| 118 | + fixable: 'code', |
| 119 | + |
| 120 | + schema: [ |
| 121 | + { |
| 122 | + type: 'object', |
| 123 | + properties: { |
| 124 | + ignores: { |
| 125 | + type: 'array', |
| 126 | + items: { |
| 127 | + enum: Object.keys(langShorthandMap), |
| 128 | + }, |
| 129 | + }, |
| 130 | + override: { |
| 131 | + type: 'object', |
| 132 | + additionalProperties: { |
| 133 | + type: 'string', |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + additionalProperties: false, |
| 138 | + }, |
| 139 | + ], |
| 140 | + |
| 141 | + defaultOptions: [ |
| 142 | + { |
| 143 | + ignores: [], |
| 144 | + override: {}, |
| 145 | + }, |
| 146 | + ], |
| 147 | + |
| 148 | + messages: { |
| 149 | + codeLangShorthand: '`{{ lang }}` should be shortened to `{{ langShorthand }}`.', |
| 150 | + }, |
| 151 | + |
| 152 | + language: 'markdown', |
| 153 | + |
| 154 | + dialects: ['commonmark', 'gfm'], |
| 155 | + }, |
| 156 | + |
| 157 | + create(context) { |
| 158 | + return { |
| 159 | + /** @param {Code} node */ |
| 160 | + code(node) { |
| 161 | + const langShorthandMapMerged = Object.fromEntries( |
| 162 | + Object.entries({ |
| 163 | + ...langShorthandMap, |
| 164 | + ...context.options[0].override, // `override` option handling. |
| 165 | + }) |
| 166 | + .map(([key, value]) => [key.toLowerCase(), value.toLowerCase()]) // Normalize keys and values. |
| 167 | + .filter(([key]) => !context.options[0].ignores.includes(key)), // `ignores` option handling. |
| 168 | + ); |
| 169 | + const langShorthand = langShorthandMapMerged[node.lang?.toLowerCase()]; // Normalize lang. |
| 170 | + |
| 171 | + if (langShorthand === undefined) return; |
| 172 | + |
| 173 | + // @ts-ignore -- TODO: https://github.com/eslint/markdown/issues/323 |
| 174 | + const match = context.sourceCode.getText(node).match(node.lang); |
| 175 | + |
| 176 | + const matchIndexStart = match.index; |
| 177 | + const matchIndexEnd = matchIndexStart + match[0].length; |
| 178 | + |
| 179 | + context.report({ |
| 180 | + loc: { |
| 181 | + start: { |
| 182 | + line: node.position.start.line, |
| 183 | + column: node.position.start.column + matchIndexStart, |
| 184 | + }, |
| 185 | + end: { |
| 186 | + line: node.position.start.line, |
| 187 | + column: node.position.start.column + matchIndexEnd, |
| 188 | + }, |
| 189 | + }, |
| 190 | + |
| 191 | + data: { |
| 192 | + lang: node.lang, // Original lang. |
| 193 | + langShorthand, |
| 194 | + }, |
| 195 | + |
| 196 | + messageId: 'codeLangShorthand', |
| 197 | + |
| 198 | + fix(fixer) { |
| 199 | + return fixer.replaceTextRange( |
| 200 | + [ |
| 201 | + node.position.start.offset + matchIndexStart, |
| 202 | + node.position.start.offset + matchIndexEnd, |
| 203 | + ], |
| 204 | + langShorthand, |
| 205 | + ); |
| 206 | + }, |
| 207 | + }); |
| 208 | + }, |
| 209 | + }; |
| 210 | + }, |
| 211 | +}; |
0 commit comments