Skip to content

Commit a0271ef

Browse files
authored
Merge pull request microsoft#14177 from ajafff/parent-type
Add specific types to parent property of Node interfaces
2 parents 609008a + 6ad9fe1 commit a0271ef

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11584,7 +11584,7 @@ namespace ts {
1158411584
if (isBindingPattern(declaration.parent)) {
1158511585
const parentDeclaration = declaration.parent.parent;
1158611586
const name = declaration.propertyName || declaration.name;
11587-
if (isVariableLike(parentDeclaration) &&
11587+
if (parentDeclaration.kind !== SyntaxKind.BindingElement &&
1158811588
parentDeclaration.type &&
1158911589
!isBindingPattern(name)) {
1159011590
const text = getTextOfPropertyName(name);

src/compiler/types.ts

+38-2
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@
621621

622622
export interface TypeParameterDeclaration extends Declaration {
623623
kind: SyntaxKind.TypeParameter;
624+
parent?: DeclarationWithTypeParameters;
624625
name: Identifier;
625626
constraint?: TypeNode;
626627
default?: TypeNode;
@@ -648,19 +649,21 @@
648649

649650
export interface VariableDeclaration extends Declaration {
650651
kind: SyntaxKind.VariableDeclaration;
651-
parent?: VariableDeclarationList;
652+
parent?: VariableDeclarationList | CatchClause;
652653
name: BindingName; // Declared variable name
653654
type?: TypeNode; // Optional type annotation
654655
initializer?: Expression; // Optional initializer
655656
}
656657

657658
export interface VariableDeclarationList extends Node {
658659
kind: SyntaxKind.VariableDeclarationList;
660+
parent?: VariableStatement | ForStatement | ForOfStatement | ForInStatement;
659661
declarations: NodeArray<VariableDeclaration>;
660662
}
661663

662664
export interface ParameterDeclaration extends Declaration {
663665
kind: SyntaxKind.Parameter;
666+
parent?: SignatureDeclaration;
664667
dotDotDotToken?: DotDotDotToken; // Present on rest parameter
665668
name: BindingName; // Declared parameter name
666669
questionToken?: QuestionToken; // Present on optional parameter
@@ -670,6 +673,7 @@
670673

671674
export interface BindingElement extends Declaration {
672675
kind: SyntaxKind.BindingElement;
676+
parent?: BindingPattern;
673677
propertyName?: PropertyName; // Binding property name (in object binding pattern)
674678
dotDotDotToken?: DotDotDotToken; // Present on rest element (in object binding pattern)
675679
name: BindingName; // Declared binding element name
@@ -751,11 +755,13 @@
751755

752756
export interface ObjectBindingPattern extends Node {
753757
kind: SyntaxKind.ObjectBindingPattern;
758+
parent?: VariableDeclaration | ParameterDeclaration | BindingElement;
754759
elements: NodeArray<BindingElement>;
755760
}
756761

757762
export interface ArrayBindingPattern extends Node {
758763
kind: SyntaxKind.ArrayBindingPattern;
764+
parent?: VariableDeclaration | ParameterDeclaration | BindingElement;
759765
elements: NodeArray<ArrayBindingElement>;
760766
}
761767

@@ -1324,14 +1330,17 @@
13241330

13251331
export interface TemplateHead extends LiteralLikeNode {
13261332
kind: SyntaxKind.TemplateHead;
1333+
parent?: TemplateExpression;
13271334
}
13281335

13291336
export interface TemplateMiddle extends LiteralLikeNode {
13301337
kind: SyntaxKind.TemplateMiddle;
1338+
parent?: TemplateSpan;
13311339
}
13321340

13331341
export interface TemplateTail extends LiteralLikeNode {
13341342
kind: SyntaxKind.TemplateTail;
1343+
parent?: TemplateSpan;
13351344
}
13361345

13371346
export type TemplateLiteral = TemplateExpression | NoSubstitutionTemplateLiteral;
@@ -1346,6 +1355,7 @@
13461355
// The template literal must have kind TemplateMiddleLiteral or TemplateTailLiteral.
13471356
export interface TemplateSpan extends Node {
13481357
kind: SyntaxKind.TemplateSpan;
1358+
parent?: TemplateExpression;
13491359
expression: Expression;
13501360
literal: TemplateMiddle | TemplateTail;
13511361
}
@@ -1433,6 +1443,7 @@
14331443

14341444
export interface ExpressionWithTypeArguments extends TypeNode {
14351445
kind: SyntaxKind.ExpressionWithTypeArguments;
1446+
parent?: HeritageClause;
14361447
expression: LeftHandSideExpression;
14371448
typeArguments?: NodeArray<TypeNode>;
14381449
}
@@ -1500,6 +1511,7 @@
15001511
/// The opening element of a <Tag>...</Tag> JsxElement
15011512
export interface JsxOpeningElement extends Expression {
15021513
kind: SyntaxKind.JsxOpeningElement;
1514+
parent?: JsxElement;
15031515
tagName: JsxTagNameExpression;
15041516
attributes: JsxAttributes;
15051517
}
@@ -1513,29 +1525,34 @@
15131525

15141526
export interface JsxAttribute extends ObjectLiteralElement {
15151527
kind: SyntaxKind.JsxAttribute;
1528+
parent?: JsxOpeningLikeElement;
15161529
name: Identifier;
15171530
/// JSX attribute initializers are optional; <X y /> is sugar for <X y={true} />
15181531
initializer?: StringLiteral | JsxExpression;
15191532
}
15201533

15211534
export interface JsxSpreadAttribute extends ObjectLiteralElement {
15221535
kind: SyntaxKind.JsxSpreadAttribute;
1536+
parent?: JsxOpeningLikeElement;
15231537
expression: Expression;
15241538
}
15251539

15261540
export interface JsxClosingElement extends Node {
15271541
kind: SyntaxKind.JsxClosingElement;
1542+
parent?: JsxElement;
15281543
tagName: JsxTagNameExpression;
15291544
}
15301545

15311546
export interface JsxExpression extends Expression {
15321547
kind: SyntaxKind.JsxExpression;
1548+
parent?: JsxElement | JsxAttributeLike;
15331549
dotDotDotToken?: Token<SyntaxKind.DotDotDotToken>;
15341550
expression?: Expression;
15351551
}
15361552

15371553
export interface JsxText extends Node {
15381554
kind: SyntaxKind.JsxText;
1555+
parent?: JsxElement;
15391556
}
15401557

15411558
export type JsxChild = JsxText | JsxExpression | JsxElement | JsxSelfClosingElement;
@@ -1677,17 +1694,20 @@
16771694

16781695
export interface CaseBlock extends Node {
16791696
kind: SyntaxKind.CaseBlock;
1697+
parent?: SwitchStatement;
16801698
clauses: NodeArray<CaseOrDefaultClause>;
16811699
}
16821700

16831701
export interface CaseClause extends Node {
16841702
kind: SyntaxKind.CaseClause;
1703+
parent?: CaseBlock;
16851704
expression: Expression;
16861705
statements: NodeArray<Statement>;
16871706
}
16881707

16891708
export interface DefaultClause extends Node {
16901709
kind: SyntaxKind.DefaultClause;
1710+
parent?: CaseBlock;
16911711
statements: NodeArray<Statement>;
16921712
}
16931713

@@ -1713,6 +1733,7 @@
17131733

17141734
export interface CatchClause extends Node {
17151735
kind: SyntaxKind.CatchClause;
1736+
parent?: TryStatement;
17161737
variableDeclaration: VariableDeclaration;
17171738
block: Block;
17181739
}
@@ -1756,6 +1777,7 @@
17561777

17571778
export interface HeritageClause extends Node {
17581779
kind: SyntaxKind.HeritageClause;
1780+
parent?: InterfaceDeclaration | ClassDeclaration | ClassExpression;
17591781
token: SyntaxKind;
17601782
types?: NodeArray<ExpressionWithTypeArguments>;
17611783
}
@@ -1769,6 +1791,7 @@
17691791

17701792
export interface EnumMember extends Declaration {
17711793
kind: SyntaxKind.EnumMember;
1794+
parent?: EnumDeclaration;
17721795
// This does include ComputedPropertyName, but the parser will give an error
17731796
// if it parses a ComputedPropertyName in an EnumMember
17741797
name: PropertyName;
@@ -1787,7 +1810,8 @@
17871810

17881811
export interface ModuleDeclaration extends DeclarationStatement {
17891812
kind: SyntaxKind.ModuleDeclaration;
1790-
name: Identifier | StringLiteral;
1813+
parent?: ModuleBody | SourceFile;
1814+
name: ModuleName;
17911815
body?: ModuleBody | JSDocNamespaceDeclaration | Identifier;
17921816
}
17931817

@@ -1807,13 +1831,15 @@
18071831

18081832
export interface ModuleBlock extends Node, Statement {
18091833
kind: SyntaxKind.ModuleBlock;
1834+
parent?: ModuleDeclaration;
18101835
statements: NodeArray<Statement>;
18111836
}
18121837

18131838
export type ModuleReference = EntityName | ExternalModuleReference;
18141839

18151840
export interface ImportEqualsDeclaration extends DeclarationStatement {
18161841
kind: SyntaxKind.ImportEqualsDeclaration;
1842+
parent?: SourceFile | ModuleBlock;
18171843
name: Identifier;
18181844

18191845
// 'EntityName' for an internal module reference, 'ExternalModuleReference' for an external
@@ -1823,6 +1849,7 @@
18231849

18241850
export interface ExternalModuleReference extends Node {
18251851
kind: SyntaxKind.ExternalModuleReference;
1852+
parent?: ImportEqualsDeclaration;
18261853
expression?: Expression;
18271854
}
18281855

@@ -1832,6 +1859,7 @@
18321859
// ImportClause information is shown at its declaration below.
18331860
export interface ImportDeclaration extends Statement {
18341861
kind: SyntaxKind.ImportDeclaration;
1862+
parent?: SourceFile | ModuleBlock;
18351863
importClause?: ImportClause;
18361864
moduleSpecifier: Expression;
18371865
}
@@ -1846,12 +1874,14 @@
18461874
// import d, { a, b as x } from "mod" => name = d, namedBinding: NamedImports = { elements: [{ name: a }, { name: x, propertyName: b}]}
18471875
export interface ImportClause extends Declaration {
18481876
kind: SyntaxKind.ImportClause;
1877+
parent?: ImportDeclaration;
18491878
name?: Identifier; // Default binding
18501879
namedBindings?: NamedImportBindings;
18511880
}
18521881

18531882
export interface NamespaceImport extends Declaration {
18541883
kind: SyntaxKind.NamespaceImport;
1884+
parent?: ImportClause;
18551885
name: Identifier;
18561886
}
18571887

@@ -1863,30 +1893,35 @@
18631893

18641894
export interface ExportDeclaration extends DeclarationStatement {
18651895
kind: SyntaxKind.ExportDeclaration;
1896+
parent?: SourceFile | ModuleBlock;
18661897
exportClause?: NamedExports;
18671898
moduleSpecifier?: Expression;
18681899
}
18691900

18701901
export interface NamedImports extends Node {
18711902
kind: SyntaxKind.NamedImports;
1903+
parent?: ImportClause;
18721904
elements: NodeArray<ImportSpecifier>;
18731905
}
18741906

18751907
export interface NamedExports extends Node {
18761908
kind: SyntaxKind.NamedExports;
1909+
parent?: ExportDeclaration;
18771910
elements: NodeArray<ExportSpecifier>;
18781911
}
18791912

18801913
export type NamedImportsOrExports = NamedImports | NamedExports;
18811914

18821915
export interface ImportSpecifier extends Declaration {
18831916
kind: SyntaxKind.ImportSpecifier;
1917+
parent?: NamedImports;
18841918
propertyName?: Identifier; // Name preceding "as" keyword (or undefined when "as" is absent)
18851919
name: Identifier; // Declared name
18861920
}
18871921

18881922
export interface ExportSpecifier extends Declaration {
18891923
kind: SyntaxKind.ExportSpecifier;
1924+
parent?: NamedExports;
18901925
propertyName?: Identifier; // Name preceding "as" keyword (or undefined when "as" is absent)
18911926
name: Identifier; // Declared name
18921927
}
@@ -1895,6 +1930,7 @@
18951930

18961931
export interface ExportAssignment extends DeclarationStatement {
18971932
kind: SyntaxKind.ExportAssignment;
1933+
parent?: SourceFile;
18981934
isExportEquals?: boolean;
18991935
expression: Expression;
19001936
}

src/services/breakpoints.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,8 @@ namespace ts.BreakpointResolver {
370370
}
371371

372372
function textSpanFromVariableDeclaration(variableDeclaration: VariableDeclaration): TextSpan {
373-
const declarations = variableDeclaration.parent.declarations;
374-
if (declarations && declarations[0] === variableDeclaration) {
373+
if (variableDeclaration.parent.kind === SyntaxKind.VariableDeclarationList &&
374+
variableDeclaration.parent.declarations[0] === variableDeclaration) {
375375
// First declaration - include let keyword
376376
return textSpan(findPrecedingToken(variableDeclaration.pos, sourceFile, variableDeclaration.parent), variableDeclaration);
377377
}
@@ -400,8 +400,8 @@ namespace ts.BreakpointResolver {
400400
return textSpanFromVariableDeclaration(variableDeclaration);
401401
}
402402

403-
const declarations = variableDeclaration.parent.declarations;
404-
if (declarations && declarations[0] !== variableDeclaration) {
403+
if (variableDeclaration.parent.kind === SyntaxKind.VariableDeclarationList &&
404+
variableDeclaration.parent.declarations[0] !== variableDeclaration) {
405405
// If we cannot set breakpoint on this declaration, set it on previous one
406406
// Because the variable declaration may be binding pattern and
407407
// we would like to set breakpoint in last binding element if that's the case,

0 commit comments

Comments
 (0)