-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! feat(core): update schematic to migrate to explicit query timing
Detect input setters with static query usage
- Loading branch information
1 parent
6ed43e1
commit ad88378
Showing
10 changed files
with
371 additions
and
57 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
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
88 changes: 88 additions & 0 deletions
88
packages/core/schematics/migrations/static-queries/angular/directive_inputs.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,88 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. 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 * as ts from 'typescript'; | ||
|
||
import {getPropertyNameText, hasPropertyNameText} from '../typescript/property_name'; | ||
import {getAngularDecorators} from './decorators'; | ||
|
||
/** Analyzes the given class and resolves the name of all inputs which are declared. */ | ||
export function getInputNamesOfClass( | ||
node: ts.ClassDeclaration, typeChecker: ts.TypeChecker): string[] { | ||
const resolvedInputSetters: string[] = []; | ||
|
||
// Determines the names of all inputs defined in the current class declaration by | ||
// checking whether a given property/getter/setter has the "@Input" decorator applied. | ||
node.members.forEach(m => { | ||
if (!m.decorators || !m.decorators.length || | ||
!ts.isPropertyDeclaration(m) && !ts.isSetAccessor(m) && !ts.isGetAccessor(m)) { | ||
return; | ||
} | ||
|
||
const inputDecorator = | ||
getAngularDecorators(typeChecker, m.decorators !).find(d => d.name === 'Input'); | ||
|
||
if (inputDecorator && hasPropertyNameText(m.name)) { | ||
resolvedInputSetters.push(m.name.text); | ||
} | ||
}); | ||
|
||
// Besides looking for immediate setters in the current class declaration, developers | ||
// can also define inputs in the directive metadata using the "inputs" property. We | ||
// also need to determine these inputs which are declared in the directive metadata. | ||
const metadataInputs = getInputNamesFromMetadata(node, typeChecker); | ||
|
||
if (metadataInputs) { | ||
resolvedInputSetters.push(...metadataInputs); | ||
} | ||
|
||
return resolvedInputSetters; | ||
} | ||
|
||
/** | ||
* Determines the names of all inputs declared in the directive/component metadata | ||
* of the given class. | ||
*/ | ||
function getInputNamesFromMetadata( | ||
node: ts.ClassDeclaration, typeChecker: ts.TypeChecker): string[]|null { | ||
if (!node.decorators || !node.decorators.length) { | ||
return null; | ||
} | ||
|
||
const decorator = getAngularDecorators(typeChecker, node.decorators) | ||
.find(d => d.name === 'Directive' || d.name === 'Component'); | ||
|
||
// In case no directive/component decorator could be found for this class, just | ||
// return null as there is no metadata where an input could be declared. | ||
if (!decorator) { | ||
return null; | ||
} | ||
|
||
const decoratorCall = decorator.node.expression as ts.CallExpression; | ||
|
||
// In case the decorator does define any metadata, there is no metadata | ||
// where inputs could be declared. This is an edge case because there | ||
// always needs to be an object literal, but in case there isn't we just | ||
// want to skip the invalid decorator and return null. | ||
if (!ts.isObjectLiteralExpression(decoratorCall.arguments[0])) { | ||
return null; | ||
} | ||
|
||
const metadata = decoratorCall.arguments[0] as ts.ObjectLiteralExpression; | ||
const inputs = metadata.properties.filter(ts.isPropertyAssignment) | ||
.find(p => getPropertyNameText(p.name) === 'inputs'); | ||
|
||
// In case there is no "inputs" property in the directive metadata, | ||
// just return "null" as no inputs can be declared for this class. | ||
if (!inputs || !ts.isArrayLiteralExpression(inputs.initializer)) { | ||
return null; | ||
} | ||
|
||
return inputs.initializer.elements.filter(ts.isStringLiteralLike) | ||
.map(element => element.text.split(':')[0].trim()); | ||
} |
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
Oops, something went wrong.