oxc-parser 0.66.0 → 0.68.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.
@@ -1622,11 +1622,36 @@ function deserializeTSTypeAliasDeclaration(pos) {
1622
1622
  }
1623
1623
 
1624
1624
  function deserializeTSClassImplements(pos) {
1625
+ let expression = deserializeTSTypeName(pos + 8);
1626
+ if (expression.type === 'TSQualifiedName') {
1627
+ let parent = expression = {
1628
+ type: 'MemberExpression',
1629
+ start: expression.start,
1630
+ end: expression.end,
1631
+ object: expression.left,
1632
+ property: expression.right,
1633
+ computed: false,
1634
+ optional: false,
1635
+ };
1636
+
1637
+ while (parent.object.type === 'TSQualifiedName') {
1638
+ const object = parent.object;
1639
+ parent = parent.object = {
1640
+ type: 'MemberExpression',
1641
+ start: object.start,
1642
+ end: object.end,
1643
+ object: object.left,
1644
+ property: object.right,
1645
+ computed: false,
1646
+ optional: false,
1647
+ };
1648
+ }
1649
+ }
1625
1650
  return {
1626
1651
  type: 'TSClassImplements',
1627
1652
  start: deserializeU32(pos),
1628
1653
  end: deserializeU32(pos + 4),
1629
- expression: deserializeTSTypeName(pos + 8),
1654
+ expression,
1630
1655
  typeArguments: deserializeOptionBoxTSTypeParameterInstantiation(pos + 24),
1631
1656
  };
1632
1657
  }
@@ -1761,17 +1786,31 @@ function deserializeTSTypePredicate(pos) {
1761
1786
  }
1762
1787
 
1763
1788
  function deserializeTSModuleDeclaration(pos) {
1764
- const kind = deserializeTSModuleDeclarationKind(pos + 80);
1765
- return {
1766
- type: 'TSModuleDeclaration',
1767
- start: deserializeU32(pos),
1768
- end: deserializeU32(pos + 4),
1769
- id: deserializeTSModuleDeclarationName(pos + 8),
1770
- body: deserializeOptionTSModuleDeclarationBody(pos + 64),
1771
- kind,
1772
- declare: deserializeBool(pos + 81),
1773
- global: kind === 'global',
1774
- };
1789
+ const kind = deserializeTSModuleDeclarationKind(pos + 80),
1790
+ global = kind === 'global',
1791
+ start = deserializeU32(pos),
1792
+ end = deserializeU32(pos + 4),
1793
+ declare = deserializeBool(pos + 81);
1794
+ let id = deserializeTSModuleDeclarationName(pos + 8),
1795
+ body = deserializeOptionTSModuleDeclarationBody(pos + 64);
1796
+
1797
+ // Flatten `body`, and nest `id`
1798
+ if (body !== null && body.type === 'TSModuleDeclaration') {
1799
+ id = {
1800
+ type: 'TSQualifiedName',
1801
+ start: body.id.start,
1802
+ end: id.end,
1803
+ left: body.id,
1804
+ right: id,
1805
+ };
1806
+ body = Object.hasOwn(body, 'body') ? body.body : null;
1807
+ }
1808
+
1809
+ // Skip `body` field if `null`
1810
+ const node = body === null
1811
+ ? { type: 'TSModuleDeclaration', start, end, id, kind, declare, global }
1812
+ : { type: 'TSModuleDeclaration', start, end, id, body, kind, declare, global };
1813
+ return node;
1775
1814
  }
1776
1815
 
1777
1816
  function deserializeTSModuleBlock(pos) {
@@ -1814,12 +1853,22 @@ function deserializeTSTypeQuery(pos) {
1814
1853
  }
1815
1854
 
1816
1855
  function deserializeTSImportType(pos) {
1856
+ let options = deserializeOptionBoxObjectExpression(pos + 24);
1857
+ if (options !== null && options.properties.length === 1) {
1858
+ const prop = options.properties[0];
1859
+ if (
1860
+ !prop.method && !prop.shorthand && !prop.computed &&
1861
+ prop.key.type === 'Identifier' && prop.key.name === 'assert'
1862
+ ) {
1863
+ prop.key.name = 'with';
1864
+ }
1865
+ }
1817
1866
  return {
1818
1867
  type: 'TSImportType',
1819
1868
  start: deserializeU32(pos),
1820
1869
  end: deserializeU32(pos + 4),
1821
1870
  argument: deserializeTSType(pos + 8),
1822
- options: deserializeOptionBoxObjectExpression(pos + 24),
1871
+ options,
1823
1872
  qualifier: deserializeOptionTSTypeName(pos + 32),
1824
1873
  typeArguments: deserializeOptionBoxTSTypeParameterInstantiation(pos + 48),
1825
1874
  };
@@ -1859,8 +1908,8 @@ function deserializeTSMappedType(pos) {
1859
1908
  end: deserializeU32(pos + 4),
1860
1909
  nameType: deserializeOptionTSType(pos + 16),
1861
1910
  typeAnnotation: deserializeOptionTSType(pos + 32),
1862
- optional: deserializeTSMappedTypeModifierOperator(pos + 48),
1863
- readonly: deserializeTSMappedTypeModifierOperator(pos + 49),
1911
+ optional: deserializeOptionTSMappedTypeModifierOperator(pos + 48),
1912
+ readonly: deserializeOptionTSMappedTypeModifierOperator(pos + 49),
1864
1913
  key: typeParameter.name,
1865
1914
  constraint: typeParameter.constraint,
1866
1915
  };
@@ -3782,7 +3831,9 @@ function deserializeTSTupleElement(pos) {
3782
3831
  function deserializeTSTypeName(pos) {
3783
3832
  switch (uint8[pos]) {
3784
3833
  case 0:
3785
- return deserializeBoxIdentifierReference(pos + 8);
3834
+ let id = deserializeIdentifierReference(pos + 8);
3835
+ if (id.name === 'this') id = { type: 'ThisExpression', start: id.start, end: id.end };
3836
+ return id;
3786
3837
  case 1:
3787
3838
  return deserializeBoxTSQualifiedName(pos + 8);
3788
3839
  default:
@@ -3882,7 +3933,9 @@ function deserializeTSModuleDeclarationBody(pos) {
3882
3933
  function deserializeTSTypeQueryExprName(pos) {
3883
3934
  switch (uint8[pos]) {
3884
3935
  case 0:
3885
- return deserializeBoxIdentifierReference(pos + 8);
3936
+ let id = deserializeIdentifierReference(pos + 8);
3937
+ if (id.name === 'this') id = { type: 'ThisExpression', start: id.start, end: id.end };
3938
+ return id;
3886
3939
  case 1:
3887
3940
  return deserializeBoxTSQualifiedName(pos + 8);
3888
3941
  case 2:
@@ -3893,14 +3946,24 @@ function deserializeTSTypeQueryExprName(pos) {
3893
3946
  }
3894
3947
 
3895
3948
  function deserializeTSMappedTypeModifierOperator(pos) {
3896
- const operator = deserializeU8(pos);
3897
- return [true, '+', '-', null][operator];
3949
+ switch (uint8[pos]) {
3950
+ case 0:
3951
+ return true;
3952
+ case 1:
3953
+ return '+';
3954
+ case 2:
3955
+ return '-';
3956
+ default:
3957
+ throw new Error(`Unexpected discriminant ${uint8[pos]} for TSMappedTypeModifierOperator`);
3958
+ }
3898
3959
  }
3899
3960
 
3900
3961
  function deserializeTSModuleReference(pos) {
3901
3962
  switch (uint8[pos]) {
3902
3963
  case 0:
3903
- return deserializeBoxIdentifierReference(pos + 8);
3964
+ let id = deserializeIdentifierReference(pos + 8);
3965
+ if (id.name === 'this') id = { type: 'ThisExpression', start: id.start, end: id.end };
3966
+ return id;
3904
3967
  case 1:
3905
3968
  return deserializeBoxTSQualifiedName(pos + 8);
3906
3969
  case 2:
@@ -5515,6 +5578,11 @@ function deserializeOptionTSTypeName(pos) {
5515
5578
  return deserializeTSTypeName(pos);
5516
5579
  }
5517
5580
 
5581
+ function deserializeOptionTSMappedTypeModifierOperator(pos) {
5582
+ if (uint8[pos] === 3) return null;
5583
+ return deserializeTSMappedTypeModifierOperator(pos);
5584
+ }
5585
+
5518
5586
  function deserializeBoxTSExternalModuleReference(pos) {
5519
5587
  return deserializeTSExternalModuleReference(uint32[pos >> 2]);
5520
5588
  }
@@ -781,6 +781,8 @@ function deserializeObjectPattern(pos) {
781
781
  end: deserializeU32(pos + 4),
782
782
  properties,
783
783
  decorators: [],
784
+ optional: false,
785
+ typeAnnotation: null,
784
786
  };
785
787
  }
786
788
 
@@ -1772,11 +1774,36 @@ function deserializeTSTypeAliasDeclaration(pos) {
1772
1774
  }
1773
1775
 
1774
1776
  function deserializeTSClassImplements(pos) {
1777
+ let expression = deserializeTSTypeName(pos + 8);
1778
+ if (expression.type === 'TSQualifiedName') {
1779
+ let parent = expression = {
1780
+ type: 'MemberExpression',
1781
+ start: expression.start,
1782
+ end: expression.end,
1783
+ object: expression.left,
1784
+ property: expression.right,
1785
+ computed: false,
1786
+ optional: false,
1787
+ };
1788
+
1789
+ while (parent.object.type === 'TSQualifiedName') {
1790
+ const object = parent.object;
1791
+ parent = parent.object = {
1792
+ type: 'MemberExpression',
1793
+ start: object.start,
1794
+ end: object.end,
1795
+ object: object.left,
1796
+ property: object.right,
1797
+ computed: false,
1798
+ optional: false,
1799
+ };
1800
+ }
1801
+ }
1775
1802
  return {
1776
1803
  type: 'TSClassImplements',
1777
1804
  start: deserializeU32(pos),
1778
1805
  end: deserializeU32(pos + 4),
1779
- expression: deserializeTSTypeName(pos + 8),
1806
+ expression,
1780
1807
  typeArguments: deserializeOptionBoxTSTypeParameterInstantiation(pos + 24),
1781
1808
  };
1782
1809
  }
@@ -1911,17 +1938,31 @@ function deserializeTSTypePredicate(pos) {
1911
1938
  }
1912
1939
 
1913
1940
  function deserializeTSModuleDeclaration(pos) {
1914
- const kind = deserializeTSModuleDeclarationKind(pos + 80);
1915
- return {
1916
- type: 'TSModuleDeclaration',
1917
- start: deserializeU32(pos),
1918
- end: deserializeU32(pos + 4),
1919
- id: deserializeTSModuleDeclarationName(pos + 8),
1920
- body: deserializeOptionTSModuleDeclarationBody(pos + 64),
1921
- kind,
1922
- declare: deserializeBool(pos + 81),
1923
- global: kind === 'global',
1924
- };
1941
+ const kind = deserializeTSModuleDeclarationKind(pos + 80),
1942
+ global = kind === 'global',
1943
+ start = deserializeU32(pos),
1944
+ end = deserializeU32(pos + 4),
1945
+ declare = deserializeBool(pos + 81);
1946
+ let id = deserializeTSModuleDeclarationName(pos + 8),
1947
+ body = deserializeOptionTSModuleDeclarationBody(pos + 64);
1948
+
1949
+ // Flatten `body`, and nest `id`
1950
+ if (body !== null && body.type === 'TSModuleDeclaration') {
1951
+ id = {
1952
+ type: 'TSQualifiedName',
1953
+ start: body.id.start,
1954
+ end: id.end,
1955
+ left: body.id,
1956
+ right: id,
1957
+ };
1958
+ body = Object.hasOwn(body, 'body') ? body.body : null;
1959
+ }
1960
+
1961
+ // Skip `body` field if `null`
1962
+ const node = body === null
1963
+ ? { type: 'TSModuleDeclaration', start, end, id, kind, declare, global }
1964
+ : { type: 'TSModuleDeclaration', start, end, id, body, kind, declare, global };
1965
+ return node;
1925
1966
  }
1926
1967
 
1927
1968
  function deserializeTSModuleBlock(pos) {
@@ -1964,12 +2005,22 @@ function deserializeTSTypeQuery(pos) {
1964
2005
  }
1965
2006
 
1966
2007
  function deserializeTSImportType(pos) {
2008
+ let options = deserializeOptionBoxObjectExpression(pos + 24);
2009
+ if (options !== null && options.properties.length === 1) {
2010
+ const prop = options.properties[0];
2011
+ if (
2012
+ !prop.method && !prop.shorthand && !prop.computed &&
2013
+ prop.key.type === 'Identifier' && prop.key.name === 'assert'
2014
+ ) {
2015
+ prop.key.name = 'with';
2016
+ }
2017
+ }
1967
2018
  return {
1968
2019
  type: 'TSImportType',
1969
2020
  start: deserializeU32(pos),
1970
2021
  end: deserializeU32(pos + 4),
1971
2022
  argument: deserializeTSType(pos + 8),
1972
- options: deserializeOptionBoxObjectExpression(pos + 24),
2023
+ options,
1973
2024
  qualifier: deserializeOptionTSTypeName(pos + 32),
1974
2025
  typeArguments: deserializeOptionBoxTSTypeParameterInstantiation(pos + 48),
1975
2026
  };
@@ -2009,8 +2060,8 @@ function deserializeTSMappedType(pos) {
2009
2060
  end: deserializeU32(pos + 4),
2010
2061
  nameType: deserializeOptionTSType(pos + 16),
2011
2062
  typeAnnotation: deserializeOptionTSType(pos + 32),
2012
- optional: deserializeTSMappedTypeModifierOperator(pos + 48),
2013
- readonly: deserializeTSMappedTypeModifierOperator(pos + 49),
2063
+ optional: deserializeOptionTSMappedTypeModifierOperator(pos + 48),
2064
+ readonly: deserializeOptionTSMappedTypeModifierOperator(pos + 49),
2014
2065
  key: typeParameter.name,
2015
2066
  constraint: typeParameter.constraint,
2016
2067
  };
@@ -3932,7 +3983,9 @@ function deserializeTSTupleElement(pos) {
3932
3983
  function deserializeTSTypeName(pos) {
3933
3984
  switch (uint8[pos]) {
3934
3985
  case 0:
3935
- return deserializeBoxIdentifierReference(pos + 8);
3986
+ let id = deserializeIdentifierReference(pos + 8);
3987
+ if (id.name === 'this') id = { type: 'ThisExpression', start: id.start, end: id.end };
3988
+ return id;
3936
3989
  case 1:
3937
3990
  return deserializeBoxTSQualifiedName(pos + 8);
3938
3991
  default:
@@ -4032,7 +4085,9 @@ function deserializeTSModuleDeclarationBody(pos) {
4032
4085
  function deserializeTSTypeQueryExprName(pos) {
4033
4086
  switch (uint8[pos]) {
4034
4087
  case 0:
4035
- return deserializeBoxIdentifierReference(pos + 8);
4088
+ let id = deserializeIdentifierReference(pos + 8);
4089
+ if (id.name === 'this') id = { type: 'ThisExpression', start: id.start, end: id.end };
4090
+ return id;
4036
4091
  case 1:
4037
4092
  return deserializeBoxTSQualifiedName(pos + 8);
4038
4093
  case 2:
@@ -4043,14 +4098,24 @@ function deserializeTSTypeQueryExprName(pos) {
4043
4098
  }
4044
4099
 
4045
4100
  function deserializeTSMappedTypeModifierOperator(pos) {
4046
- const operator = deserializeU8(pos);
4047
- return [true, '+', '-', null][operator];
4101
+ switch (uint8[pos]) {
4102
+ case 0:
4103
+ return true;
4104
+ case 1:
4105
+ return '+';
4106
+ case 2:
4107
+ return '-';
4108
+ default:
4109
+ throw new Error(`Unexpected discriminant ${uint8[pos]} for TSMappedTypeModifierOperator`);
4110
+ }
4048
4111
  }
4049
4112
 
4050
4113
  function deserializeTSModuleReference(pos) {
4051
4114
  switch (uint8[pos]) {
4052
4115
  case 0:
4053
- return deserializeBoxIdentifierReference(pos + 8);
4116
+ let id = deserializeIdentifierReference(pos + 8);
4117
+ if (id.name === 'this') id = { type: 'ThisExpression', start: id.start, end: id.end };
4118
+ return id;
4054
4119
  case 1:
4055
4120
  return deserializeBoxTSQualifiedName(pos + 8);
4056
4121
  case 2:
@@ -5665,6 +5730,11 @@ function deserializeOptionTSTypeName(pos) {
5665
5730
  return deserializeTSTypeName(pos);
5666
5731
  }
5667
5732
 
5733
+ function deserializeOptionTSMappedTypeModifierOperator(pos) {
5734
+ if (uint8[pos] === 3) return null;
5735
+ return deserializeTSMappedTypeModifierOperator(pos);
5736
+ }
5737
+
5668
5738
  function deserializeBoxTSExternalModuleReference(pos) {
5669
5739
  return deserializeTSExternalModuleReference(uint32[pos >> 2]);
5670
5740
  }
package/index.js CHANGED
@@ -1,6 +1,4 @@
1
1
  const bindings = require('./bindings.js');
2
- const deserializeJS = require('./deserialize-js.js');
3
- const deserializeTS = require('./deserialize-ts.js');
4
2
  const { wrap } = require('./wrap.cjs');
5
3
 
6
4
  module.exports.ParseResult = bindings.ParseResult;
@@ -22,7 +20,7 @@ module.exports.parseSync = function parseSync(filename, sourceText, options) {
22
20
  return wrap(bindings.parseSync(filename, sourceText, options));
23
21
  };
24
22
 
25
- let buffer, encoder;
23
+ let buffer, encoder, deserializeJS, deserializeTS;
26
24
 
27
25
  function parseSyncRaw(filename, sourceText, options) {
28
26
  if (!rawTransferSupported()) {
@@ -62,9 +60,23 @@ function parseSyncRaw(filename, sourceText, options) {
62
60
  const astTypeFlagPos = 2147483636;
63
61
  let isJsAst = buffer[astTypeFlagPos] === 0;
64
62
 
65
- const data = isJsAst
66
- ? deserializeJS(buffer, sourceText, sourceByteLen)
67
- : deserializeTS(buffer, sourceText, sourceByteLen);
63
+ // Lazy load deserializer, and deserialize buffer to JS objects
64
+ let data;
65
+ if (isJsAst) {
66
+ if (!deserializeJS) deserializeJS = require('./generated/deserialize/js.js');
67
+ data = deserializeJS(buffer, sourceText, sourceByteLen);
68
+
69
+ // Add a line comment for hashbang
70
+ const { hashbang } = data.program;
71
+ if (hashbang !== null) {
72
+ data.comments.unshift({ type: 'Line', value: hashbang.value, start: hashbang.start, end: hashbang.end });
73
+ }
74
+ } else {
75
+ if (!deserializeTS) deserializeTS = require('./generated/deserialize/ts.js');
76
+ data = deserializeTS(buffer, sourceText, sourceByteLen);
77
+ // Note: Do not add line comment for hashbang, to match `@typescript-eslint/parser`.
78
+ // See https://github.com/oxc-project/oxc/blob/ea784f5f082e4c53c98afde9bf983afd0b95e44e/napi/parser/src/lib.rs#L106-L130
79
+ }
68
80
 
69
81
  return {
70
82
  get program() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oxc-parser",
3
- "version": "0.66.0",
3
+ "version": "0.68.0",
4
4
  "main": "index.js",
5
5
  "browser": "wasm.mjs",
6
6
  "engines": {
@@ -31,15 +31,15 @@
31
31
  "wasm.mjs",
32
32
  "bindings.js",
33
33
  "webcontainer-fallback.js",
34
- "deserialize-js.js",
35
- "deserialize-ts.js"
34
+ "generated/deserialize/js.js",
35
+ "generated/deserialize/ts.js"
36
36
  ],
37
37
  "publishConfig": {
38
38
  "registry": "https://registry.npmjs.org/",
39
39
  "access": "public"
40
40
  },
41
41
  "dependencies": {
42
- "@oxc-project/types": "^0.66.0"
42
+ "@oxc-project/types": "^0.68.0"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@codspeed/vitest-plugin": "^4.0.0",
@@ -72,16 +72,16 @@
72
72
  "dtsHeaderFile": "header.js"
73
73
  },
74
74
  "optionalDependencies": {
75
- "@oxc-parser/binding-win32-x64-msvc": "0.66.0",
76
- "@oxc-parser/binding-win32-arm64-msvc": "0.66.0",
77
- "@oxc-parser/binding-linux-x64-gnu": "0.66.0",
78
- "@oxc-parser/binding-linux-x64-musl": "0.66.0",
79
- "@oxc-parser/binding-linux-arm64-gnu": "0.66.0",
80
- "@oxc-parser/binding-linux-arm64-musl": "0.66.0",
81
- "@oxc-parser/binding-linux-arm-gnueabihf": "0.66.0",
82
- "@oxc-parser/binding-darwin-x64": "0.66.0",
83
- "@oxc-parser/binding-darwin-arm64": "0.66.0",
84
- "@oxc-parser/binding-wasm32-wasi": "0.66.0"
75
+ "@oxc-parser/binding-win32-x64-msvc": "0.68.0",
76
+ "@oxc-parser/binding-win32-arm64-msvc": "0.68.0",
77
+ "@oxc-parser/binding-linux-x64-gnu": "0.68.0",
78
+ "@oxc-parser/binding-linux-x64-musl": "0.68.0",
79
+ "@oxc-parser/binding-linux-arm64-gnu": "0.68.0",
80
+ "@oxc-parser/binding-linux-arm64-musl": "0.68.0",
81
+ "@oxc-parser/binding-linux-arm-gnueabihf": "0.68.0",
82
+ "@oxc-parser/binding-darwin-x64": "0.68.0",
83
+ "@oxc-parser/binding-darwin-arm64": "0.68.0",
84
+ "@oxc-parser/binding-wasm32-wasi": "0.68.0"
85
85
  },
86
86
  "scripts": {
87
87
  "build-dev": "napi build --no-dts-cache --platform --js bindings.js",
package/wrap.cjs CHANGED
@@ -1,29 +1,11 @@
1
+ // Note: This code is repeated in `wrap.mjs`.
2
+ // Any changes should be applied in that file too.
3
+
1
4
  module.exports.wrap = function wrap(result) {
2
5
  let program, module, comments, errors;
3
6
  return {
4
7
  get program() {
5
- if (!program) {
6
- // Note: This code is repeated in `crates/oxc-wasm/update-bindings.mjs` and `napi/parser/wrap.cjs`.
7
- // Any changes should be applied in those 2 scripts too.
8
- program = JSON.parse(result.program, function(key, value) {
9
- // Set `value` field of `Literal`s for `BigInt`s and `RegExp`s.
10
- // This is not possible to do on Rust side, as neither can be represented correctly in JSON.
11
- if (value === null && key === 'value' && Object.hasOwn(this, 'type') && this.type === 'Literal') {
12
- if (Object.hasOwn(this, 'bigint')) {
13
- return BigInt(this.bigint);
14
- }
15
- if (Object.hasOwn(this, 'regex')) {
16
- const { regex } = this;
17
- try {
18
- return RegExp(regex.pattern, regex.flags);
19
- } catch (_err) {
20
- // Invalid regexp, or valid regexp using syntax not supported by this version of NodeJS
21
- }
22
- }
23
- }
24
- return value;
25
- });
26
- }
8
+ if (!program) program = jsonParseAst(result.program);
27
9
  return program;
28
10
  },
29
11
  get module() {
@@ -40,3 +22,26 @@ module.exports.wrap = function wrap(result) {
40
22
  },
41
23
  };
42
24
  };
25
+
26
+ function jsonParseAst(program) {
27
+ return JSON.parse(program, transform);
28
+ }
29
+
30
+ function transform(key, value) {
31
+ // Set `value` field of `Literal`s for `BigInt`s and `RegExp`s.
32
+ // This is not possible to do on Rust side, as neither can be represented correctly in JSON.
33
+ if (value === null && key === 'value' && Object.hasOwn(this, 'type') && this.type === 'Literal') {
34
+ if (Object.hasOwn(this, 'bigint')) {
35
+ return BigInt(this.bigint);
36
+ }
37
+ if (Object.hasOwn(this, 'regex')) {
38
+ const { regex } = this;
39
+ try {
40
+ return RegExp(regex.pattern, regex.flags);
41
+ } catch (_err) {
42
+ // Invalid regexp, or valid regexp using syntax not supported by this version of NodeJS
43
+ }
44
+ }
45
+ }
46
+ return value;
47
+ }
package/wrap.mjs CHANGED
@@ -1,10 +1,11 @@
1
+ // Note: This code is repeated in `wrap.cjs`.
2
+ // Any changes should be applied in that file too.
3
+
1
4
  export function wrap(result) {
2
5
  let program, module, comments, errors;
3
6
  return {
4
7
  get program() {
5
- if (!program) {
6
- program = jsonParseAst(result.program);
7
- }
8
+ if (!program) program = jsonParseAst(result.program);
8
9
  return program;
9
10
  },
10
11
  get module() {
@@ -23,23 +24,25 @@ export function wrap(result) {
23
24
  }
24
25
 
25
26
  // Used by napi/playground/patch.mjs
26
- export function jsonParseAst(ast) {
27
- return JSON.parse(ast, function(key, value) {
28
- // Set `value` field of `Literal`s for `BigInt`s and `RegExp`s.
29
- // This is not possible to do on Rust side, as neither can be represented correctly in JSON.
30
- if (value === null && key === 'value' && Object.hasOwn(this, 'type') && this.type === 'Literal') {
31
- if (Object.hasOwn(this, 'bigint')) {
32
- return BigInt(this.bigint);
33
- }
34
- if (Object.hasOwn(this, 'regex')) {
35
- const { regex } = this;
36
- try {
37
- return RegExp(regex.pattern, regex.flags);
38
- } catch (_err) {
39
- // Invalid regexp, or valid regexp using syntax not supported by this version of NodeJS
40
- }
27
+ export function jsonParseAst(program) {
28
+ return JSON.parse(program, transform);
29
+ }
30
+
31
+ function transform(key, value) {
32
+ // Set `value` field of `Literal`s for `BigInt`s and `RegExp`s.
33
+ // This is not possible to do on Rust side, as neither can be represented correctly in JSON.
34
+ if (value === null && key === 'value' && Object.hasOwn(this, 'type') && this.type === 'Literal') {
35
+ if (Object.hasOwn(this, 'bigint')) {
36
+ return BigInt(this.bigint);
37
+ }
38
+ if (Object.hasOwn(this, 'regex')) {
39
+ const { regex } = this;
40
+ try {
41
+ return RegExp(regex.pattern, regex.flags);
42
+ } catch (_err) {
43
+ // Invalid regexp, or valid regexp using syntax not supported by this version of NodeJS
41
44
  }
42
45
  }
43
- return value;
44
- });
46
+ }
47
+ return value;
45
48
  }