refactor: move @babel/helper-annotate-as-pure to ts (#12415)

* refactor: move @babel/helper-annotate-as-pure to ts

* chore: add flow types
This commit is contained in:
Huáng Jùnliàng 2020-12-01 11:23:23 -05:00 committed by GitHub
parent 5e4b85ab1c
commit 7000ae04a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -49,3 +49,13 @@ declare module "@babel/helper-get-function-arity" {
node: BabelNodeFunction node: BabelNodeFunction
): number; ): number;
} }
declare module "@babel/helper-annotate-as-pure" {
declare export default function annotateAsPure(
pathOrNode:
| BabelNode
| {
node: BabelNode,
}
): void;
}

View File

@ -1,13 +1,16 @@
import * as t from "@babel/types"; import * as t from "@babel/types";
import type { Node } from "@babel/types";
const PURE_ANNOTATION = "#__PURE__"; const PURE_ANNOTATION = "#__PURE__";
const isPureAnnotated = ({ leadingComments }) => const isPureAnnotated = ({ leadingComments }: Node): boolean =>
!!leadingComments && !!leadingComments &&
leadingComments.some(comment => /[@#]__PURE__/.test(comment.value)); leadingComments.some(comment => /[@#]__PURE__/.test(comment.value));
export default function annotateAsPure(pathOrNode) { export default function annotateAsPure(
const node = pathOrNode.node || pathOrNode; pathOrNode: Node | { node: Node },
): void {
const node = pathOrNode["node"] || pathOrNode;
if (isPureAnnotated(node)) { if (isPureAnnotated(node)) {
return; return;
} }