-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add `no-duplicate-dt` rule
- Loading branch information
Showing
18 changed files
with
353 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/@markuplint/rules/src/no-duplicate-dt/README.ja.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
description: '`<dl>`内の名前の重複禁止' | ||
--- | ||
|
||
# `no-duplicate-dt` | ||
|
||
<!-- textlint-disable ja-technical-writing/ja-no-mixed-period --> | ||
|
||
`<dl>`内の名前の重複禁止 | ||
|
||
❌ 間違ったコード例 | ||
|
||
```html | ||
<dl> | ||
<dt>名前1</dt> | ||
<dd>内容1</dd> | ||
<dt>名前1</dt> | ||
<dd>内容2</dd> | ||
<div> | ||
<dt>名前1</dt> | ||
<dd>内容3</dd> | ||
</div> | ||
</dl> | ||
``` | ||
|
||
✅ 正しいコード例 | ||
|
||
```html | ||
<dl> | ||
<dt>名前1</dt> | ||
<dd>内容1</dd> | ||
<dt>名前2</dt> | ||
<dd>内容2</dd> | ||
<div> | ||
<dt>名前3</dt> | ||
<dd>内容3</dd> | ||
</div> | ||
</dl> | ||
``` | ||
|
||
<!-- textlint-enable ja-technical-writing/ja-no-mixed-period --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
id: no-duplicate-dt | ||
description: No duplicate names in `<dl>` | ||
--- | ||
|
||
# `no-duplicate-dt` | ||
|
||
No duplicate names in `<dl>`. | ||
|
||
❌ Examples of **incorrect** code for this rule | ||
|
||
```html | ||
<dl> | ||
<dt>Name 1</dt> | ||
<dd>Content 1</dd> | ||
<dt>Name 1</dt> | ||
<dd>Content 2</dd> | ||
<div> | ||
<dt>Name 1</dt> | ||
<dd>Content 3</dd> | ||
</div> | ||
</dl> | ||
``` | ||
|
||
✅ Examples of **correct** code for this rule | ||
|
||
```html | ||
<dl> | ||
<dt>Name 1</dt> | ||
<dd>Content 1</dd> | ||
<dt>Name 2</dt> | ||
<dd>Content 2</dd> | ||
<div> | ||
<dt>Name 3</dt> | ||
<dd>Content 3</dd> | ||
</div> | ||
</dl> | ||
``` |
84 changes: 84 additions & 0 deletions
84
packages/@markuplint/rules/src/no-duplicate-dt/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { mlRuleTest } from 'markuplint'; | ||
import { test, expect } from 'vitest'; | ||
|
||
import rule from './index.js'; | ||
|
||
test('Duplicate in content', async () => { | ||
expect( | ||
( | ||
await mlRuleTest( | ||
rule, | ||
` | ||
<dl> | ||
<dt>name1</dt> | ||
<dd>description1</dd> | ||
<dt>name1</dt> | ||
<dd>description2</dd> | ||
</dl> | ||
`, | ||
) | ||
).violations, | ||
).toStrictEqual([ | ||
{ | ||
severity: 'error', | ||
line: 5, | ||
col: 3, | ||
raw: '<dt>', | ||
message: 'The name duplicated', | ||
}, | ||
]); | ||
}); | ||
|
||
test('Duplicate with <div>', async () => { | ||
expect( | ||
( | ||
await mlRuleTest( | ||
rule, | ||
` | ||
<dl> | ||
<div> | ||
<dt>name1</dt> | ||
<dd>description1</dd> | ||
</div> | ||
<div> | ||
<dt>name1</dt> | ||
<dd>description2</dd> | ||
</div> | ||
</dl> | ||
`, | ||
) | ||
).violations, | ||
).toStrictEqual([ | ||
{ | ||
severity: 'error', | ||
line: 8, | ||
col: 5, | ||
raw: '<dt>', | ||
message: 'The name duplicated', | ||
}, | ||
]); | ||
}); | ||
|
||
test('Nested', async () => { | ||
expect( | ||
( | ||
await mlRuleTest( | ||
rule, | ||
` | ||
<dl> | ||
<div> | ||
<dt>name1</dt> | ||
<dd>description1</dd> | ||
</div> | ||
<dd> | ||
<dl> | ||
<dt>name1</dt> | ||
<dd>description2</dd> | ||
</dl> | ||
</dd> | ||
</dl> | ||
`, | ||
) | ||
).violations, | ||
).toStrictEqual([]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { createRule } from '@markuplint/ml-core'; | ||
|
||
export default createRule({ | ||
meta: { | ||
category: 'validation', | ||
}, | ||
verify({ document, report, t }) { | ||
const dlList = [...document.querySelectorAll('dl')]; | ||
for (const dl of dlList) { | ||
const dtList = [...dl.querySelectorAll(':scope > dt, :scope > div > dt')]; | ||
|
||
const names = new Set<string>(); | ||
for (const dt of dtList) { | ||
// TODO: Supoort for alternative text for images and accessible names for contained elements. | ||
const name = dt.textContent?.trim(); | ||
if (!name) { | ||
continue; | ||
} | ||
if (name) { | ||
if (names.has(name)) { | ||
report({ | ||
scope: dt, | ||
message: t('{0} {1:c}', t('The {0}', 'name'), 'duplicated'), | ||
}); | ||
continue; | ||
} | ||
names.add(name); | ||
} | ||
} | ||
} | ||
}, | ||
}); |
29 changes: 29 additions & 0 deletions
29
packages/@markuplint/rules/src/no-duplicate-dt/schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"definitions": { | ||
"value": { | ||
"type": "boolean", | ||
"description": "No duplicate names in `<dl>`", | ||
"description:ja": "`<dl>`内の名前の重複禁止" | ||
} | ||
}, | ||
"oneOf": [ | ||
{ | ||
"$ref": "#/definitions/value" | ||
}, | ||
{ | ||
"type": "object", | ||
"additionalProperties": false, | ||
"properties": { | ||
"value": { "$ref": "#/definitions/value" }, | ||
"severity": { | ||
"$ref": "https://raw.githubusercontent.com/markuplint/markuplint/main/packages/%40markuplint/ml-config/schema.json#/definitions/severity", | ||
"default": "error" | ||
}, | ||
"reason": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<dl> | ||
<dt>Name 1</dt> | ||
<dd>Definition 1</dd> | ||
<dt>Name 2</dt> | ||
<dd>Definition 2</dd> | ||
<dt>Name 3</dt> | ||
<dd>Definition 3</dd> | ||
<dt>Name 3</dt> | ||
<dd>Definition 3</dd> | ||
</dl> |
Oops, something went wrong.