Skip to content

Commit

Permalink
feat(i18n): add checkDuplicateId option to i18n rule
Browse files Browse the repository at this point in the history
This adds an option to the i18n linting rule in order to oppress duplice id reports

fix angular-eslint#867
  • Loading branch information
Marcus Krahl authored and Marcus Krahl committed Jan 12, 2022
1 parent 2000c0c commit 90a9f6a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
34 changes: 34 additions & 0 deletions packages/eslint-plugin-template/docs/rules/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ interface Options {
* Default: `true`
*/
checkAttributes?: boolean;
/**
* Default: `true`
*/
checkDuplicateId?: boolean;
/**
* Default: `true`
*/
Expand Down Expand Up @@ -1250,6 +1254,36 @@ interface Options {
<h1 i18n="An introduction header for this sample@@custom-id">Hello i18n!</h1>
```

<br>

---

<br>

#### Custom Config

```json
{
"rules": {
"@angular-eslint/template/i18n": [
"error",
{
"checkDuplicateId": false
}
]
}
}
```

<br>

#### ✅ Valid Code

```html
<span i18n="@@custom-id">Some text to translate</span>
<span i18n="@@custom-id">Some text to translate</span>
```

</details>

<br>
33 changes: 21 additions & 12 deletions packages/eslint-plugin-template/src/rules/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Options = [
{
readonly boundTextAllowedPattern?: string;
readonly checkAttributes?: boolean;
readonly checkDuplicateId?: boolean;
readonly checkId?: boolean;
readonly checkText?: boolean;
readonly ignoreAttributes?: readonly string[];
Expand Down Expand Up @@ -91,6 +92,7 @@ export const RULE_NAME = 'i18n';
const DEFAULT_OPTIONS: Options[number] = {
checkAttributes: true,
checkId: true,
checkDuplicateId: true,
checkText: true,
ignoreAttributes: [...DEFAULT_ALLOWED_ATTRIBUTES],
};
Expand Down Expand Up @@ -129,6 +131,10 @@ export default createESLintRule<Options, MessageIds>({
type: 'boolean',
default: DEFAULT_OPTIONS.checkAttributes,
},
checkDuplicateId: {
type: 'boolean',
default: DEFAULT_OPTIONS.checkDuplicateId,
},
checkId: {
type: 'boolean',
default: DEFAULT_OPTIONS.checkId,
Expand Down Expand Up @@ -176,6 +182,7 @@ export default createESLintRule<Options, MessageIds>({
boundTextAllowedPattern,
checkAttributes,
checkId,
checkDuplicateId,
checkText,
ignoreAttributes,
ignoreTags,
Expand Down Expand Up @@ -307,18 +314,20 @@ export default createESLintRule<Options, MessageIds>({
}

function reportDuplicatedCustomIds() {
for (const [customId, sourceSpans] of collectedCustomIds) {
if (sourceSpans.length <= 1) {
break;
}

for (const sourceSpan of sourceSpans) {
const loc = parserServices.convertNodeSourceSpanToLoc(sourceSpan);
context.report({
messageId: 'i18nDuplicateCustomId',
loc,
data: { customId },
});
if (checkDuplicateId) {
for (const [customId, sourceSpans] of collectedCustomIds) {
if (sourceSpans.length <= 1) {
break;
}

for (const sourceSpan of sourceSpans) {
const loc = parserServices.convertNodeSourceSpanToLoc(sourceSpan);
context.report({
messageId: 'i18nDuplicateCustomId',
loc,
data: { customId },
});
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions packages/eslint-plugin-template/tests/rules/i18n/cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ export const valid = [
`,
options: [{ requireDescription: true }],
},
{
code: `
<span i18n="@@custom-id">Some text to translate</span>
<span i18n="@@custom-id">Some text to translate</span>
`,
options: [{ checkDuplicateId: false }],
},
];

export const invalid = [
Expand Down

0 comments on commit 90a9f6a

Please sign in to comment.