forked from babel/babel
/
node.js
168 lines (149 loc) 路 4.58 KB
/
node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// @flow
import type Parser from "./index";
import UtilParser from "./util";
import { SourceLocation, type Position } from "../util/location";
import type { Comment, Node as NodeType, NodeBase } from "../types";
// Start an AST node, attaching a start offset.
class Node implements NodeBase {
constructor(parser: Parser, pos: number, loc: Position) {
this.start = pos;
this.end = 0;
this.loc = new SourceLocation(loc);
if (parser?.options.ranges) this.range = [pos, 0];
if (parser?.filename) this.loc.filename = parser.filename;
}
type: string = "";
declare start: number;
declare end: number;
declare loc: SourceLocation;
declare range: [number, number];
declare leadingComments: Array<Comment>;
declare trailingComments: Array<Comment>;
declare innerComments: Array<Comment>;
declare extra: { [key: string]: any };
}
const NodePrototype = Node.prototype;
if (!process.env.BABEL_8_BREAKING) {
// $FlowIgnore
NodePrototype.__clone = function (): Node {
// $FlowIgnore
const newNode: any = new Node();
const keys = Object.keys(this);
for (let i = 0, length = keys.length; i < length; i++) {
const key = keys[i];
// Do not clone comments that are already attached to the node
if (
key !== "leadingComments" &&
key !== "trailingComments" &&
key !== "innerComments"
) {
newNode[key] = this[key];
}
}
return newNode;
};
}
function clonePlaceholder(node: any): any {
return cloneIdentifier(node);
}
export function cloneIdentifier(node: any): any {
// We don't need to clone `typeAnnotations` and `optional`: because
// cloneIdentifier is only used in object shorthand and named import/export.
// Neither of them allow type annotations after the identifier or optional identifier
const { type, start, end, loc, range, extra, name } = node;
const cloned = Object.create(NodePrototype);
cloned.type = type;
cloned.start = start;
cloned.end = end;
cloned.loc = loc;
cloned.range = range;
cloned.extra = extra;
cloned.name = name;
if (type === "Placeholder") {
cloned.expectedNode = node.expectedNode;
}
return cloned;
}
export function cloneStringLiteral(node: any): any {
const { type, start, end, loc, range, extra } = node;
if (type === "Placeholder") {
return clonePlaceholder(node);
}
const cloned = Object.create(NodePrototype);
cloned.type = type;
cloned.start = start;
cloned.end = end;
cloned.loc = loc;
cloned.range = range;
if (node.raw !== undefined) {
// estree set node.raw instead of node.extra
cloned.raw = node.raw;
} else {
cloned.extra = extra;
}
cloned.value = node.value;
return cloned;
}
export class NodeUtils extends UtilParser {
startNode<T: NodeType>(): T {
// $FlowIgnore
return new Node(this, this.state.start, this.state.startLoc);
}
startNodeAt<T: NodeType>(pos: number, loc: Position): T {
// $FlowIgnore
return new Node(this, pos, loc);
}
/** Start a new node with a previous node's location. */
startNodeAtNode<T: NodeType>(type: NodeType): T {
return this.startNodeAt(type.start, type.loc.start);
}
// Finish an AST node, adding `type` and `end` properties.
finishNode<T: NodeType>(node: T, type: string): T {
return this.finishNodeAt(
node,
type,
this.state.lastTokEnd,
this.state.lastTokEndLoc,
);
}
// Finish node at given position
finishNodeAt<T: NodeType>(
node: T,
type: string,
pos: number,
loc: Position,
): T {
if (process.env.NODE_ENV !== "production" && node.end > 0) {
throw new Error(
"Do not call finishNode*() twice on the same node." +
" Instead use resetEndLocation() or change type directly.",
);
}
node.type = type;
node.end = pos;
node.loc.end = loc;
if (this.options.ranges) node.range[1] = pos;
if (this.options.attachComment) this.processComment(node);
return node;
}
resetStartLocation(node: NodeBase, start: number, startLoc: Position): void {
node.start = start;
node.loc.start = startLoc;
if (this.options.ranges) node.range[0] = start;
}
resetEndLocation(
node: NodeBase,
end?: number = this.state.lastTokEnd,
endLoc?: Position = this.state.lastTokEndLoc,
): void {
node.end = end;
node.loc.end = endLoc;
if (this.options.ranges) node.range[1] = end;
}
/**
* Reset the start location of node to the start location of locationNode
*/
resetStartLocationFromNode(node: NodeBase, locationNode: NodeBase): void {
this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
}
}