@swc/core 1.15.40 → 1.15.41

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.
Files changed (2) hide show
  1. package/index.js +56 -12
  2. package/package.json +13 -13
package/index.js CHANGED
@@ -91,6 +91,50 @@ const bindings = (() => {
91
91
  return binding;
92
92
  }
93
93
  })();
94
+ const programSourceContextKey = Symbol("@swc/core.programSourceContext");
95
+ function attachProgramSourceContext(program, sourceContext) {
96
+ if (!sourceContext || typeof program !== "object" || program === null) {
97
+ return program;
98
+ }
99
+ try {
100
+ Object.defineProperty(program, programSourceContextKey, {
101
+ configurable: true,
102
+ enumerable: false,
103
+ value: sourceContext,
104
+ writable: true,
105
+ });
106
+ }
107
+ catch (_) {
108
+ // Frozen user-created ASTs keep the legacy raw Program behavior.
109
+ }
110
+ return program;
111
+ }
112
+ function getProgramSourceContext(program) {
113
+ return program[programSourceContextKey];
114
+ }
115
+ function copyProgramSourceContext(from, to) {
116
+ return attachProgramSourceContext(to, getProgramSourceContext(from));
117
+ }
118
+ function parseProgramJson(json) {
119
+ const value = JSON.parse(json);
120
+ if (value &&
121
+ typeof value === "object" &&
122
+ "program" in value &&
123
+ value.program) {
124
+ return attachProgramSourceContext(value.program, value.sourceContext);
125
+ }
126
+ return value;
127
+ }
128
+ function stringifyProgram(program) {
129
+ const sourceContext = getProgramSourceContext(program);
130
+ if (sourceContext) {
131
+ return JSON.stringify({
132
+ program,
133
+ sourceContext,
134
+ });
135
+ }
136
+ return JSON.stringify(program);
137
+ }
94
138
  /**
95
139
  * Version of the swc binding.
96
140
  */
@@ -143,7 +187,7 @@ class Compiler {
143
187
  }
144
188
  if (bindings) {
145
189
  const res = yield bindings.parse(src, toBuffer(options), filename);
146
- return JSON.parse(res);
190
+ return parseProgramJson(res);
147
191
  }
148
192
  else if (fallbackBindings) {
149
193
  return fallbackBindings.parse(src, options);
@@ -155,7 +199,7 @@ class Compiler {
155
199
  options = options || { syntax: "ecmascript" };
156
200
  options.syntax = options.syntax || "ecmascript";
157
201
  if (bindings) {
158
- return JSON.parse(bindings.parseSync(src, toBuffer(options), filename));
202
+ return parseProgramJson(bindings.parseSync(src, toBuffer(options), filename));
159
203
  }
160
204
  else if (fallbackBindings) {
161
205
  return fallbackBindings.parseSync(src, options);
@@ -173,7 +217,7 @@ class Compiler {
173
217
  throw new Error("Bindings not found.");
174
218
  }
175
219
  const res = yield bindings.parseFile(path, toBuffer(options));
176
- return JSON.parse(res);
220
+ return parseProgramJson(res);
177
221
  });
178
222
  }
179
223
  parseFileSync(path, options) {
@@ -185,7 +229,7 @@ class Compiler {
185
229
  else if (!bindings) {
186
230
  throw new Error("Bindings not found.");
187
231
  }
188
- return JSON.parse(bindings.parseFileSync(path, toBuffer(options)));
232
+ return parseProgramJson(bindings.parseFileSync(path, toBuffer(options)));
189
233
  }
190
234
  /**
191
235
  * Note: this method should be invoked on the compiler instance used
@@ -195,7 +239,7 @@ class Compiler {
195
239
  return __awaiter(this, void 0, void 0, function* () {
196
240
  options = options || {};
197
241
  if (bindings) {
198
- return bindings.print(JSON.stringify(m), toBuffer(options));
242
+ return bindings.print(stringifyProgram(m), toBuffer(options));
199
243
  }
200
244
  else if (fallbackBindings) {
201
245
  return fallbackBindings.print(m, options);
@@ -210,7 +254,7 @@ class Compiler {
210
254
  printSync(m, options) {
211
255
  options = options || {};
212
256
  if (bindings) {
213
- return bindings.printSync(JSON.stringify(m), toBuffer(options));
257
+ return bindings.printSync(stringifyProgram(m), toBuffer(options));
214
258
  }
215
259
  else if (fallbackBindings) {
216
260
  return fallbackBindings.printSync(m, options);
@@ -232,9 +276,9 @@ class Compiler {
232
276
  const m = typeof src === "string"
233
277
  ? yield this.parse(src, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser, options.filename)
234
278
  : src;
235
- return this.transform(plugin(m), newOptions);
279
+ return this.transform(copyProgramSourceContext(m, plugin(m)), newOptions);
236
280
  }
237
- return bindings.transform(isModule ? JSON.stringify(src) : src, isModule, toBuffer(newOptions));
281
+ return bindings.transform(isModule ? stringifyProgram(src) : src, isModule, toBuffer(newOptions));
238
282
  }
239
283
  else if (fallbackBindings) {
240
284
  if (plugin && !this.fallbackBindingsPluginWarningDisplayed) {
@@ -260,9 +304,9 @@ class Compiler {
260
304
  const m = typeof src === "string"
261
305
  ? this.parseSync(src, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser, options.filename)
262
306
  : src;
263
- return this.transformSync(plugin(m), newOptions);
307
+ return this.transformSync(copyProgramSourceContext(m, plugin(m)), newOptions);
264
308
  }
265
- return bindings.transformSync(isModule ? JSON.stringify(src) : src, isModule, toBuffer(newOptions));
309
+ return bindings.transformSync(isModule ? stringifyProgram(src) : src, isModule, toBuffer(newOptions));
266
310
  }
267
311
  else if (fallbackBindings) {
268
312
  if (plugin && !this.fallbackBindingsPluginWarningDisplayed) {
@@ -291,7 +335,7 @@ class Compiler {
291
335
  newOptions.filename = path;
292
336
  if (plugin) {
293
337
  const m = yield this.parseFile(path, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser);
294
- return this.transform(plugin(m), newOptions);
338
+ return this.transform(copyProgramSourceContext(m, plugin(m)), newOptions);
295
339
  }
296
340
  return bindings.transformFile(path, false, toBuffer(newOptions));
297
341
  });
@@ -313,7 +357,7 @@ class Compiler {
313
357
  newOptions.filename = path;
314
358
  if (plugin) {
315
359
  const m = this.parseFileSync(path, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser);
316
- return this.transformSync(plugin(m), newOptions);
360
+ return this.transformSync(copyProgramSourceContext(m, plugin(m)), newOptions);
317
361
  }
318
362
  return bindings.transformFileSync(path,
319
363
  /* isModule */ false, toBuffer(newOptions));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swc/core",
3
- "version": "1.15.40",
3
+ "version": "1.15.41",
4
4
  "description": "Super-fast alternative for babel",
5
5
  "homepage": "https://swc.rs",
6
6
  "main": "./index.js",
@@ -92,18 +92,18 @@
92
92
  "typescript": "^5.3.3"
93
93
  },
94
94
  "optionalDependencies": {
95
- "@swc/core-darwin-x64": "1.15.40",
96
- "@swc/core-win32-x64-msvc": "1.15.40",
97
- "@swc/core-linux-x64-gnu": "1.15.40",
98
- "@swc/core-linux-ppc64-gnu": "1.15.40",
99
- "@swc/core-linux-s390x-gnu": "1.15.40",
100
- "@swc/core-linux-x64-musl": "1.15.40",
101
- "@swc/core-win32-ia32-msvc": "1.15.40",
102
- "@swc/core-linux-arm-gnueabihf": "1.15.40",
103
- "@swc/core-darwin-arm64": "1.15.40",
104
- "@swc/core-linux-arm64-gnu": "1.15.40",
105
- "@swc/core-linux-arm64-musl": "1.15.40",
106
- "@swc/core-win32-arm64-msvc": "1.15.40"
95
+ "@swc/core-darwin-x64": "1.15.41",
96
+ "@swc/core-win32-x64-msvc": "1.15.41",
97
+ "@swc/core-linux-x64-gnu": "1.15.41",
98
+ "@swc/core-linux-ppc64-gnu": "1.15.41",
99
+ "@swc/core-linux-s390x-gnu": "1.15.41",
100
+ "@swc/core-linux-x64-musl": "1.15.41",
101
+ "@swc/core-win32-ia32-msvc": "1.15.41",
102
+ "@swc/core-linux-arm-gnueabihf": "1.15.41",
103
+ "@swc/core-darwin-arm64": "1.15.41",
104
+ "@swc/core-linux-arm64-gnu": "1.15.41",
105
+ "@swc/core-linux-arm64-musl": "1.15.41",
106
+ "@swc/core-win32-arm64-msvc": "1.15.41"
107
107
  },
108
108
  "scripts": {
109
109
  "postinstall": "node postinstall.js",