10000 feat(eslint-plugin-template): [i18n] add checkDuplicateId option (#868) · angular-eslint/angular-eslint@edaf46f · GitHub
[go: up one dir, main page]

Skip to content

Commit edaf46f

Browse files
authored
feat(eslint-plugin-template): [i18n] add checkDuplicateId option (#868)
1 parent 5f874d9 commit edaf46f

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

packages/eslint-plugin-template/docs/rules/i18n.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ interface Options {
3535
* Default: `true`
3636
*/
3737
checkAttributes?: boolean;
38+
/**
39+
* Default: `true`
40+
*/
41+
checkDuplicateId?: boolean;
3842
/**
3943
* Default: `true`
4044
*/
@@ -1250,6 +1254,36 @@ interface Options {
12501254
<h1 i18n="An introduction header for this sample@@custom-id">Hello i18n!</h1>
12511255
```
12521256

1257+
<br>
1258+
1259+
---
1260+
1261+
<br>
1262+
1263+
#### Custom Config
1264+
1265+
```json
1266+
{
1267+
"rules": {
1268+
"@angular-eslint/template/i18n": [
1269+
"error",
1270+
{
1271+
"checkDuplicateId": false
1272+
}
1273+
]
1274+
}
1275+
}
1276+
```
1277+
1278+
<br>
1279+
1280+
#### ✅ Valid Code
1281+
1282+
```html
1283+
<span i18n="@@custom-id">Some text to translate</span>
1284+
<span i18n="@@custom-id">Some text to translate</span>
1285+
```
1286+
12531287
</details>
12541288

12551289
<br>

packages/eslint-plugin-template/src/rules/i18n.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type Options = [
5454
{
5555
readonly boundTextAllowedPattern?: string;
5656
readonly checkAttributes?: boolean;
57+
readonly checkDuplicateId?: boolean;
5758
readonly checkId?: boolean;
5859
readonly checkText?: boolean;
5960
readonly ignoreAttributes?: readonly string[];
@@ -91,6 +92,7 @@ export const RULE_NAME = 'i18n';
9192
const DEFAULT_OPTIONS: Options[number] = {
9293
checkAttributes: true,
9394
checkId: true,
95+
checkDuplicateId: true,
9496
checkText: true,
9597
ignoreAttributes: [...DEFAULT_ALLOWED_ATTRIBUTES],
9698
};
@@ -129,6 +131,10 @@ export default createESLintRule<Options, MessageIds>({
129131
type: 'boolean',
130132
default: DEFAULT_OPTIONS.checkAttributes,
131133
},
134+
checkDuplicateId: {
135+
type: 'boolean',
136+
default: DEFAULT_OPTIONS.checkDuplicateId,
137+
},
132138
checkId: {
133139
type: 'boolean',
134140
default: DEFAULT_OPTIONS.checkId,
@@ -176,6 +182,7 @@ export default createESLintRule<Options, MessageIds>({
176182
boundTextAllowedPattern,
177183
checkAttributes,
178184
checkId,
185+
checkDuplicateId,
179186
checkText,
180187
ignoreAttributes,
181188
ignoreTags,
@@ -307,18 +314,20 @@ export default createESLintRule<Options, MessageIds>({
307314
}
308315

309316
function reportDuplicatedCustomIds() {
310-
for (const [customId, sourceSpans] of collectedCustomIds) {
311-
if (sourceSpans.length <= 1) {
312-
break;
313-
}
314-
315-
for (const sourceSpan of sourceSpans) {
316-
const loc = parserServices.convertNodeSourceSpanToLoc(sourceSpan);
317-
context.report({
318-
messageId: 'i18nDuplicateCustomId',
319-
loc,
320-
data: { customId },
321-
});
317+
if (checkDuplicateId) {
318+
for (const [customId, sourceSpans] of collectedCustomIds) {
319+
if (sourceSpans.length <= 1) {
320+
break;
321+
}
322+
323+
for (const sourceSpan of sourceSpans) {
324+
const loc = parserServices.convertNodeSourceSpanToLoc(sourceSpan);
325+
context.report({
326+
messageId: 'i18nDuplicateCustomId',
327+
loc,
328+
data: { customId },
329+
});
330+
}
322331
}
323332
}
324333

packages/eslint-plugin-template/tests/rules/i18n/cases.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ export const valid = [
170170
`,
171171
options: [{ requireDescription: true }],
172172
},
173+
{
174+
code: `
175+
<span i18n="@@custom-id">Some text to translate</span>
176+
<span i18n="@@custom-id">Some text to translate</span>
177+
`,
178+
options: [{ checkDuplicateId: false }],
179+
},
173180
];
174181

175182
export const invalid = [

0 commit comments

Comments
 (0)
0