Skip to content

Commit

Permalink
refactor: cache type annotation in path.data
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed May 19, 2022
1 parent 53fd187 commit bc69fa8
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/babel-traverse/src/path/inference/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ import type * as t from "@babel/types";
*/

export function getTypeAnnotation(this: NodePath): t.FlowType {
if (this.typeAnnotation) return this.typeAnnotation;

let type = this._getTypeAnnotation() || anyTypeAnnotation();
let type = this.getData("typeAnnotation");
if (type != null) {
return type;
}
type = this._getTypeAnnotation() || anyTypeAnnotation();
if (isTypeAnnotation(type)) type = type.typeAnnotation;
return (this.typeAnnotation = type);
this.setData("typeAnnotation", type);
return type;
}

// Used to avoid infinite recursion in cases like
Expand Down Expand Up @@ -65,7 +68,9 @@ export function _getTypeAnnotation(this: NodePath): any {
}
}

// @ts-ignore
if (node.typeAnnotation) {
// @ts-ignore
return node.typeAnnotation;
}

Expand Down

0 comments on commit bc69fa8

Please sign in to comment.