-
Notifications
You must be signed in to change notification settings - Fork 6.8k
refactor(schematics): cleanup attribute selector rules #12507
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
Merged
josephperrott
merged 1 commit into
angular:master
from
devversion:refactor/schematics-cleanup-attribute-selectors
Aug 9, 2018
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 hidden or 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 was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
src/lib/schematics/update/rules/attribute-selectors/attributeSelectorsStringLiteralRule.ts
This file contains hidden or 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,55 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {green, red} from 'chalk'; | ||
import {Replacement, RuleFailure, Rules, RuleWalker} from 'tslint'; | ||
import { | ||
attributeSelectors, | ||
MaterialAttributeSelectorData, | ||
} from '../../material/data/attribute-selectors'; | ||
import {findAllSubstringIndices} from '../../typescript/literal'; | ||
import * as ts from 'typescript'; | ||
|
||
/** | ||
* Rule that walks through every string literal that is part of a call expression and | ||
* switches deprecated attribute selectors to the updated selector. | ||
*/ | ||
export class Rule extends Rules.AbstractRule { | ||
apply(sourceFile: ts.SourceFile): RuleFailure[] { | ||
return this.applyWithWalker(new Walker(sourceFile, this.getOptions())); | ||
} | ||
} | ||
|
||
class Walker extends RuleWalker { | ||
|
||
visitStringLiteral(literal: ts.StringLiteral) { | ||
if (literal.parent && literal.parent.kind !== ts.SyntaxKind.CallExpression) { | ||
return; | ||
} | ||
|
||
const literalText = literal.getFullText(); | ||
|
||
attributeSelectors.forEach(selector => { | ||
findAllSubstringIndices(literalText, selector.replace) | ||
.map(offset => literal.getStart() + offset) | ||
.map(start => new Replacement(start, selector.replace.length, selector.replaceWith)) | ||
.forEach(replacement => this._addFailureWithReplacement(literal, replacement, selector)); | ||
}); | ||
} | ||
|
||
/** Adds an attribute selector failure with the given replacement at the specified node. */ | ||
private _addFailureWithReplacement(node: ts.Node, replacement: Replacement, | ||
selector: MaterialAttributeSelectorData) { | ||
|
||
this.addFailureAtNode( | ||
node, | ||
`Found deprecated attribute selector "${red('[' + selector.replace + ']')}" which ` + | ||
`has been renamed to "${green('[' + selector.replaceWith + ']')}"`, | ||
replacement); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/lib/schematics/update/rules/attribute-selectors/attributeSelectorsStylesheetRule.ts
This file contains hidden or 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,78 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {green, red} from 'chalk'; | ||
import {sync as globSync} from 'glob'; | ||
import {IOptions, Replacement, RuleFailure, Rules} from 'tslint'; | ||
import {attributeSelectors} from '../../material/data/attribute-selectors'; | ||
import {ExternalResource} from '../../tslint/component-file'; | ||
import {ComponentWalker} from '../../tslint/component-walker'; | ||
import { | ||
addFailureAtReplacement, | ||
createExternalReplacementFailure, | ||
} from '../../tslint/rule-failures'; | ||
import {findAllSubstringIndices} from '../../typescript/literal'; | ||
import * as ts from 'typescript'; | ||
|
||
/** | ||
* Rule that walks through every stylesheet in the application and updates outdated | ||
* attribute selectors to the updated selector. | ||
*/ | ||
export class Rule extends Rules.AbstractRule { | ||
|
||
apply(sourceFile: ts.SourceFile): RuleFailure[] { | ||
return this.applyWithWalker(new Walker(sourceFile, this.getOptions())); | ||
} | ||
} | ||
|
||
export class Walker extends ComponentWalker { | ||
|
||
constructor(sourceFile: ts.SourceFile, options: IOptions) { | ||
// In some applications, developers will have global stylesheets that are not specified in any | ||
// Angular component. Therefore we glob up all css and scss files outside of node_modules and | ||
// dist and check them as well. | ||
const extraFiles = globSync('!(node_modules|dist)/**/*.+(css|scss)'); | ||
super(sourceFile, options, extraFiles); | ||
extraFiles.forEach(styleUrl => this._reportExternalStyle(styleUrl)); | ||
} | ||
|
||
visitInlineStylesheet(literal: ts.StringLiteral) { | ||
this._createReplacementsForContent(literal, literal.getText()) | ||
.forEach(data => addFailureAtReplacement(this, data.failureMessage, data.replacement)); | ||
} | ||
|
||
visitExternalStylesheet(node: ExternalResource) { | ||
this._createReplacementsForContent(node, node.getFullText()) | ||
.map(data => createExternalReplacementFailure(node, data.failureMessage, | ||
this.getRuleName(), data.replacement)) | ||
.forEach(failure => this.addFailure(failure)); | ||
} | ||
|
||
/** | ||
* Searches for outdated attribute selectors in the specified content and creates replacements | ||
* with the according messages that can be added to a rule failure. | ||
*/ | ||
private _createReplacementsForContent(node: ts.Node, stylesheetContent: string) { | ||
const replacements: {failureMessage: string, replacement: Replacement}[] = []; | ||
|
||
attributeSelectors.forEach(selector => { | ||
const currentSelector = `[${selector.replace}]`; | ||
const updatedSelector = `[${selector.replaceWith}]`; | ||
|
||
const failureMessage = `Found deprecated attribute selector "${red(currentSelector)}"` + | ||
` which has been renamed to "${green(updatedSelector)}"`; | ||
|
||
findAllSubstringIndices(stylesheetContent, currentSelector) | ||
.map(offset => node.getStart() + offset) | ||
.map(start => new Replacement(start, currentSelector.length, updatedSelector)) | ||
.forEach(replacement => replacements.push({replacement, failureMessage})); | ||
}); | ||
|
||
return replacements; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/lib/schematics/update/rules/attribute-selectors/attributeSelectorsTemplateRule.ts
This file contains hidden or 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,64 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {green, red} from 'chalk'; | ||
import {Replacement, RuleFailure, Rules} from 'tslint'; | ||
import * as ts from 'typescript'; | ||
import {attributeSelectors} from '../../material/data/attribute-selectors'; | ||
import {ExternalResource} from '../../tslint/component-file'; | ||
import {ComponentWalker} from '../../tslint/component-walker'; | ||
import { | ||
addFailureAtReplacement, | ||
createExternalReplacementFailure, | ||
} from '../../tslint/rule-failures'; | ||
import {findAllSubstringIndices} from '../../typescript/literal'; | ||
|
||
/** | ||
* Rule that walks through every component template and switches outdated attribute | ||
* selectors to the updated selector. | ||
*/ | ||
export class Rule extends Rules.AbstractRule { | ||
apply(sourceFile: ts.SourceFile): RuleFailure[] { | ||
return this.applyWithWalker(new Walker(sourceFile, this.getOptions())); | ||
} | ||
} | ||
|
||
export class Walker extends ComponentWalker { | ||
|
||
visitInlineTemplate(template: ts.StringLiteral) { | ||
this._createReplacementsForContent(template, template.getText()) | ||
.forEach(data => addFailureAtReplacement(this, data.failureMessage, data.replacement)); | ||
} | ||
|
||
visitExternalTemplate(template: ExternalResource) { | ||
this._createReplacementsForContent(template, template.getFullText()) | ||
.map(data => createExternalReplacementFailure(template, data.failureMessage, | ||
this.getRuleName(), data.replacement)) | ||
.forEach(failure => this.addFailure(failure)); | ||
} | ||
|
||
/** | ||
* Searches for outdated attribute selectors in the specified content and creates replacements | ||
* with the according messages that can be added to a rule failure. | ||
*/ | ||
private _createReplacementsForContent(node: ts.Node, templateContent: string) { | ||
const replacements: {failureMessage: string, replacement: Replacement}[] = []; | ||
|
||
attributeSelectors.forEach(selector => { | ||
const failureMessage = `Found deprecated attribute selector "[${red(selector.replace)}]"` + | ||
` which has been renamed to "[${green(selector.replaceWith)}]"`; | ||
|
||
findAllSubstringIndices(templateContent, selector.replace) | ||
.map(offset => node.getStart() + offset) | ||
.map(start => new Replacement(start, selector.replace.length, selector.replaceWith)) | ||
.forEach(replacement => replacements.push({replacement, failureMessage})); | ||
}); | ||
|
||
return replacements; | ||
} | ||
} |
This file contains hidden or 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
52 changes: 0 additions & 52 deletions
52
src/lib/schematics/update/rules/switchStringLiteralAttributeSelectorsRule.ts
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Probably in another PR, but we should also make these filenames consistent with the rest of the project (dashes instead of camelCase)
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.
That does unfortunately not work because TSLint expects the files to the be camel-cased in order to load them.