forked from angular-eslint/angular-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin-template): add no duplicate attributes rule
Closes angular-eslint#293
- Loading branch information
1 parent
926c402
commit acdc80a
Showing
4 changed files
with
294 additions
and
0 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
138 changes: 138 additions & 0 deletions
138
packages/eslint-plugin-template/src/rules/no-duplicate-attributes.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,138 @@ | ||
import { | ||
BindingType, | ||
TmplAstBoundAttribute, | ||
TmplAstBoundEvent, | ||
TmplAstElement, | ||
TmplAstTextAttribute, | ||
ParsedEventType, | ||
} from '@angular/compiler'; | ||
import { | ||
createESLintRule, | ||
getTemplateParserServices, | ||
} from '../utils/create-eslint-rule'; | ||
|
||
type Options = []; | ||
export type MessageIds = 'noDuplicateAttributes'; | ||
export const RULE_NAME = 'no-duplicate-attributes'; | ||
|
||
export default createESLintRule<Options, MessageIds>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: | ||
'Ensures that there are no duplicate input properties or output event listeners', | ||
category: 'Possible Errors', | ||
recommended: false, | ||
}, | ||
schema: [], | ||
messages: { | ||
noDuplicateAttributes: 'Duplicate attribute "{{attributeName}}"', | ||
}, | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const parserServices = getTemplateParserServices(context); | ||
|
||
return { | ||
Element({ inputs, outputs, attributes }: TmplAstElement) { | ||
const duplicateInputsAndAttributes = findDuplicates([ | ||
...inputs, | ||
...attributes, | ||
]); | ||
|
||
const nonTwoWayBindingOutputs = outputs.filter((output) => { | ||
return !inputs.some( | ||
(input) => | ||
input.sourceSpan.start === output.sourceSpan.start && | ||
input.sourceSpan.end === output.sourceSpan.end, | ||
); | ||
}); | ||
|
||
const duplicateOutputs = findDuplicates(nonTwoWayBindingOutputs); | ||
|
||
const allDuplicates = [ | ||
...duplicateInputsAndAttributes, | ||
...duplicateOutputs, | ||
]; | ||
|
||
allDuplicates.forEach((duplicate) => { | ||
const loc = parserServices.convertNodeSourceSpanToLoc( | ||
duplicate.sourceSpan, | ||
); | ||
|
||
context.report({ | ||
messageId: 'noDuplicateAttributes', | ||
loc, | ||
data: { | ||
attributeName: getAttributeName(duplicate), | ||
}, | ||
}); | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
interface BoundAttribute extends Omit<TmplAstBoundAttribute, 'type'> { | ||
type: 'BoundAttribute'; | ||
__originalType: BindingType; | ||
} | ||
|
||
interface BoundEvent extends Omit<TmplAstBoundEvent, 'type'> { | ||
type: 'BoundEvent'; | ||
__originalType: ParsedEventType; | ||
} | ||
|
||
function getAttributeName( | ||
attribute: | ||
| BoundAttribute | ||
| TmplAstTextAttribute | ||
| BoundEvent | ||
| { name: string }, | ||
): string { | ||
if ('type' in attribute) { | ||
if (attribute.type === 'BoundAttribute') { | ||
switch (attribute.__originalType) { | ||
case BindingType.Class: | ||
return `class.${attribute.name}`; | ||
case BindingType.Style: | ||
return `style.${attribute.name}${ | ||
attribute.unit ? '.' + attribute.unit : '' | ||
}`; | ||
case BindingType.Animation: | ||
return `@${attribute.name}`; | ||
} | ||
} else if (attribute.type === 'BoundEvent') { | ||
if (attribute.__originalType === ParsedEventType.Animation) { | ||
return `@${attribute.name}${ | ||
attribute.phase ? '.' + attribute.phase : '' | ||
}`; | ||
} | ||
|
||
if (attribute.target) { | ||
return `${attribute.target}:${attribute.name}`; | ||
} | ||
} | ||
} | ||
|
||
return attribute.name; | ||
} | ||
|
||
function findDuplicates( | ||
elements: Array<TmplAstBoundEvent>, | ||
): Array<TmplAstBoundEvent>; | ||
function findDuplicates( | ||
elements: Array<TmplAstBoundAttribute | TmplAstTextAttribute>, | ||
): Array<TmplAstBoundAttribute | TmplAstTextAttribute>; | ||
function findDuplicates( | ||
elements: Array<{ name: string }>, | ||
): Array<{ name: string }> { | ||
return elements.filter((element) => { | ||
return elements.some( | ||
(otherElement) => | ||
otherElement !== element && | ||
getAttributeName(otherElement) === getAttributeName(element), | ||
); | ||
}); | ||
} |
151 changes: 151 additions & 0 deletions
151
packages/eslint-plugin-template/tests/rules/no-duplicate-attributes.test.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,151 @@ | ||
import { | ||
convertAnnotatedSourceToFailureCase, | ||
RuleTester, | ||
} from '@angular-eslint/utils'; | ||
import rule, { | ||
MessageIds, | ||
RULE_NAME, | ||
} from '../../src/rules/no-duplicate-attributes'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@angular-eslint/template-parser', | ||
}); | ||
|
||
const messageId: MessageIds = 'noDuplicateAttributes'; | ||
|
||
ruleTester.run(RULE_NAME, rule, { | ||
valid: [ | ||
'<input name="foo">', | ||
'<input [name]="foo">', | ||
'<input (change)="bar()">', | ||
'<input [(ngModel)]="foo">', | ||
'<input [(ngModel)]="model" (ngModelChange)="modelChanged()">', | ||
'<div (@fade.start)="animationStarted($event)" (@fade.done)="animationDone($event)"></div>', | ||
'<div (window:keydown)="windowKeydown($event)" (document:keydown)="documentKeydown($event)" (document:keyup)="documentKeyup($event)" (keyup)="keyup($event)" (keydown)="keydown($event)"></div>', | ||
'<div [style.width.px]="col.width" [width]="col.width"></div>', | ||
'<button [class.disabled]="!enabled" [disabled]="!enabled"></button>', | ||
'<button [@.disabled]="!enabled" [.disabled]="!enabled"></button>', | ||
'<div [style.width]="col.width + \'px\'" [width]="col.width"></div>', | ||
], | ||
invalid: [ | ||
convertAnnotatedSourceToFailureCase({ | ||
description: 'should fail with 2 inputs with the same name', | ||
annotatedSource: ` | ||
<input [name]="foo" [name]="bar"> | ||
~~~~~~~~~~~~ ^^^^^^^^^^^^ | ||
`, | ||
messages: [ | ||
{ char: '~', messageId }, | ||
{ char: '^', messageId }, | ||
], | ||
}), | ||
convertAnnotatedSourceToFailureCase({ | ||
description: | ||
'should fail with an input and a text attribute with the same name', | ||
annotatedSource: ` | ||
<input [name]="foo" name="bar"> | ||
~~~~~~~~~~~~ ^^^^^^^^^^ | ||
`, | ||
messages: [ | ||
{ char: '~', messageId }, | ||
{ char: '^', messageId }, | ||
], | ||
}), | ||
convertAnnotatedSourceToFailureCase({ | ||
description: 'should fail with 2 text attributes with the same name', | ||
annotatedSource: ` | ||
<input name="foo" name="bar"> | ||
~~~~~~~~~~ ^^^^^^^^^^ | ||
`, | ||
messages: [ | ||
{ char: '~', messageId }, | ||
{ char: '^', messageId }, | ||
], | ||
}), | ||
convertAnnotatedSourceToFailureCase({ | ||
description: 'should fail with 2 outputs with the same name', | ||
annotatedSource: ` | ||
<input (change)="foo($event)" (change)="bar($event)"> | ||
~~~~~~~~~~~~~~~~~~~~~~ ^^^^^^^^^^^^^^^^^^^^^^ | ||
`, | ||
messages: [ | ||
{ char: '~', messageId }, | ||
{ char: '^', messageId }, | ||
], | ||
}), | ||
convertAnnotatedSourceToFailureCase({ | ||
description: 'should fail with 2 banana in a box with the same name', | ||
annotatedSource: ` | ||
<input [(ngModel)]="model" [(ngModel)]="otherModel"> | ||
~~~~~~~~~~~~~~~~~~~ ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
`, | ||
messages: [ | ||
{ char: '~', messageId }, | ||
{ char: '^', messageId }, | ||
], | ||
}), | ||
convertAnnotatedSourceToFailureCase({ | ||
description: | ||
'should fail with duplicate attributes but allow non duplicates', | ||
annotatedSource: ` | ||
<input [name]="foo" [other]="bam" [name]="bar"> | ||
~~~~~~~~~~~~ ^^^^^^^^^^^^ | ||
`, | ||
messages: [ | ||
{ char: '~', messageId }, | ||
{ char: '^', messageId }, | ||
], | ||
}), | ||
convertAnnotatedSourceToFailureCase({ | ||
description: 'should fail with 3 duplications', | ||
annotatedSource: ` | ||
<input [name]="foo" [name]="bar" [name]="bam"> | ||
~~~~~~~~~~~~ ^^^^^^^^^^^^ ############ | ||
`, | ||
messages: [ | ||
{ char: '~', messageId }, | ||
{ char: '^', messageId }, | ||
{ char: '#', messageId }, | ||
], | ||
}), | ||
convertAnnotatedSourceToFailureCase({ | ||
description: 'should fail multiple combinations of duplicates', | ||
annotatedSource: ` | ||
<input [(ngModel)]="model" [name]="foo" [(ngModel)]="otherModel" name="bar"> | ||
~~~~~~~~~~~~~~~~~~~ ^^^^^^^^^^^^ ######################## %%%%%%%%%% | ||
`, | ||
messages: [ | ||
{ char: '~', messageId }, | ||
{ char: '^', messageId }, | ||
{ char: '#', messageId }, | ||
{ char: '%', messageId }, | ||
], | ||
}), | ||
convertAnnotatedSourceToFailureCase({ | ||
description: 'should fail with multiple animation outputs', | ||
annotatedSource: ` | ||
<input (@fade.start)="animationStarted($event)" (@fade.start)="animationStarted($event)"> | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
`, | ||
messages: [ | ||
{ char: '~', messageId }, | ||
{ char: '^', messageId }, | ||
], | ||
}), | ||
convertAnnotatedSourceToFailureCase({ | ||
description: 'should fail with multiple outputs on the window', | ||
annotatedSource: ` | ||
<input (window:resize)="windowResized($event)" (resize)="resize()" (window:resize)="windowResized2($event)"> | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
`, | ||
messages: [ | ||
{ char: '~', messageId }, | ||
{ char: '^', messageId }, | ||
], | ||
}), | ||
], | ||
}); |