Skip to content
Merged
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
39 changes: 29 additions & 10 deletions src/rules/no-duplicate-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { normalizeIdentifier } from "micromark-util-normalize-identifier";
//-----------------------------------------------------------------------------

/**
* @import { Definition, FootnoteDefinition } from "mdast";
* @import { MarkdownRuleDefinition } from "../types.js";
* @typedef {"duplicateDefinition" | "duplicateFootnoteDefinition"} NoDuplicateDefinitionsMessageIds
* @typedef {[{ allowDefinitions?: string[], allowFootnoteDefinitions?: string[] }]} NoDuplicateDefinitionsOptions
Expand All @@ -37,9 +38,9 @@ export default {

messages: {
duplicateDefinition:
"Unexpected duplicate definition `{{ identifier }}` found.",
"Unexpected duplicate definition `{{ identifier }}` (label: `{{ label }}`) found. First defined at line {{ firstLine }} (label: `{{ firstLabel }}`).",
duplicateFootnoteDefinition:
"Unexpected duplicate footnote definition `{{ identifier }}` found.",
"Unexpected duplicate footnote definition `{{ identifier }}` (label: `{{ label }}`) found. First defined at line {{ firstLine }} (label: `{{ firstLabel }}`).",
},

schema: [
Expand Down Expand Up @@ -85,11 +86,11 @@ export default {
),
);

/** @type {Set<string>} */
const definitions = new Set();
/** @type {Map<string, Definition>} */
const definitions = new Map();

/** @type {Set<string>} */
const footnoteDefinitions = new Set();
/** @type {Map<string, FootnoteDefinition>} */
const footnoteDefinitions = new Map();

return {
definition(node) {
Expand All @@ -98,13 +99,22 @@ export default {
}

if (definitions.has(node.identifier)) {
const firstDefinitionNode = definitions.get(
node.identifier,
);
context.report({
node,
messageId: "duplicateDefinition",
data: { identifier: node.identifier },
data: {
identifier: node.identifier,
label: node.label.trim(),
firstLine:
firstDefinitionNode.position.start.line.toString(),
firstLabel: firstDefinitionNode.label.trim(),
},
});
} else {
definitions.add(node.identifier);
definitions.set(node.identifier, node);
}
},

Expand All @@ -114,13 +124,22 @@ export default {
}

if (footnoteDefinitions.has(node.identifier)) {
const firstFootnoteDefinitionNode = footnoteDefinitions.get(
node.identifier,
);
context.report({
node,
messageId: "duplicateFootnoteDefinition",
data: { identifier: node.identifier },
data: {
identifier: node.identifier,
label: node.label,
firstLine:
firstFootnoteDefinitionNode.position.start.line.toString(),
firstLabel: firstFootnoteDefinitionNode.label,
},
});
} else {
footnoteDefinitions.add(node.identifier);
footnoteDefinitions.set(node.identifier, node);
}
},
};
Expand Down
14 changes: 10 additions & 4 deletions src/rules/no-empty-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export default {

messages: {
emptyDefinition:
"Unexpected empty definition `{{ identifier }}` found.",
"Unexpected empty definition `{{ identifier }}` (label: `{{ label }}`) found.",
emptyFootnoteDefinition:
"Unexpected empty footnote definition `{{ identifier }}` found.",
"Unexpected empty footnote definition `{{ identifier }}` (label: `{{ label }}`) found.",
},

schema: [
Expand Down Expand Up @@ -115,7 +115,10 @@ export default {
context.report({
loc: node.position,
messageId: "emptyDefinition",
data: { identifier: node.identifier },
data: {
identifier: node.identifier,
label: node.label.trim(),
Comment thread
crimsonjay0 marked this conversation as resolved.
},
});
}
},
Expand All @@ -134,7 +137,10 @@ export default {
context.report({
loc: node.position,
messageId: "emptyFootnoteDefinition",
data: { identifier: node.identifier },
data: {
identifier: node.identifier,
label: node.label,
},
});
}
},
Expand Down
14 changes: 10 additions & 4 deletions src/rules/no-unused-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export default {

messages: {
unusedDefinition:
"Unexpected unused definition `{{ identifier }}` found.",
"Unexpected unused definition `{{ identifier }}` (label: `{{ label }}`) found.",
unusedFootnoteDefinition:
"Unexpected unused footnote definition `{{ identifier }}` found.",
"Unexpected unused footnote definition `{{ identifier }}` (label: `{{ label }}`) found.",
},

schema: [
Expand Down Expand Up @@ -130,7 +130,10 @@ export default {
context.report({
node: definition,
messageId: "unusedDefinition",
data: { identifier: definition.identifier },
data: {
identifier: definition.identifier,
label: definition.label.trim(),
Comment thread
lumirlumir marked this conversation as resolved.
},
});
}
}
Expand All @@ -144,7 +147,10 @@ export default {
context.report({
node: footnoteDefinition,
messageId: "unusedFootnoteDefinition",
data: { identifier: footnoteDefinition.identifier },
data: {
identifier: footnoteDefinition.identifier,
label: footnoteDefinition.label,
},
});
}
}
Expand Down
98 changes: 84 additions & 14 deletions tests/rules/no-duplicate-definitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,12 @@ ruleTester.run("no-duplicate-definitions", rule, {
errors: [
{
messageId: "duplicateDefinition",
data: { identifier: "mercury" },
data: {
identifier: "mercury",
label: "mercury",
firstLine: "2",
firstLabel: "mercury",
},
line: 3,
column: 1,
endLine: 3,
Expand All @@ -204,23 +209,38 @@ ruleTester.run("no-duplicate-definitions", rule, {
errors: [
{
messageId: "duplicateDefinition",
data: { identifier: "mercury" },
data: {
identifier: "mercury",
label: "mercury",
firstLine: "2",
firstLabel: "mercury",
},
line: 3,
column: 1,
endLine: 3,
endColumn: 38,
},
{
messageId: "duplicateDefinition",
data: { identifier: "mercury" },
data: {
identifier: "mercury",
label: "mercury",
firstLine: "2",
firstLabel: "mercury",
},
line: 4,
column: 1,
endLine: 4,
endColumn: 38,
},
{
messageId: "duplicateDefinition",
data: { identifier: "mercury" },
data: {
identifier: "mercury",
label: "mercury",
firstLine: "2",
firstLabel: "mercury",
},
line: 5,
column: 1,
endLine: 5,
Expand All @@ -237,7 +257,12 @@ ruleTester.run("no-duplicate-definitions", rule, {
errors: [
{
messageId: "duplicateDefinition",
data: { identifier: "mercury" },
data: {
identifier: "mercury",
label: "Mercury",
firstLine: "2",
firstLabel: "mercury",
},
line: 3,
column: 1,
endLine: 3,
Expand All @@ -254,7 +279,12 @@ ruleTester.run("no-duplicate-definitions", rule, {
errors: [
{
messageId: "duplicateDefinition",
data: { identifier: "mercury" },
data: {
identifier: "mercury",
label: "mercury",
firstLine: "2",
firstLabel: "mercury",
},
line: 3,
column: 1,
endLine: 3,
Expand All @@ -278,7 +308,12 @@ ruleTester.run("no-duplicate-definitions", rule, {
errors: [
{
messageId: "duplicateDefinition",
data: { identifier: "mercury" },
data: {
identifier: "mercury",
label: "mercury",
firstLine: "2",
firstLabel: "mercury",
},
line: 3,
column: 1,
endLine: 3,
Expand All @@ -295,7 +330,12 @@ ruleTester.run("no-duplicate-definitions", rule, {
errors: [
{
messageId: "duplicateFootnoteDefinition",
data: { identifier: "mercury" },
data: {
identifier: "mercury",
label: "mercury",
firstLine: "2",
firstLabel: "mercury",
},
line: 3,
column: 1,
endLine: 3,
Expand All @@ -314,23 +354,38 @@ ruleTester.run("no-duplicate-definitions", rule, {
errors: [
{
messageId: "duplicateFootnoteDefinition",
data: { identifier: "mercury" },
data: {
identifier: "mercury",
label: "mercury",
firstLine: "2",
firstLabel: "mercury",
},
line: 3,
column: 1,
endLine: 3,
endColumn: 26,
},
{
messageId: "duplicateFootnoteDefinition",
data: { identifier: "mercury" },
data: {
identifier: "mercury",
label: "mercury",
firstLine: "2",
firstLabel: "mercury",
},
line: 4,
column: 1,
endLine: 4,
endColumn: 26,
},
{
messageId: "duplicateFootnoteDefinition",
data: { identifier: "mercury" },
data: {
identifier: "mercury",
label: "mercury",
firstLine: "2",
firstLabel: "mercury",
},
line: 5,
column: 1,
endLine: 5,
Expand All @@ -347,7 +402,12 @@ ruleTester.run("no-duplicate-definitions", rule, {
errors: [
{
messageId: "duplicateFootnoteDefinition",
data: { identifier: "mercury" },
data: {
identifier: "mercury",
label: "Mercury",
firstLine: "2",
firstLabel: "mercury",
},
line: 3,
column: 1,
endLine: 3,
Expand All @@ -371,7 +431,12 @@ ruleTester.run("no-duplicate-definitions", rule, {
errors: [
{
messageId: "duplicateFootnoteDefinition",
data: { identifier: "mercury" },
data: {
identifier: "mercury",
label: "mercury",
firstLine: "2",
firstLabel: "mercury",
},
line: 3,
column: 1,
endLine: 3,
Expand All @@ -397,7 +462,12 @@ ruleTester.run("no-duplicate-definitions", rule, {
errors: [
{
messageId: "duplicateDefinition",
data: { identifier: "mercury" },
data: {
identifier: "mercury",
label: "mercury",
firstLine: "2",
firstLabel: "mercury",
},
line: 12,
column: 1,
endLine: 12,
Expand Down
Loading
Loading