refactor(compiler-cli): ensure `isNamed....()` helpers check name is identity (#38959)

Previously the `node.name` property was only checked to ensure it was
defined. But that meant that it was a `ts.BindingName`, which also includes
`ts.BindingPattern`, which we do not support. But these helper methods were
forcefully casting the value to `ts.Identifier.

Now we also check that the `node.name` is actually an `ts.Identifier`.

PR Close #38959
This commit is contained in:
Pete Bacon Darwin 2020-10-03 21:14:59 +01:00 committed by atscott
parent 2736a43ecb
commit 952710b43b
1 changed files with 7 additions and 3 deletions

View File

@ -11,15 +11,19 @@ import {ClassDeclaration} from './host';
export function isNamedClassDeclaration(node: ts.Node): export function isNamedClassDeclaration(node: ts.Node):
node is ClassDeclaration<ts.ClassDeclaration> { node is ClassDeclaration<ts.ClassDeclaration> {
return ts.isClassDeclaration(node) && (node.name !== undefined); return ts.isClassDeclaration(node) && isIdentifier(node.name);
} }
export function isNamedFunctionDeclaration(node: ts.Node): export function isNamedFunctionDeclaration(node: ts.Node):
node is ClassDeclaration<ts.FunctionDeclaration> { node is ClassDeclaration<ts.FunctionDeclaration> {
return ts.isFunctionDeclaration(node) && (node.name !== undefined); return ts.isFunctionDeclaration(node) && isIdentifier(node.name);
} }
export function isNamedVariableDeclaration(node: ts.Node): export function isNamedVariableDeclaration(node: ts.Node):
node is ClassDeclaration<ts.VariableDeclaration> { node is ClassDeclaration<ts.VariableDeclaration> {
return ts.isVariableDeclaration(node) && (node.name !== undefined); return ts.isVariableDeclaration(node) && isIdentifier(node.name);
}
function isIdentifier(node: ts.Node|undefined): node is ts.Identifier {
return node !== undefined && ts.isIdentifier(node);
} }