yuku-parser 0.5.39 → 0.5.42
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/README.md +0 -15
- package/decode.js +20 -52
- package/index.d.ts +0 -12
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -58,19 +58,6 @@ sourceTypeFromPath("foo.cjs"); // "script"
|
|
|
58
58
|
sourceTypeFromPath("foo.mjs"); // "module"
|
|
59
59
|
```
|
|
60
60
|
|
|
61
|
-
## Resolving offsets to `(line, column)`
|
|
62
|
-
|
|
63
|
-
Nodes and diagnostics carry `start`/`end` offsets (UTF-16 code units). To turn an offset into a `{ line, column }` pair, call `locOf` on the parse result:
|
|
64
|
-
|
|
65
|
-
```ts
|
|
66
|
-
import { parse } from "yuku-parser";
|
|
67
|
-
|
|
68
|
-
const result = parse(source);
|
|
69
|
-
const { line, column } = result.locOf(result.program.body[0].start);
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
Lines are 1-based and columns are 0-based, matching ESTree's `loc` convention. The lookup is an O(log n) binary search: tens of nanoseconds per call, safe to invoke per node during a walk.
|
|
73
|
-
|
|
74
61
|
## Walking the AST
|
|
75
62
|
|
|
76
63
|
A typed, mutating walker is built in:
|
|
@@ -149,8 +136,6 @@ interface ParseResult {
|
|
|
149
136
|
program: Program;
|
|
150
137
|
comments: Comment[]; // every comment in source order
|
|
151
138
|
diagnostics: Diagnostic[];
|
|
152
|
-
lineStarts: number[];
|
|
153
|
-
locOf(offset: number): { line: number; column: number };
|
|
154
139
|
}
|
|
155
140
|
```
|
|
156
141
|
|
package/decode.js
CHANGED
|
@@ -245,15 +245,14 @@ function decode(buffer, source) {
|
|
|
245
245
|
extraCount = _u32[1],
|
|
246
246
|
spLen = _u32[2];
|
|
247
247
|
const commentCount = _u32[4],
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
progIdx = _u32[8];
|
|
248
|
+
diagCount = _u32[6],
|
|
249
|
+
progIdx = _u32[7];
|
|
251
250
|
const attachedCommentCount = _u32[5];
|
|
252
|
-
const _flags = _u32[
|
|
251
|
+
const _flags = _u32[8];
|
|
253
252
|
const _isTs = !!(_flags & 1);
|
|
254
253
|
const _attached = !!(_flags & 2);
|
|
255
|
-
const _firstNa = _u32[
|
|
256
|
-
const _nodesOff =
|
|
254
|
+
const _firstNa = _u32[9];
|
|
255
|
+
const _nodesOff = 40;
|
|
257
256
|
const eOff = _nodesOff + nodeCount * 48;
|
|
258
257
|
const _extraBase = eOff >> 2;
|
|
259
258
|
const _spOff = eOff + extraCount * 4;
|
|
@@ -296,19 +295,20 @@ function decode(buffer, source) {
|
|
|
296
295
|
if (s === e) return "";
|
|
297
296
|
if (s >= _srcLen) return _poolDecode(s, e);
|
|
298
297
|
if (e <= _firstNa) return _src.slice(s, e);
|
|
299
|
-
|
|
300
|
-
return _src.slice(ss, pm[e - _firstNa]);
|
|
298
|
+
return _src.slice(s < _firstNa ? s : pm[s - _firstNa], pm[e - _firstNa]);
|
|
301
299
|
};
|
|
302
300
|
function nodeArr(s, len) {
|
|
303
|
-
const r =
|
|
304
|
-
|
|
301
|
+
const r = [];
|
|
302
|
+
const base = _extraBase + s;
|
|
303
|
+
for (let j = 0; j < len; j++) r.push(node(_u32[base + j]));
|
|
305
304
|
return r;
|
|
306
305
|
}
|
|
307
306
|
function nodeArrHoles(s, len) {
|
|
308
|
-
const r =
|
|
307
|
+
const r = [];
|
|
308
|
+
const base = _extraBase + s;
|
|
309
309
|
for (let j = 0; j < len; j++) {
|
|
310
|
-
const x = _u32[
|
|
311
|
-
r
|
|
310
|
+
const x = _u32[base + j];
|
|
311
|
+
r.push(x !== NULL ? node(x) : null);
|
|
312
312
|
}
|
|
313
313
|
return r;
|
|
314
314
|
}
|
|
@@ -348,11 +348,11 @@ function decode(buffer, source) {
|
|
|
348
348
|
return r;
|
|
349
349
|
}
|
|
350
350
|
function _decode(i) {
|
|
351
|
-
const
|
|
352
|
-
const
|
|
353
|
-
const
|
|
354
|
-
const
|
|
355
|
-
const
|
|
351
|
+
const b = (_nodesOff + i * 48) >> 2;
|
|
352
|
+
const h0 = _u32[b];
|
|
353
|
+
const tag = h0 & 255;
|
|
354
|
+
const flags = h0 >>> 16;
|
|
355
|
+
const f0 = _u32[b + 1] & 65535;
|
|
356
356
|
const f1 = _u32[b + 2], f2 = _u32[b + 3],
|
|
357
357
|
f3 = _u32[b + 4], f4 = _u32[b + 5],
|
|
358
358
|
f5 = _u32[b + 6], f6 = _u32[b + 7],
|
|
@@ -777,11 +777,7 @@ function decode(buffer, source) {
|
|
|
777
777
|
}
|
|
778
778
|
}
|
|
779
779
|
const node = _attached ? nodeWithComments : _decode;
|
|
780
|
-
const
|
|
781
|
-
function startOf(i) { return _p(_u32[_nodesU32 + i * 12 + 10]); }
|
|
782
|
-
function endOf(i) { return _p(_u32[_nodesU32 + i * 12 + 11]); }
|
|
783
|
-
const lsOff = _cOff + commentCount * 20;
|
|
784
|
-
const dOff = lsOff + lineStartsCount * 4;
|
|
780
|
+
const dOff = _cOff + commentCount * 20;
|
|
785
781
|
function _decodeComments() {
|
|
786
782
|
const out = Array.from({ length: commentCount });
|
|
787
783
|
for (let j = 0; j < commentCount; j++) {
|
|
@@ -800,20 +796,6 @@ function decode(buffer, source) {
|
|
|
800
796
|
}
|
|
801
797
|
return out;
|
|
802
798
|
}
|
|
803
|
-
function _decodeLineStarts() {
|
|
804
|
-
const out = Array.from({ length: lineStartsCount });
|
|
805
|
-
if (_firstNa >= _srcLen) {
|
|
806
|
-
for (let j = 0; j < lineStartsCount; j++) {
|
|
807
|
-
out[j] = dv.getUint32(lsOff + j * 4, true);
|
|
808
|
-
}
|
|
809
|
-
return out;
|
|
810
|
-
}
|
|
811
|
-
for (let j = 0; j < lineStartsCount; j++) {
|
|
812
|
-
const v = dv.getUint32(lsOff + j * 4, true);
|
|
813
|
-
out[j] = v < _firstNa ? v : (v >= _srcLen ? pm[pm.length - 1] : pm[v - _firstNa]);
|
|
814
|
-
}
|
|
815
|
-
return out;
|
|
816
|
-
}
|
|
817
799
|
function _decodeDiagnostics() {
|
|
818
800
|
const out = Array.from({ length: diagCount });
|
|
819
801
|
let dp = dOff;
|
|
@@ -846,11 +828,7 @@ function decode(buffer, source) {
|
|
|
846
828
|
}
|
|
847
829
|
return out;
|
|
848
830
|
}
|
|
849
|
-
let _program,
|
|
850
|
-
function _getLineStarts() {
|
|
851
|
-
if (_lineStarts === undefined) _lineStarts = _decodeLineStarts();
|
|
852
|
-
return _lineStarts;
|
|
853
|
-
}
|
|
831
|
+
let _program, _diagnostics, _comments;
|
|
854
832
|
return {
|
|
855
833
|
get program() {
|
|
856
834
|
return _program !== undefined ? _program : (_program = node(progIdx));
|
|
@@ -865,16 +843,6 @@ function decode(buffer, source) {
|
|
|
865
843
|
? _diagnostics
|
|
866
844
|
: (_diagnostics = _decodeDiagnostics());
|
|
867
845
|
},
|
|
868
|
-
get lineStarts() { return _getLineStarts(); },
|
|
869
|
-
locOf(offset) {
|
|
870
|
-
const ls = _getLineStarts();
|
|
871
|
-
let lo = 0, hi = ls.length;
|
|
872
|
-
while (lo < hi) {
|
|
873
|
-
const mid = (lo + hi) >>> 1;
|
|
874
|
-
if (ls[mid] <= offset) lo = mid + 1; else hi = mid;
|
|
875
|
-
}
|
|
876
|
-
return { line: lo, column: offset - ls[lo - 1] };
|
|
877
|
-
},
|
|
878
846
|
};
|
|
879
847
|
}
|
|
880
848
|
export { decode, CHILD_KEYS };
|
package/index.d.ts
CHANGED
|
@@ -10,7 +10,6 @@ import type {
|
|
|
10
10
|
NodeType,
|
|
11
11
|
Program,
|
|
12
12
|
SourceLang,
|
|
13
|
-
SourceLocation,
|
|
14
13
|
SourceType,
|
|
15
14
|
WalkContext,
|
|
16
15
|
} from "@yuku-toolchain/types";
|
|
@@ -70,17 +69,6 @@ interface ParseResult {
|
|
|
70
69
|
comments: Comment[];
|
|
71
70
|
/** Syntax diagnostics, and semantic diagnostics when {@link ParseOptions.semanticErrors} is enabled. */
|
|
72
71
|
diagnostics: Diagnostic[];
|
|
73
|
-
/**
|
|
74
|
-
* Sorted UTF-16 offsets where each line begins. Index `i` is the start of
|
|
75
|
-
* line `i + 1`. Used internally by {@link locOf}, and required by
|
|
76
|
-
* `yuku-codegen` for source maps.
|
|
77
|
-
*/
|
|
78
|
-
lineStarts: number[];
|
|
79
|
-
/**
|
|
80
|
-
* Resolves an offset to a `{ line, column }` pair. Lines are
|
|
81
|
-
* 1-based, columns are 0-based, matching ESTree's `loc` convention.
|
|
82
|
-
*/
|
|
83
|
-
locOf(offset: number): SourceLocation;
|
|
84
72
|
}
|
|
85
73
|
|
|
86
74
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yuku-parser",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.42",
|
|
4
4
|
"description": "High-performance JavaScript/TypeScript parser",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -18,20 +18,20 @@
|
|
|
18
18
|
"walk.js"
|
|
19
19
|
],
|
|
20
20
|
"optionalDependencies": {
|
|
21
|
-
"@yuku-parser/binding-linux-x64-gnu": "0.5.
|
|
22
|
-
"@yuku-parser/binding-linux-arm64-gnu": "0.5.
|
|
23
|
-
"@yuku-parser/binding-linux-arm-gnu": "0.5.
|
|
24
|
-
"@yuku-parser/binding-linux-x64-musl": "0.5.
|
|
25
|
-
"@yuku-parser/binding-linux-arm64-musl": "0.5.
|
|
26
|
-
"@yuku-parser/binding-linux-arm-musl": "0.5.
|
|
27
|
-
"@yuku-parser/binding-darwin-x64": "0.5.
|
|
28
|
-
"@yuku-parser/binding-darwin-arm64": "0.5.
|
|
29
|
-
"@yuku-parser/binding-win32-x64": "0.5.
|
|
30
|
-
"@yuku-parser/binding-win32-arm64": "0.5.
|
|
31
|
-
"@yuku-parser/binding-freebsd-x64": "0.5.
|
|
21
|
+
"@yuku-parser/binding-linux-x64-gnu": "0.5.42",
|
|
22
|
+
"@yuku-parser/binding-linux-arm64-gnu": "0.5.42",
|
|
23
|
+
"@yuku-parser/binding-linux-arm-gnu": "0.5.42",
|
|
24
|
+
"@yuku-parser/binding-linux-x64-musl": "0.5.42",
|
|
25
|
+
"@yuku-parser/binding-linux-arm64-musl": "0.5.42",
|
|
26
|
+
"@yuku-parser/binding-linux-arm-musl": "0.5.42",
|
|
27
|
+
"@yuku-parser/binding-darwin-x64": "0.5.42",
|
|
28
|
+
"@yuku-parser/binding-darwin-arm64": "0.5.42",
|
|
29
|
+
"@yuku-parser/binding-win32-x64": "0.5.42",
|
|
30
|
+
"@yuku-parser/binding-win32-arm64": "0.5.42",
|
|
31
|
+
"@yuku-parser/binding-freebsd-x64": "0.5.42"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@yuku-toolchain/types": "0.5.
|
|
34
|
+
"@yuku-toolchain/types": "0.5.41"
|
|
35
35
|
},
|
|
36
36
|
"keywords": [
|
|
37
37
|
"acorn",
|