Skip to content

Commit b248383

Browse files
authored
fix: normalize definition identifiers in no-*-definitions rules (#488)
1 parent 1b30119 commit b248383

7 files changed

Lines changed: 323 additions & 7 deletions

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@
9797
"mdast-util-frontmatter": "^2.0.1",
9898
"mdast-util-gfm": "^3.1.0",
9999
"micromark-extension-frontmatter": "^2.0.0",
100-
"micromark-extension-gfm": "^3.0.0"
100+
"micromark-extension-gfm": "^3.0.0",
101+
"micromark-util-normalize-identifier": "^2.0.1"
101102
},
102103
"engines": {
103104
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"

src/rules/no-duplicate-definitions.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
* @author 루밀LuMir(lumirlumir)
44
*/
55

6+
//-----------------------------------------------------------------------------
7+
// Imports
8+
//-----------------------------------------------------------------------------
9+
10+
import { normalizeIdentifier } from "micromark-util-normalize-identifier";
11+
612
//-----------------------------------------------------------------------------
713
// Type Definitions
814
//-----------------------------------------------------------------------------
@@ -67,9 +73,15 @@ export default {
6773
},
6874

6975
create(context) {
70-
const allowDefinitions = new Set(context.options[0]?.allowDefinitions);
76+
const allowDefinitions = new Set(
77+
context.options[0].allowDefinitions.map(identifier =>
78+
normalizeIdentifier(identifier).toLowerCase(),
79+
),
80+
);
7181
const allowFootnoteDefinitions = new Set(
72-
context.options[0]?.allowFootnoteDefinitions,
82+
context.options[0].allowFootnoteDefinitions.map(identifier =>
83+
normalizeIdentifier(identifier).toLowerCase(),
84+
),
7385
);
7486

7587
/** @type {Set<string>} */

src/rules/no-empty-definitions.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// Imports
88
//-----------------------------------------------------------------------------
99

10+
import { normalizeIdentifier } from "micromark-util-normalize-identifier";
1011
import { htmlCommentPattern } from "../util.js";
1112

1213
//-----------------------------------------------------------------------------
@@ -92,9 +93,15 @@ export default {
9293
},
9394

9495
create(context) {
95-
const allowDefinitions = new Set(context.options[0].allowDefinitions);
96+
const allowDefinitions = new Set(
97+
context.options[0].allowDefinitions.map(identifier =>
98+
normalizeIdentifier(identifier).toLowerCase(),
99+
),
100+
);
96101
const allowFootnoteDefinitions = new Set(
97-
context.options[0].allowFootnoteDefinitions,
102+
context.options[0].allowFootnoteDefinitions.map(identifier =>
103+
normalizeIdentifier(identifier).toLowerCase(),
104+
),
98105
);
99106
const [{ checkFootnoteDefinitions }] = context.options;
100107

src/rules/no-unused-definitions.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
* @author 루밀LuMir(lumirlumir)
44
*/
55

6+
//-----------------------------------------------------------------------------
7+
// Imports
8+
//-----------------------------------------------------------------------------
9+
10+
import { normalizeIdentifier } from "micromark-util-normalize-identifier";
11+
612
//-----------------------------------------------------------------------------
713
// Type Definitions
814
//-----------------------------------------------------------------------------
@@ -69,9 +75,15 @@ export default {
6975
},
7076

7177
create(context) {
72-
const allowDefinitions = new Set(context.options[0]?.allowDefinitions);
78+
const allowDefinitions = new Set(
79+
context.options[0].allowDefinitions.map(identifier =>
80+
normalizeIdentifier(identifier).toLowerCase(),
81+
),
82+
);
7383
const allowFootnoteDefinitions = new Set(
74-
context.options[0]?.allowFootnoteDefinitions,
84+
context.options[0].allowFootnoteDefinitions.map(identifier =>
85+
normalizeIdentifier(identifier).toLowerCase(),
86+
),
7587
);
7688

7789
/** @type {Set<string>} Set to track used identifiers */

tests/rules/no-duplicate-definitions.test.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,104 @@ ruleTester.run("no-duplicate-definitions", rule, {
7676
},
7777
],
7878
},
79+
{
80+
code: `
81+
[MERCURY]: https://example.com/mercury/
82+
[mercury]: https://example.com/venus/
83+
`,
84+
options: [
85+
{
86+
allowDefinitions: ["MERCURY"],
87+
},
88+
],
89+
},
90+
{
91+
code: `
92+
[mercury]: https://example.com/mercury/
93+
[MERCURY]: https://example.com/venus/
94+
`,
95+
options: [
96+
{
97+
allowDefinitions: ["mercury"],
98+
},
99+
],
100+
},
101+
{
102+
code: `
103+
[ mercury ]: https://example.com/mercury/
104+
[mercury]: https://example.com/venus/
105+
`,
106+
options: [
107+
{
108+
allowDefinitions: ["mercury"],
109+
},
110+
],
111+
},
112+
{
113+
code: `
114+
[mercury]: https://example.com/mercury/
115+
[ mercury ]: https://example.com/venus/
116+
`,
117+
options: [
118+
{
119+
allowDefinitions: [" mercury "],
120+
},
121+
],
122+
},
123+
{
124+
code: `
125+
[foo bar]: https://example.com/foo-bar/
126+
[foo bar]: https://example.com/foo-bar/
127+
`,
128+
options: [
129+
{
130+
allowDefinitions: ["foo\t\r\nbar"],
131+
},
132+
],
133+
},
134+
{
135+
code: `
136+
[^MERCURY]: Hello, Mercury!
137+
[^mercury]: Hello, Venus!
138+
`,
139+
options: [
140+
{
141+
allowFootnoteDefinitions: ["MERCURY"],
142+
},
143+
],
144+
},
145+
{
146+
code: `
147+
[^mercury]: Hello, Mercury!
148+
[^MERCURY]: Hello, Venus!
149+
`,
150+
options: [
151+
{
152+
allowFootnoteDefinitions: ["mercury"],
153+
},
154+
],
155+
},
156+
// This test case is skipped for non-Node environments like Bun
157+
...(typeof process !== "undefined" &&
158+
process.release?.name === "node" &&
159+
!process.versions?.bun
160+
? [
161+
{
162+
code: `
163+
[Grüsse]: https://example.com/
164+
[Grüsse]: https://example.com/
165+
`,
166+
options: [{ allowDefinitions: ["GRÜẞE"] }],
167+
},
168+
{
169+
code: `
170+
[^Grüsse]: Grüsse
171+
[^Grüsse]: Grüsse
172+
`,
173+
options: [{ allowFootnoteDefinitions: ["GRÜẞE"] }],
174+
},
175+
]
176+
: []),
79177
],
80178

81179
invalid: [

tests/rules/no-empty-definitions.test.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,89 @@ ruleTester.run("no-empty-definitions", rule, {
7474
},
7575
],
7676
},
77+
{
78+
code: "[FOO]: #",
79+
options: [{ allowDefinitions: ["FOO"] }],
80+
},
81+
{
82+
code: "[foo]: #",
83+
options: [{ allowDefinitions: ["FOO"] }],
84+
},
85+
{
86+
code: "[FOO]: #",
87+
options: [{ allowDefinitions: ["foo"] }],
88+
},
89+
{
90+
code: "[ foo ]: #",
91+
options: [{ allowDefinitions: ["foo"] }],
92+
},
93+
{
94+
code: "[foo]: #",
95+
options: [{ allowDefinitions: [" foo "] }],
96+
},
97+
{
98+
code: "[foo bar]: #",
99+
options: [{ allowDefinitions: ["foo\t\r\nbar"] }],
100+
},
101+
{
102+
code: "[FOO]: <>",
103+
options: [{ allowDefinitions: ["FOO"] }],
104+
},
105+
{
106+
code: "[foo]: <>",
107+
options: [{ allowDefinitions: ["FOO"] }],
108+
},
109+
{
110+
code: "[FOO]: <>",
111+
options: [{ allowDefinitions: ["foo"] }],
112+
},
113+
{
114+
code: "[ foo ]: <>",
115+
options: [{ allowDefinitions: ["foo"] }],
116+
},
117+
{
118+
code: "[foo]: <>",
119+
options: [{ allowDefinitions: [" foo "] }],
120+
},
121+
{
122+
code: "[foo bar]: <>",
123+
options: [{ allowDefinitions: ["foo\t\r\nbar"] }],
124+
},
125+
{
126+
code: "[^NOTE]:",
127+
options: [{ allowFootnoteDefinitions: ["NOTE"] }],
128+
},
129+
{
130+
code: "[^note]:",
131+
options: [{ allowFootnoteDefinitions: ["NOTE"] }],
132+
},
133+
{
134+
code: "[^NOTE]:",
135+
options: [{ allowFootnoteDefinitions: ["note"] }],
136+
},
137+
{
138+
code: "[^note]:",
139+
options: [{ allowFootnoteDefinitions: [" note "] }],
140+
},
141+
// This test case is skipped for non-Node environments like Bun
142+
...(typeof process !== "undefined" &&
143+
process.release?.name === "node" &&
144+
!process.versions?.bun
145+
? [
146+
{
147+
code: "[Grüsse]: #",
148+
options: [{ allowDefinitions: ["GRÜẞE"] }],
149+
},
150+
{
151+
code: "[Grüsse]: <>",
152+
options: [{ allowDefinitions: ["GRÜẞE"] }],
153+
},
154+
{
155+
code: "[^Grüsse]:",
156+
options: [{ allowFootnoteDefinitions: ["GRÜẞE"] }],
157+
},
158+
]
159+
: []),
77160
],
78161
invalid: [
79162
{

0 commit comments

Comments
 (0)