Skip to content

Commit 8d6e527

Browse files
committed
also lint mdx, add rule for canonicalReferences
1 parent da8c31f commit 8d6e527

File tree

7 files changed

+1532
-254
lines changed

7 files changed

+1532
-254
lines changed

eslint-local-rules/canonical-references.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,41 @@ export const validInheritDoc = ESLintUtils.RuleCreator.withoutDocs({
6767
defaultOptions: [],
6868
});
6969

70+
export const validMdxCanonicalReferences = ESLintUtils.RuleCreator.withoutDocs({
71+
create(context, optionsWithDefault) {
72+
return {
73+
JSXAttribute(node: AST.JSXAttribute) {
74+
if (node.name.name == "canonicalReference") {
75+
const ref =
76+
node.value.type === "JSXExpressionContainer" ?
77+
node.value.expression
78+
: node.value;
79+
if (!ref || ref.type !== "Literal" || typeof ref.value !== "string") {
80+
context.report({
81+
node: ref || node,
82+
messageId: "shouldBeString",
83+
});
84+
} else if (!referenceSet.has(ref.value)) {
85+
context.report({
86+
node: ref,
87+
messageId: "invalidCanonicalReference",
88+
});
89+
}
90+
}
91+
},
92+
};
93+
},
94+
meta: {
95+
messages: {
96+
shouldBeString: "The canonicalReference value should be a string.",
97+
invalidCanonicalReference: "Unknown canonical reference.",
98+
},
99+
type: "problem",
100+
schema: [],
101+
},
102+
defaultOptions: [],
103+
});
104+
70105
function locForMatch(
71106
source: SourceCode,
72107
node: AST.NodeOrTokenData,

eslint-local-rules/index.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { validInheritDoc } from "./canonical-references.ts";
1+
import {
2+
validInheritDoc,
3+
validMdxCanonicalReferences,
4+
} from "./canonical-references.ts";
25
import { rule as forbidActInDisabledActEnvironment } from "./forbid-act-in-disabled-act-environment.ts";
36
import {
47
importFromExport,
@@ -20,4 +23,5 @@ export default {
2023
"no-duplicate-exports": noDuplicateExports,
2124
"no-relative-imports": noRelativeImports,
2225
"valid-inherit-doc": validInheritDoc,
26+
"mdx-valid-canonical-references": validMdxCanonicalReferences,
2327
};

eslint.config.mjs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import js from "@eslint/js";
2121
import typescriptEslint from "@typescript-eslint/eslint-plugin";
2222
import tsParser from "@typescript-eslint/parser";
2323
import _import from "eslint-plugin-import";
24+
import * as mdx from "eslint-plugin-mdx";
2425
import reactCompiler from "eslint-plugin-react-compiler";
2526
import globals from "globals";
2627

@@ -39,15 +40,18 @@ const compat = new FlatCompat({
3940
*/
4041
const runExtendedRules = !!process.env.EXTENDED_RULES;
4142

43+
const tsPlugins = {
44+
import: fixupPluginRules(_import),
45+
"local-rules": {
46+
rules: localRules,
47+
},
48+
"@typescript-eslint": typescriptEslint,
49+
};
50+
4251
export default [
4352
{
44-
plugins: {
45-
"@typescript-eslint": typescriptEslint,
46-
import: fixupPluginRules(_import),
47-
"local-rules": {
48-
rules: localRules,
49-
},
50-
},
53+
files: ["**/*.ts", "**/*.tsx"],
54+
plugins: tsPlugins,
5155
ignores: ["tests.codegen.ts"],
5256

5357
languageOptions: {
@@ -139,6 +143,7 @@ export default [
139143

140144
plugins: {
141145
"react-compiler": reactCompiler,
146+
...tsPlugins,
142147
},
143148

144149
languageOptions: {
@@ -192,7 +197,6 @@ export default [
192197
],
193198

194199
"import/consistent-type-specifier-style": ["error", "prefer-top-level"],
195-
196200
"local-rules/require-using-disposable": "error",
197201
"local-rules/valid-inherit-doc": "error",
198202
},
@@ -215,6 +219,7 @@ export default [
215219
"**/?(*.)+(test).[jt]sx",
216220
],
217221

222+
plugins: tsPlugins,
218223
languageOptions: {
219224
ecmaVersion: 5,
220225
sourceType: "script",
@@ -257,4 +262,21 @@ export default [
257262
"local-rules/no-relative-imports": "off",
258263
},
259264
},
265+
{
266+
basePath: "docs",
267+
...mdx.flat,
268+
plugins: {
269+
...mdx.flat.plugins,
270+
"local-rules": {
271+
rules: localRules,
272+
},
273+
},
274+
rules: {
275+
"local-rules/mdx-valid-canonical-references": "error",
276+
},
277+
},
278+
{
279+
basePath: "docs",
280+
...mdx.flatCodeBlocks,
281+
},
260282
];

0 commit comments

Comments
 (0)