-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(rule): no distracting elements should be used #760
Merged
mgechev
merged 1 commit into
mgechev:master
from
mohammedzamakhan:no-distracting-elements
Feb 15, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { ElementAst } from '@angular/compiler'; | ||
import { sprintf } from 'sprintf-js'; | ||
import { IRuleMetadata, RuleFailure, Rules } from 'tslint/lib'; | ||
import { SourceFile } from 'typescript/lib/typescript'; | ||
import { NgWalker } from './angular/ngWalker'; | ||
import { BasicTemplateAstVisitor } from './angular/templates/basicTemplateAstVisitor'; | ||
|
||
export class Rule extends Rules.AbstractRule { | ||
static readonly metadata: IRuleMetadata = { | ||
description: 'Enforces that no distracting elements are used', | ||
options: null, | ||
optionsDescription: 'Not configurable.', | ||
rationale: 'Elements that can be visually distracting can cause accessibility issues with visually impaired users.', | ||
ruleName: 'template-no-distracting-elements', | ||
type: 'functionality', | ||
typescriptOnly: true | ||
}; | ||
|
||
static readonly FAILURE_STRING = 'Avoid using <%s/> elements as they create visual accessibility issues.'; | ||
|
||
apply(sourceFile: SourceFile): RuleFailure[] { | ||
return this.applyWithWalker( | ||
new NgWalker(sourceFile, this.getOptions(), { | ||
templateVisitorCtrl: TemplateNoDistractingElementsVisitor | ||
}) | ||
); | ||
} | ||
} | ||
|
||
export function getFailureMessage(element: string) { | ||
return sprintf(Rule.FAILURE_STRING, element); | ||
} | ||
|
||
class TemplateNoDistractingElementsVisitor extends BasicTemplateAstVisitor { | ||
visitElement(prop: ElementAst, context: any): any { | ||
this.validateElement(prop); | ||
super.visitElement(prop, context); | ||
} | ||
|
||
private validateElement(el: ElementAst): void { | ||
if (el.name === 'marquee' || el.name === 'blink') { | ||
const { | ||
sourceSpan: { | ||
end: { offset: endOffset }, | ||
start: { offset: startOffset } | ||
} | ||
} = el; | ||
this.addFailureFromStartToEnd(startOffset, endOffset, getFailureMessage(el.name)); | ||
} | ||
} | ||
} |
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,58 @@ | ||
import { getFailureMessage, Rule } from '../src/templateNoDistractingElementsRule'; | ||
import { assertAnnotated, assertSuccess } from './testHelper'; | ||
|
||
const { | ||
metadata: { ruleName } | ||
} = Rule; | ||
|
||
describe(ruleName, () => { | ||
describe('failure', () => { | ||
it('should fail when distracting element marquee is used', () => { | ||
const source = ` | ||
@Component({ | ||
template: \` | ||
<marquee></marquee> | ||
~~~~~~~~~ | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertAnnotated({ | ||
message: getFailureMessage('marquee'), | ||
ruleName, | ||
source | ||
}); | ||
}); | ||
|
||
it('should fail when distracting element blink is used', () => { | ||
const source = ` | ||
@Component({ | ||
template: \` | ||
<blink></blink> | ||
~~~~~~~ | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertAnnotated({ | ||
message: getFailureMessage('blink'), | ||
ruleName, | ||
source | ||
}); | ||
}); | ||
}); | ||
|
||
describe('success', () => { | ||
it('should work when distracting element is not used', () => { | ||
const source = ` | ||
@Component({ | ||
template: \` | ||
<div>Valid</div> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertSuccess(ruleName, source); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I see this link, I'm not sure that we have to test 'blink'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its fine, since all the accessibility checkers and linters check for it. Sometimes developers use code generated or from old references.