espolar 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "espolar",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "exports": {
@@ -11,7 +11,8 @@
11
11
  }
12
12
  },
13
13
  "files": [
14
- "dist"
14
+ "dist",
15
+ "src"
15
16
  ],
16
17
  "license": "MIT",
17
18
  "repository": {
package/src/api.ts ADDED
@@ -0,0 +1,87 @@
1
+ import type { Mapping } from "@volar/source-map";
2
+ import type { AST, AST_NODE_TYPES, Comment } from "./types.ts";
3
+ import type { SourceRange } from "./mappings.ts";
4
+
5
+ export interface PrintResult<Data> {
6
+ code: string;
7
+ mappings: Mapping<Data>[];
8
+ }
9
+
10
+ export interface PrintOptionsBase<Data> {
11
+ source: string;
12
+ isUntouched?: (node: AST.Node) => boolean | SourceRange;
13
+ combineMappingData?: (left: Data, right: Data) => Data;
14
+ printers?: Printers<Data>;
15
+ getLeadingComments?: (node: AST.Node) => Comment[] | undefined;
16
+ getTrailingComments?: (node: AST.Node) => Comment[] | undefined;
17
+ /**
18
+ * Provide additional source range for the left parenthesis of `CallExpression` and `NewExpression`.
19
+ * This is useful for language tools that want to provide signature hints when user enter `(`.
20
+ *
21
+ * @notes This hook will not interact with parentheses around the callee.
22
+ *
23
+ * @param node The `CallExpression` or `NewExpression` node.
24
+ * @returns The source range of the left parenthesis, `undefined` if not available.
25
+ */
26
+ experimentalGetLeftParenSourceRange?: (
27
+ node: AST.CallExpression | AST.NewExpression,
28
+ ) => SourceRange | undefined;
29
+ }
30
+
31
+ export interface MappingDataOptions<Data> {
32
+ getMappingData: (node?: AST.Node | null) => Data;
33
+ }
34
+
35
+ type MappingDataUndefinedOptions = {
36
+ [K in keyof MappingDataOptions<0>]?: undefined;
37
+ };
38
+
39
+ export type PrintOptions<Data> = PrintOptionsBase<Data> &
40
+ ([Data] extends [undefined]
41
+ ? MappingDataUndefinedOptions
42
+ : MappingDataOptions<Data>);
43
+
44
+ export interface PrinterContext<Data = any> {
45
+ readonly options: PrintOptions<Data>;
46
+ readonly source: string;
47
+ readonly generatedOffset: number;
48
+ write(text: string): void;
49
+ writeMapped(
50
+ text: string,
51
+ sourceStart: number,
52
+ sourceEnd: number,
53
+ data?: Data,
54
+ ): void;
55
+ writeNode(node: AST.Node | null | undefined): void;
56
+ writeNodeList(nodes: readonly (AST.Node | null)[], separator: string): void;
57
+ writeExpressionListWithCommaSep(
58
+ nodes: readonly (AST.Expression | AST.SpreadElement | null)[],
59
+ ): void;
60
+ writeNodeListWithNewLineSep(
61
+ nodes: readonly (AST.ProgramStatement | AST.ClassElement)[],
62
+ ): void;
63
+ writeSource(start: number, end: number, data?: Data): void;
64
+ writePreservedNode(node: AST.Node): void;
65
+ appendMapping(
66
+ sourceRange: SourceRange,
67
+ generatedStart: number,
68
+ generatedEnd: number,
69
+ data?: Data,
70
+ ): void;
71
+ /** Extra mappings that won't be merged automatically */
72
+ createExtraMapping(
73
+ sourceRange: SourceRange,
74
+ generatedStart: number,
75
+ generatedEnd: number,
76
+ data?: Data,
77
+ ): void;
78
+ }
79
+
80
+ export type NodePrinter<Key extends AST_NODE_TYPES, Data> = (
81
+ node: Extract<AST.Node, { type: Key }>,
82
+ context: PrinterContext<Data>,
83
+ ) => void;
84
+
85
+ export type Printers<Data> = {
86
+ [K in AST_NODE_TYPES]?: NodePrinter<K, Data>;
87
+ };
package/src/index.ts ADDED
@@ -0,0 +1,14 @@
1
+ export {
2
+ defaultCombineMappingData,
3
+ defaultIsUntouched,
4
+ print,
5
+ } from "./printer.ts";
6
+ export { defaultPrinters } from "./printers.ts";
7
+ export type {
8
+ NodePrinter,
9
+ PrintOptions,
10
+ PrintResult,
11
+ PrinterContext,
12
+ } from "./api.ts";
13
+ export type { AST, NodeLike, Comment } from "./types.ts";
14
+ export type { SourceRange } from "./mappings.ts";
@@ -0,0 +1,85 @@
1
+ import type { Mapping } from "@volar/source-map";
2
+ import type { AST } from "./types.ts";
3
+
4
+ export interface InternalMapping<Data = unknown> {
5
+ sourceStart: number;
6
+ sourceEnd: number;
7
+ generatedStart: number;
8
+ generatedEnd: number;
9
+ data: Data;
10
+ }
11
+
12
+ export interface SourceRange {
13
+ start: number;
14
+ end: number;
15
+ }
16
+
17
+ export function getNodeRange(node: AST.Node): SourceRange | undefined {
18
+ if (Array.isArray(node.range) && node.range.length === 2) {
19
+ const [start, end] = node.range;
20
+ if (Number.isInteger(start) && Number.isInteger(end) && start <= end) {
21
+ return { start, end };
22
+ }
23
+ }
24
+ const { start, end } = node;
25
+ if (
26
+ typeof start === "number" &&
27
+ typeof end === "number" &&
28
+ Number.isInteger(start) &&
29
+ Number.isInteger(end) &&
30
+ start <= end
31
+ ) {
32
+ return { start, end };
33
+ }
34
+
35
+ return undefined;
36
+ }
37
+
38
+ export function pushMapping<Data>(
39
+ mappings: InternalMapping<Data>[],
40
+ mapping: InternalMapping<Data>,
41
+ combineMappingData: (left: Data, right: Data) => Data,
42
+ ): void {
43
+ const previous = mappings.at(-1);
44
+ if (previous && canMerge(previous, mapping)) {
45
+ previous.sourceEnd = mapping.sourceEnd;
46
+ previous.generatedEnd = mapping.generatedEnd;
47
+ previous.data = combineMappingData(previous.data, mapping.data);
48
+ return;
49
+ }
50
+
51
+ mappings.push(mapping);
52
+ }
53
+
54
+ /**
55
+ * Two adjacent mappings with identical lengths can be merged into one mapping.
56
+ * @param left
57
+ * @param right
58
+ * @returns
59
+ */
60
+ function canMerge<Data>(
61
+ left: InternalMapping<Data>,
62
+ right: InternalMapping<Data>,
63
+ ): boolean {
64
+ return (
65
+ left.sourceEnd - left.sourceStart === left.generatedEnd - left.generatedStart &&
66
+ right.sourceEnd - right.sourceStart === right.generatedEnd - right.generatedStart &&
67
+ left.sourceEnd === right.sourceStart &&
68
+ left.generatedEnd === right.generatedStart
69
+ );
70
+ }
71
+
72
+ export function toVolarMapping<Data>(
73
+ mapping: InternalMapping<Data>,
74
+ ): Mapping<Data> {
75
+ const sourceLength = mapping.sourceEnd - mapping.sourceStart;
76
+ const generatedLength = mapping.generatedEnd - mapping.generatedStart;
77
+ return {
78
+ sourceOffsets: [mapping.sourceStart],
79
+ generatedOffsets: [mapping.generatedStart],
80
+ lengths: [sourceLength],
81
+ generatedLengths:
82
+ generatedLength !== sourceLength ? [generatedLength] : undefined,
83
+ data: mapping.data,
84
+ };
85
+ }
package/src/printer.ts ADDED
@@ -0,0 +1,301 @@
1
+ import type {
2
+ NodePrinter,
3
+ PrintOptions,
4
+ PrintResult,
5
+ PrinterContext,
6
+ Printers,
7
+ } from "./api.ts";
8
+ import {
9
+ defaultPrinters,
10
+ expectAssignmentExprNeedsParen,
11
+ writeComment,
12
+ } from "./printers.ts";
13
+ import type { AST, AST_NODE_TYPES, Comment, NodeLike } from "./types.ts";
14
+ import {
15
+ getNodeRange,
16
+ pushMapping,
17
+ toVolarMapping,
18
+ type InternalMapping,
19
+ type SourceRange,
20
+ } from "./mappings.ts";
21
+
22
+ interface InternalPrinterContext extends PrinterContext<any> {
23
+ // make typescript happy about complex types
24
+ options: any;
25
+ result(): PrintResult<any>;
26
+ }
27
+
28
+ export function print<Data = undefined>(
29
+ node: AST.Node,
30
+ options: PrintOptions<Data>,
31
+ ): PrintResult<Data>;
32
+ export function print<Data = undefined>(
33
+ node: import("estree").Node,
34
+ options: PrintOptions<Data>,
35
+ ): PrintResult<Data>;
36
+ export function print<Data = undefined>(
37
+ node: NodeLike,
38
+ options: PrintOptions<Data>,
39
+ ): PrintResult<Data>;
40
+ export function print<Data = undefined>(
41
+ node: unknown,
42
+ options: PrintOptions<Data>,
43
+ ): PrintResult<Data> {
44
+ const context = createPrinterContext(options);
45
+ context.writeNode(node as AST.Node);
46
+ return context.result();
47
+ }
48
+
49
+ export function defaultIsUntouched(node: AST.Node): boolean | SourceRange {
50
+ return getNodeRange(node) || false;
51
+ }
52
+
53
+ export function defaultCombineMappingData<T>(left: T, right: T): T {
54
+ if (left === right) {
55
+ return left;
56
+ }
57
+ throw new Error(
58
+ "Cannot combine mapping data with different values when no custom combineMappingData function is provided",
59
+ );
60
+ }
61
+
62
+ function createPrinterContext<Data>(
63
+ options: PrintOptions<Data>,
64
+ ): InternalPrinterContext {
65
+ const chunks: string[] = [];
66
+ const mappings: InternalMapping<Data>[] = [];
67
+ const extraMappings: InternalMapping<Data>[] = [];
68
+ let generatedOffset = 0;
69
+
70
+ const isUntouched = options.isUntouched ?? defaultIsUntouched;
71
+ const getMappingData = options.getMappingData ?? ((): any => undefined);
72
+ const combineMappingData =
73
+ options.combineMappingData ?? defaultCombineMappingData;
74
+ const printers: Printers<Data> = {
75
+ ...defaultPrinters,
76
+ ...options.printers,
77
+ };
78
+
79
+ const appendMapping = (
80
+ sourceRange: SourceRange,
81
+ generatedStart: number,
82
+ generatedEnd: number,
83
+ data: Data,
84
+ ) => {
85
+ pushMapping(
86
+ mappings,
87
+ {
88
+ sourceStart: sourceRange.start,
89
+ sourceEnd: sourceRange.end,
90
+ generatedStart,
91
+ generatedEnd,
92
+ data,
93
+ },
94
+ combineMappingData,
95
+ );
96
+ };
97
+
98
+ const context: InternalPrinterContext = {
99
+ options: options,
100
+ source: options.source,
101
+ get generatedOffset() {
102
+ return generatedOffset;
103
+ },
104
+ write(text) {
105
+ if (text.length === 0) {
106
+ return;
107
+ }
108
+ chunks.push(text);
109
+ generatedOffset += text.length;
110
+ },
111
+ writeMapped(text, sourceStart, sourceEnd, data) {
112
+ if (text.length === 0 || sourceEnd < sourceStart) {
113
+ return;
114
+ }
115
+ const generatedStart = generatedOffset;
116
+ context.write(text);
117
+ appendMapping(
118
+ { start: sourceStart, end: sourceEnd },
119
+ generatedStart,
120
+ generatedOffset,
121
+ data ?? getMappingData(null),
122
+ );
123
+ },
124
+ writeNode(node) {
125
+ if (!node) {
126
+ return;
127
+ }
128
+
129
+ const range = getNodeRange(node);
130
+ const untouchedRet = isUntouched(node);
131
+ if (untouchedRet) {
132
+ const sourceRange = untouchedRet === true ? range : untouchedRet;
133
+ if (!sourceRange) {
134
+ throw new Error(
135
+ `Node of type ${node.type} is marked as untouched but does not have valid source offsets`,
136
+ );
137
+ }
138
+ context.writeSource(
139
+ sourceRange.start,
140
+ sourceRange.end,
141
+ getMappingData(node),
142
+ );
143
+ return;
144
+ }
145
+
146
+ const printer = printers[node.type] as
147
+ | NodePrinter<AST_NODE_TYPES, Data>
148
+ | undefined;
149
+ if (!printer) {
150
+ throw new Error(`No printer registered for node type ${node.type}`);
151
+ }
152
+
153
+ const leadingComments = options.getLeadingComments?.(node);
154
+ if (leadingComments) {
155
+ for (const comment of leadingComments) {
156
+ writeComment(comment, context);
157
+ }
158
+ }
159
+
160
+ const generatedStart = generatedOffset;
161
+ printer(node, context);
162
+ const generatedEnd = generatedOffset;
163
+
164
+ const trailingComments = options.getTrailingComments?.(node);
165
+ if (trailingComments) {
166
+ for (const comment of trailingComments) {
167
+ writeComment(comment, context);
168
+ }
169
+ }
170
+ // If children nodes don't emit any mapping but the parent node itself
171
+ // can produce mapping, add that mapping
172
+ const lastMappingGeneratedEnd = mappings.at(-1)?.generatedEnd ?? 0;
173
+ if (range && lastMappingGeneratedEnd <= generatedStart) {
174
+ appendMapping(
175
+ range,
176
+ generatedStart,
177
+ generatedEnd,
178
+ getMappingData(node),
179
+ );
180
+ }
181
+ },
182
+ writeNodeList(nodes, separator) {
183
+ let needsSeparator = false;
184
+ for (const node of nodes) {
185
+ if (!node) {
186
+ if (needsSeparator) {
187
+ context.write(separator);
188
+ }
189
+ continue;
190
+ }
191
+
192
+ if (needsSeparator) {
193
+ context.write(separator);
194
+ }
195
+ context.writeNode(node);
196
+ needsSeparator = true;
197
+ }
198
+ },
199
+ writeExpressionListWithCommaSep(nodes) {
200
+ let needsSeparator = false;
201
+ for (const node of nodes) {
202
+ if (!node) {
203
+ if (needsSeparator) {
204
+ context.write(", ");
205
+ }
206
+ continue;
207
+ }
208
+
209
+ if (needsSeparator) {
210
+ context.write(", ");
211
+ }
212
+ const needsParens = expectAssignmentExprNeedsParen(node);
213
+ if (needsParens) {
214
+ context.write("(");
215
+ context.writeNode(node);
216
+ context.write(")");
217
+ } else {
218
+ context.writeNode(node);
219
+ }
220
+ needsSeparator = true;
221
+ }
222
+ },
223
+ writeNodeListWithNewLineSep(nodes) {
224
+ let lastRangeEnd: number | undefined;
225
+ let wroteNode = false;
226
+
227
+ for (const node of nodes) {
228
+ if (!node) {
229
+ continue;
230
+ }
231
+
232
+ const range = getNodeRange(node);
233
+ if (wroteNode) {
234
+ if (
235
+ lastRangeEnd !== undefined &&
236
+ range &&
237
+ range.start >= lastRangeEnd
238
+ ) {
239
+ context.writeSource(
240
+ lastRangeEnd,
241
+ range.start,
242
+ getMappingData(null),
243
+ );
244
+ } else {
245
+ context.write("\n");
246
+ }
247
+ }
248
+
249
+ context.writeNode(node);
250
+ wroteNode = true;
251
+
252
+ if (range) {
253
+ lastRangeEnd = range.end;
254
+ } else {
255
+ lastRangeEnd = undefined;
256
+ }
257
+ }
258
+ },
259
+ writeSource(start, end, data) {
260
+ if (end < start) {
261
+ return;
262
+ }
263
+
264
+ const generatedStart = generatedOffset;
265
+ context.write(options.source.slice(start, end));
266
+ appendMapping(
267
+ { start, end },
268
+ generatedStart,
269
+ generatedOffset,
270
+ data ?? getMappingData(null),
271
+ );
272
+ },
273
+ writePreservedNode(node) {
274
+ const range = getNodeRange(node);
275
+ if (!range) {
276
+ throw new Error(
277
+ `Cannot preserve node ${node.type} without source offsets`,
278
+ );
279
+ }
280
+ context.writeSource(range.start, range.end, getMappingData(node));
281
+ },
282
+ appendMapping,
283
+ createExtraMapping(sourceRange, generatedStart, generatedEnd, data) {
284
+ extraMappings.push({
285
+ sourceStart: sourceRange.start,
286
+ sourceEnd: sourceRange.end,
287
+ generatedStart,
288
+ generatedEnd,
289
+ data,
290
+ });
291
+ },
292
+ result() {
293
+ return {
294
+ code: chunks.join(""),
295
+ mappings: [...mappings, ...extraMappings].map(toVolarMapping),
296
+ };
297
+ },
298
+ };
299
+
300
+ return context;
301
+ }