Skip to content

refactor(schematics): cleanup property name rules #12820

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 0 additions & 60 deletions src/lib/schematics/update/rules/checkPropertyAccessMiscRule.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @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 {ProgramAwareRuleWalker, RuleFailure, Rules} from 'tslint';
import * as ts from 'typescript';
import {propertyNames} from '../../material/data/property-names';

/**
* Rule that walks through every property access expression and updates properties that have
* been changed in favor of a new name.
*/
export class Rule extends Rules.TypedRule {
applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[] {
return this.applyWithWalker(new Walker(sourceFile, this.getOptions(), program));
}
}

export class Walker extends ProgramAwareRuleWalker {

visitPropertyAccessExpression(node: ts.PropertyAccessExpression) {
const hostType = this.getTypeChecker().getTypeAtLocation(node.expression);
const typeName = hostType && hostType.symbol && hostType.symbol.getName();

propertyNames.forEach(data => {
if (node.name.text !== data.replace) {
return;
}

if (!data.whitelist || data.whitelist.classes.includes(typeName)) {
const replacement = this.createReplacement(node.name.getStart(),
node.name.getWidth(), data.replaceWith);
this.addFailureAtNode(node.name, `Found deprecated property ${red(data.replace)} which ` +
`has been renamed to "${green(data.replaceWith)}"`, replacement);
}
});

super.visitPropertyAccessExpression(node);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @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 {bold, green, red} from 'chalk';
import {ProgramAwareRuleWalker, RuleFailure, Rules} from 'tslint';
import * as ts from 'typescript';

/**
* Rule that walks through every property access expression and and reports to TSLint if
* a given property name is no longer existing but cannot be automatically migrated.
*/
export class Rule extends Rules.TypedRule {
applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[] {
return this.applyWithWalker(new Walker(sourceFile, this.getOptions(), program));
}
}

export class Walker extends ProgramAwareRuleWalker {

visitPropertyAccessExpression(node: ts.PropertyAccessExpression) {
const hostType = this.getTypeChecker().getTypeAtLocation(node.expression);
const typeName = hostType && hostType.symbol && hostType.symbol.getName();

if (typeName === 'MatListOption' && node.name.text === 'selectionChange') {
this.addFailureAtNode(node, `Found deprecated property "${red('selectionChange')}" of ` +
`class "${bold('MatListOption')}". Use the "${green('selectionChange')}" property on ` +
`the parent "${bold('MatSelectionList')}" instead.`);
}

if (typeName === 'MatDatepicker' && node.name.text === 'selectedChanged') {
this.addFailureAtNode(node, `Found deprecated property "${red('selectedChanged')}" of ` +
`class "${bold('MatDatepicker')}". Use the "${green('dateChange')}" or ` +
`"${green('dateInput')}" methods on "${bold('MatDatepickerInput')}" instead`);
}

super.visitPropertyAccessExpression(node);
}
}
65 changes: 0 additions & 65 deletions src/lib/schematics/update/rules/switchPropertyNamesRule.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/lib/schematics/update/test-cases/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('test cases', () => {
'v5/element-selectors',
'v5/input-names',
'v5/output-names',
'v5/property-names',
];

// Iterates through every test case directory and generates a jasmine test block that will
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Fake definitions because the property name rules can only determine the host type
* properly by using type checking.
*/

class CdkConnectedOverlay {
_deprecatedOrigin: any;
_deprecatedPositions: any;
_deprecatedOffsetX: any;
_deprecatedOffsetY: any;
_deprecatedWidth: any;
_deprecatedHeight: any;
_deprecatedMinWidth: any;
_deprecatedMinHeight: any;
_deprecatedBackdropClass: any;
_deprecatedScrollStrategy: any;
_deprecatedOpen: any;
_deprecatedHasBackdrop: any;
}

class MatSelect {
change: any;
onOpen: any;
onClose: any;
}

class MatRadioGroup {
align: any;
}

class MatSnackBarConfig {
extraClasses: any;
}

class CdkPortalOutlet {
_deprecatedPortal: any;
_deprecatedPortalHost: any;
}

class MatDrawer {
align: any;
onAlignChanged: any;
onOpen: any;
onClose: any;
}

/* Actual test case using the previously defined definitions. */

class A {
self = {me: this};

constructor(private a: CdkConnectedOverlay) {}

onAngularClick() {
this.a.origin = '1';
this.a.positions = '2';
this.a.offsetX = '3';
this.a.offsetY = '4';
this.a.height = '5';

console.log(this.a.width || 10);
console.log(this.a.minWidth || this.a.minHeight);

this.self.me.a.backdropClass = ['a', 'b', 'c'];

const x = ({test: true} || this.a);

if (this.isConnectedOverlay(x)) {
x.scrollStrategy = 'myScrollStrategy';
x.open = false;
x.hasBackdrop = true;
}
}

isConnectedOverlay(val: any): val is CdkConnectedOverlay {
return val instanceof CdkConnectedOverlay;
}
}

class B {
self = {me: this};
b: MatRadioGroup;

constructor(private a: MatSelect,
public c: MatSnackBarConfig,
protected d: CdkPortalOutlet,
private e: MatDrawer) {}

onClick() {
this.a.selectionChange.subscribe(() => console.log('On Change'));
this.a.openedChange.pipe(filter(isOpen => isOpen)).subscribe(() => console.log('On Open'));
this.a.openedChange.pipe(filter(isOpen => !isOpen)).subscribe(() => console.log('On Close'));

this.b.labelPosition = 'end';
this.c.panelClass = ['x', 'y', 'z'];
this.d.portal = this.d.portal = 'myNewPortal';

this.e.position = 'end';
this.e.onPositionChanged.subscribe(() => console.log('Align Changed'));
this.e.openedChange.pipe(filter(isOpen => isOpen)).subscribe(() => console.log('Open'));
this.e.openedChange.pipe(filter(isOpen => !isOpen)).subscribe(() => console.log('Close'));
}
}
Loading