sh-ast 0.0.0 → 0.1.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/README.md +147 -0
- package/dist/analyze/ansi-c-escapes.d.ts +53 -0
- package/dist/analyze/ansi-c-escapes.d.ts.map +1 -0
- package/dist/analyze/ansi-c-escapes.js +255 -0
- package/dist/analyze/ansi-c-escapes.js.map +1 -0
- package/dist/analyze/decode-lit.d.ts +74 -0
- package/dist/analyze/decode-lit.d.ts.map +1 -0
- package/dist/analyze/decode-lit.js +114 -0
- package/dist/analyze/decode-lit.js.map +1 -0
- package/dist/analyze/enumerate-commands.d.ts +159 -0
- package/dist/analyze/enumerate-commands.d.ts.map +1 -0
- package/dist/analyze/enumerate-commands.js +390 -0
- package/dist/analyze/enumerate-commands.js.map +1 -0
- package/dist/analyze/index.d.ts +18 -0
- package/dist/analyze/index.d.ts.map +1 -0
- package/dist/analyze/index.js +27 -0
- package/dist/analyze/index.js.map +1 -0
- package/dist/analyze/node-helpers.d.ts +28 -0
- package/dist/analyze/node-helpers.d.ts.map +1 -0
- package/dist/analyze/node-helpers.js +37 -0
- package/dist/analyze/node-helpers.js.map +1 -0
- package/dist/analyze/resolve-word.d.ts +146 -0
- package/dist/analyze/resolve-word.d.ts.map +1 -0
- package/dist/analyze/resolve-word.js +202 -0
- package/dist/analyze/resolve-word.js.map +1 -0
- package/dist/analyze.d.ts +390 -0
- package/dist/deep-freeze.d.ts +14 -0
- package/dist/deep-freeze.d.ts.map +1 -0
- package/dist/deep-freeze.js +25 -0
- package/dist/deep-freeze.js.map +1 -0
- package/dist/errors.d.ts +115 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +106 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/normalize.d.ts +52 -0
- package/dist/normalize.d.ts.map +1 -0
- package/dist/normalize.js +260 -0
- package/dist/normalize.js.map +1 -0
- package/dist/parse.d.ts +45 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js +118 -0
- package/dist/parse.js.map +1 -0
- package/dist/sh-ast.d.ts +1270 -0
- package/dist/types.d.ts +70 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/visitor-keys.d.ts +21 -0
- package/dist/visitor-keys.d.ts.map +1 -0
- package/dist/visitor-keys.js +23 -0
- package/dist/visitor-keys.js.map +1 -0
- package/dist/walk.d.ts +18 -0
- package/dist/walk.d.ts.map +1 -0
- package/dist/walk.js +40 -0
- package/dist/walk.js.map +1 -0
- package/dist/wasm-instance.d.ts +13 -0
- package/dist/wasm-instance.d.ts.map +1 -0
- package/dist/wasm-instance.js +88 -0
- package/dist/wasm-instance.js.map +1 -0
- package/generated/child-type-schema.d.ts +14 -0
- package/generated/child-type-schema.js +188 -0
- package/generated/node-types.d.ts +958 -0
- package/generated/position-fields.d.ts +21 -0
- package/generated/position-fields.js +50 -0
- package/generated/visitor-keys.d.ts +11 -0
- package/generated/visitor-keys.js +50 -0
- package/package.json +93 -10
- package/shim/sh-ast.wasm +0 -0
- package/shim/wasm_exec.js +654 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { parseSync } from './parse.js';
|
|
2
|
+
export { ShAnalyzeMaxDepthError, ShBridgeError, ShBridgeInternalError, ShInvalidDialectError, ShParseError, } from './errors.js';
|
|
3
|
+
export type { ShParseErrorInfo } from './errors.js';
|
|
4
|
+
export type { ParseOptions, Position, ShellDialect, ShFile, ShNode } from './types.js';
|
|
5
|
+
export { visitorKeys } from './visitor-keys.js';
|
|
6
|
+
export { walk } from './walk.js';
|
|
7
|
+
/**
|
|
8
|
+
* Strongly-typed `ShNode` subtypes for every mvdan/sh node type — generated
|
|
9
|
+
* by `tools/gen-visitor-keys` alongside {@link visitorKeys} (see
|
|
10
|
+
* design/ARCHITECTURE.md, "The schema table is generated, not
|
|
11
|
+
* hand-written"). {@link ShNode}'s index signature remains the base
|
|
12
|
+
* contract; these are an additive, opt-in stronger typing for consumers
|
|
13
|
+
* that want e.g. `ShNodes.ShCallExprNode` instead of a generic `ShNode`.
|
|
14
|
+
*
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
export type * as ShNodes from '../generated/node-types.js';
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EACL,sBAAsB,EACtB,aAAa,EACb,qBAAqB,EACrB,qBAAqB,EACrB,YAAY,GACb,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACpD,YAAY,EAAE,YAAY,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACvF,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC;;;;;;;;;GASG;AACH,YAAY,KAAK,OAAO,MAAM,4BAA4B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { parseSync } from './parse.js';
|
|
2
|
+
export { ShAnalyzeMaxDepthError, ShBridgeError, ShBridgeInternalError, ShInvalidDialectError, ShParseError, } from './errors.js';
|
|
3
|
+
export { visitorKeys } from './visitor-keys.js';
|
|
4
|
+
export { walk } from './walk.js';
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EACL,sBAAsB,EACtB,aAAa,EACb,qBAAqB,EACrB,qBAAqB,EACrB,YAAY,GACb,MAAM,aAAa,CAAC;AAGrB,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { ShNode } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* A JSON value as produced by `JSON.parse`.
|
|
4
|
+
*
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
export type JsonValue = string | number | boolean | null | JsonValue[] | {
|
|
8
|
+
[key: string]: JsonValue;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Builds a byte-offset -> UTF-16-index lookup for `text`. mvdan/sh reports
|
|
12
|
+
* byte offsets; ESLint (and JavaScript strings generally) index by UTF-16
|
|
13
|
+
* code unit, so every position needs this conversion. Throws if asked to
|
|
14
|
+
* convert an offset that does not fall on a UTF-8 code-point boundary — an
|
|
15
|
+
* invariant every offset the parser emits must satisfy (see
|
|
16
|
+
* design/ARCHITECTURE.md's "Serialization contract").
|
|
17
|
+
*
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
export declare function makeByteToUtf16(text: string): (byteOffset: number) => number;
|
|
21
|
+
/**
|
|
22
|
+
* Converts a byte offset and the 1-based line mvdan/sh already computed for
|
|
23
|
+
* it into a 1-based, UTF-16-code-unit column — the same conversion
|
|
24
|
+
* `normalize` applies to every node's `loc.start`/`loc.end` below, extracted
|
|
25
|
+
* so a parse error's reported position (see `errors.ts`'s
|
|
26
|
+
* `ShParseErrorInfo`) can use the identical algorithm instead of the raw
|
|
27
|
+
* byte-counting column mvdan/sh itself reports. See design/ARCHITECTURE.md's
|
|
28
|
+
* "Byte→UTF-16 conversion".
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export declare function toUtf16Column(text: string, byteOffset: number, line: number): number;
|
|
33
|
+
/**
|
|
34
|
+
* Normalizes mvdan/sh's typedjson tree into the shape ESLint expects: every
|
|
35
|
+
* node gets `type`, `range` (UTF-16), `loc` (1-based, UTF-16 columns), and
|
|
36
|
+
* every other field copied over with its name lowercased. See
|
|
37
|
+
* design/ARCHITECTURE.md's "Normalized node shape".
|
|
38
|
+
*
|
|
39
|
+
* Three of mvdan/sh's struct types — `Slice`, `Replace`, `Expansion`, all
|
|
40
|
+
* reachable only via `ParamExp.Slice`/`.Repl`/`.Exp` — do not implement
|
|
41
|
+
* `syntax.Node` (no `Pos()`/`End()`), so typedjson never gives them a `Pos`/
|
|
42
|
+
* `End` pair, let alone a `Type` discriminator. They still hold real child
|
|
43
|
+
* nodes (`Slice.Offset`/`.Length`, `Replace.Orig`/`.With`,
|
|
44
|
+
* `Expansion.Word`), so dropping them would silently lose that subtree (see
|
|
45
|
+
* issue #13 — `${USER:-nobody}`'s `nobody` literal). This function
|
|
46
|
+
* synthesizes a `type`/`range`/`loc` for these from their own children's
|
|
47
|
+
* already-computed positions instead, via `walkSynthetic` below.
|
|
48
|
+
*
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
export declare function normalize(root: JsonValue, text: string): ShNode;
|
|
52
|
+
//# sourceMappingURL=normalize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../src/normalize.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAY,MAAM,EAAE,MAAM,YAAY,CAAC;AAkBnD;;;;GAIG;AACH,MAAM,MAAM,SAAS,GACnB,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,EAAE,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAoEhF;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC,UAAU,EAAE,MAAM,KAAK,MAAM,CAsB5E;AAkBD;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAIpF;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAsI/D"}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { deepFreeze } from './deep-freeze.js';
|
|
2
|
+
import { CHILD_TYPE_SCHEMA } from '../generated/child-type-schema.js';
|
|
3
|
+
import { POSITION_FIELDS } from '../generated/position-fields.js';
|
|
4
|
+
/**
|
|
5
|
+
* Frozen (including each per-type field record) so immutability of this
|
|
6
|
+
* shared, module-scoped table is a contract, not just a convention any
|
|
7
|
+
* importer happens to honor — mirrors {@link visitorKeys}'s freezing in
|
|
8
|
+
* `visitor-keys.ts`.
|
|
9
|
+
*/
|
|
10
|
+
const FROZEN_CHILD_TYPE_SCHEMA = deepFreeze(CHILD_TYPE_SCHEMA);
|
|
11
|
+
/**
|
|
12
|
+
* Frozen (including each per-type field-name array), same rationale as
|
|
13
|
+
* {@link FROZEN_CHILD_TYPE_SCHEMA} above.
|
|
14
|
+
*/
|
|
15
|
+
const FROZEN_POSITION_FIELDS = deepFreeze(POSITION_FIELDS);
|
|
16
|
+
/**
|
|
17
|
+
* `Pos`/`End` are synthesized top-level keys every raw node carries (see
|
|
18
|
+
* `shim/internal/nodeencode/encode.go`'s `posField`/`endField`) — never a
|
|
19
|
+
* literal Go struct field, so they're checked directly rather than through
|
|
20
|
+
* {@link FROZEN_POSITION_FIELDS} (which is keyed by node *type*, and these
|
|
21
|
+
* two keys are universal, not type-specific).
|
|
22
|
+
*/
|
|
23
|
+
const UNIVERSAL_POSITION_KEYS = new Set(['Pos', 'End']);
|
|
24
|
+
/**
|
|
25
|
+
* Reports whether `key` on a raw node of type `type` holds a position
|
|
26
|
+
* (`syntax.Pos`) rather than child/scalar data — driving the "drop this
|
|
27
|
+
* field" decision `buildFields` below makes, in place of the bare-name
|
|
28
|
+
* `POS_KEYS` denylist this replaced (see issue #8, "replace the bare-name
|
|
29
|
+
* POS_KEYS denylist with generated per-(type,field) position data").
|
|
30
|
+
*
|
|
31
|
+
* Scoping the check per (type, key) — via the generated
|
|
32
|
+
* {@link FROZEN_POSITION_FIELDS} table, itself derived from mvdan/sh's own
|
|
33
|
+
* struct definitions by `tools/gen-visitor-keys` — is what closes the
|
|
34
|
+
* collision class a single global bare-name set could never close: mvdan/sh
|
|
35
|
+
* reuses field names across unrelated struct shapes. `Do` is both
|
|
36
|
+
* `WhileClause.DoPos Pos` (a position) and `ForClause.Do []*Stmt` (the loop
|
|
37
|
+
* body — real data, issue #2); `Dollar` is both `ParamExp.Dollar Pos` and
|
|
38
|
+
* `SglQuoted.Dollar`/`DblQuoted.Dollar bool` (the `$'...'`/`$"..."` ANSI-C
|
|
39
|
+
* quoting flag — real data, issue #3); `Select` (`ForClause.Select bool`),
|
|
40
|
+
* `Until` (`WhileClause.Until bool`), and `Unsigned`
|
|
41
|
+
* (`ArithmExp`/`ArithmCmd.Unsigned bool`) were never positions in *any*
|
|
42
|
+
* mvdan/sh v3.13.1 struct at all, yet the old bare-name denylist dropped
|
|
43
|
+
* them unconditionally regardless of node type.
|
|
44
|
+
*/
|
|
45
|
+
function isPositionField(type, key) {
|
|
46
|
+
if (UNIVERSAL_POSITION_KEYS.has(key))
|
|
47
|
+
return true;
|
|
48
|
+
return (FROZEN_POSITION_FIELDS[type] ?? []).includes(key);
|
|
49
|
+
}
|
|
50
|
+
function isJsonObject(value) {
|
|
51
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
52
|
+
}
|
|
53
|
+
function isRawPos(value) {
|
|
54
|
+
if (!isJsonObject(value))
|
|
55
|
+
return false;
|
|
56
|
+
const keys = Object.keys(value);
|
|
57
|
+
return (keys.length === 3 &&
|
|
58
|
+
typeof value.Offset === 'number' &&
|
|
59
|
+
typeof value.Line === 'number' &&
|
|
60
|
+
typeof value.Col === 'number');
|
|
61
|
+
}
|
|
62
|
+
function isRawNode(value) {
|
|
63
|
+
return isJsonObject(value) && isRawPos(value.Pos) && isRawPos(value.End);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Builds a byte-offset -> UTF-16-index lookup for `text`. mvdan/sh reports
|
|
67
|
+
* byte offsets; ESLint (and JavaScript strings generally) index by UTF-16
|
|
68
|
+
* code unit, so every position needs this conversion. Throws if asked to
|
|
69
|
+
* convert an offset that does not fall on a UTF-8 code-point boundary — an
|
|
70
|
+
* invariant every offset the parser emits must satisfy (see
|
|
71
|
+
* design/ARCHITECTURE.md's "Serialization contract").
|
|
72
|
+
*
|
|
73
|
+
* @internal
|
|
74
|
+
*/
|
|
75
|
+
export function makeByteToUtf16(text) {
|
|
76
|
+
const map = new Map();
|
|
77
|
+
let byte = 0;
|
|
78
|
+
map.set(0, 0);
|
|
79
|
+
for (let i = 0; i < text.length;) {
|
|
80
|
+
const codePoint = text.codePointAt(i);
|
|
81
|
+
// codePointAt(i) is always defined for i < text.length; this guards the
|
|
82
|
+
// type only (strict null checks require it, not a reachable branch).
|
|
83
|
+
if (codePoint === undefined)
|
|
84
|
+
break;
|
|
85
|
+
const units = codePoint > 0xffff ? 2 : 1;
|
|
86
|
+
const bytes = codePoint <= 0x7f ? 1 : codePoint <= 0x7ff ? 2 : codePoint <= 0xffff ? 3 : 4;
|
|
87
|
+
byte += bytes;
|
|
88
|
+
i += units;
|
|
89
|
+
map.set(byte, i);
|
|
90
|
+
}
|
|
91
|
+
return (byteOffset) => {
|
|
92
|
+
const value = map.get(byteOffset);
|
|
93
|
+
if (value === undefined) {
|
|
94
|
+
throw new Error(`byte offset ${String(byteOffset)} is not a UTF-8 code point boundary`);
|
|
95
|
+
}
|
|
96
|
+
return value;
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* The UTF-16 index of the start of each 1-based source line (`lineStarts[0]`
|
|
101
|
+
* is line 1's start, always `0`).
|
|
102
|
+
*/
|
|
103
|
+
function computeLineStarts(text) {
|
|
104
|
+
const lineStarts = [0];
|
|
105
|
+
for (let i = 0; i < text.length; i++) {
|
|
106
|
+
if (text[i] === '\n')
|
|
107
|
+
lineStarts.push(i + 1);
|
|
108
|
+
}
|
|
109
|
+
return lineStarts;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Converts a byte offset and the 1-based line mvdan/sh already computed for
|
|
113
|
+
* it into a 1-based, UTF-16-code-unit column — the same conversion
|
|
114
|
+
* `normalize` applies to every node's `loc.start`/`loc.end` below, extracted
|
|
115
|
+
* so a parse error's reported position (see `errors.ts`'s
|
|
116
|
+
* `ShParseErrorInfo`) can use the identical algorithm instead of the raw
|
|
117
|
+
* byte-counting column mvdan/sh itself reports. See design/ARCHITECTURE.md's
|
|
118
|
+
* "Byte→UTF-16 conversion".
|
|
119
|
+
*
|
|
120
|
+
* @internal
|
|
121
|
+
*/
|
|
122
|
+
export function toUtf16Column(text, byteOffset, line) {
|
|
123
|
+
const index = makeByteToUtf16(text)(byteOffset);
|
|
124
|
+
const lineStart = computeLineStarts(text)[line - 1] ?? 0;
|
|
125
|
+
return index - lineStart + 1;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Normalizes mvdan/sh's typedjson tree into the shape ESLint expects: every
|
|
129
|
+
* node gets `type`, `range` (UTF-16), `loc` (1-based, UTF-16 columns), and
|
|
130
|
+
* every other field copied over with its name lowercased. See
|
|
131
|
+
* design/ARCHITECTURE.md's "Normalized node shape".
|
|
132
|
+
*
|
|
133
|
+
* Three of mvdan/sh's struct types — `Slice`, `Replace`, `Expansion`, all
|
|
134
|
+
* reachable only via `ParamExp.Slice`/`.Repl`/`.Exp` — do not implement
|
|
135
|
+
* `syntax.Node` (no `Pos()`/`End()`), so typedjson never gives them a `Pos`/
|
|
136
|
+
* `End` pair, let alone a `Type` discriminator. They still hold real child
|
|
137
|
+
* nodes (`Slice.Offset`/`.Length`, `Replace.Orig`/`.With`,
|
|
138
|
+
* `Expansion.Word`), so dropping them would silently lose that subtree (see
|
|
139
|
+
* issue #13 — `${USER:-nobody}`'s `nobody` literal). This function
|
|
140
|
+
* synthesizes a `type`/`range`/`loc` for these from their own children's
|
|
141
|
+
* already-computed positions instead, via `walkSynthetic` below.
|
|
142
|
+
*
|
|
143
|
+
* @internal
|
|
144
|
+
*/
|
|
145
|
+
export function normalize(root, text) {
|
|
146
|
+
const toUtf16 = makeByteToUtf16(text);
|
|
147
|
+
const lineStarts = computeLineStarts(text);
|
|
148
|
+
const toLoc = (pos) => {
|
|
149
|
+
const index = toUtf16(pos.Offset);
|
|
150
|
+
const lineStart = lineStarts[pos.Line - 1] ?? 0;
|
|
151
|
+
const column = index - lineStart + 1;
|
|
152
|
+
return { line: pos.Line, column, index };
|
|
153
|
+
};
|
|
154
|
+
const startOf = (node) => ({
|
|
155
|
+
index: node.range[0],
|
|
156
|
+
line: node.loc.start.line,
|
|
157
|
+
column: node.loc.start.column,
|
|
158
|
+
});
|
|
159
|
+
const endOf = (node) => ({
|
|
160
|
+
index: node.range[1],
|
|
161
|
+
line: node.loc.end.line,
|
|
162
|
+
column: node.loc.end.column,
|
|
163
|
+
});
|
|
164
|
+
const earlier = (a, b) => (a.index <= b.index ? a : b);
|
|
165
|
+
const later = (a, b) => (a.index >= b.index ? a : b);
|
|
166
|
+
/**
|
|
167
|
+
* Builds `node`'s own lowercased field map, recursing into schema-declared
|
|
168
|
+
* children (real or synthetic). `fallback` is the nearest enclosing *real*
|
|
169
|
+
* node's start/end — used only if a synthetic child ends up with no
|
|
170
|
+
* positioned children of its own to derive a span from (see
|
|
171
|
+
* `walkSynthetic`).
|
|
172
|
+
*/
|
|
173
|
+
function buildFields(node, type, fallback) {
|
|
174
|
+
const schema = FROZEN_CHILD_TYPE_SCHEMA[type] ?? {};
|
|
175
|
+
const out = {};
|
|
176
|
+
const children = [];
|
|
177
|
+
const resolve = (value, childType) => {
|
|
178
|
+
if (isRawNode(value)) {
|
|
179
|
+
const child = walkNode(value, childType);
|
|
180
|
+
children.push(child);
|
|
181
|
+
return child;
|
|
182
|
+
}
|
|
183
|
+
if (childType !== null && isJsonObject(value)) {
|
|
184
|
+
const child = walkSynthetic(value, childType, fallback);
|
|
185
|
+
children.push(child);
|
|
186
|
+
return child;
|
|
187
|
+
}
|
|
188
|
+
return value;
|
|
189
|
+
};
|
|
190
|
+
for (const [key, value] of Object.entries(node)) {
|
|
191
|
+
if (key === 'Type' || isPositionField(type, key))
|
|
192
|
+
continue;
|
|
193
|
+
const outKey = key.toLowerCase();
|
|
194
|
+
const childType = schema[key] ?? null;
|
|
195
|
+
if (Array.isArray(value)) {
|
|
196
|
+
out[outKey] = value.map((item) => resolve(item, childType));
|
|
197
|
+
}
|
|
198
|
+
else if (isRawNode(value) || (childType !== null && isJsonObject(value))) {
|
|
199
|
+
out[outKey] = resolve(value, childType);
|
|
200
|
+
}
|
|
201
|
+
else if (!isRawPos(value)) {
|
|
202
|
+
out[outKey] = value;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return { fields: out, children };
|
|
206
|
+
}
|
|
207
|
+
function walkNode(node, staticType) {
|
|
208
|
+
const rawType = node.Type;
|
|
209
|
+
const type = typeof rawType === 'string' ? rawType : staticType;
|
|
210
|
+
if (type === null) {
|
|
211
|
+
throw new Error(`node without resolvable type: ${JSON.stringify(node).slice(0, 120)}`);
|
|
212
|
+
}
|
|
213
|
+
if (!isRawPos(node.Pos) || !isRawPos(node.End)) {
|
|
214
|
+
throw new Error(`node of type ${type} is missing Pos/End`);
|
|
215
|
+
}
|
|
216
|
+
const start = toLoc(node.Pos);
|
|
217
|
+
const end = toLoc(node.End);
|
|
218
|
+
const { fields } = buildFields(node, type, { start, end });
|
|
219
|
+
return {
|
|
220
|
+
type,
|
|
221
|
+
range: [start.index, end.index],
|
|
222
|
+
loc: {
|
|
223
|
+
start: { line: start.line, column: start.column },
|
|
224
|
+
end: { line: end.line, column: end.column },
|
|
225
|
+
},
|
|
226
|
+
...fields,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Normalizes a struct-typed field's value that has no `Pos`/`End` of its
|
|
231
|
+
* own — one of mvdan/sh's three non-`Node` "auxiliary" structs (`Slice`,
|
|
232
|
+
* `Replace`, `Expansion`). Its `range`/`loc` are synthesized from the
|
|
233
|
+
* union of its own children's already-computed spans (the exact source
|
|
234
|
+
* span its children cover — e.g. a `Replace`'s span is `Orig`'s start
|
|
235
|
+
* through `With`'s end, which for `${var/foo/bar}` is exactly `foo/bar`).
|
|
236
|
+
* Falls back to the nearest enclosing real node's span only when there
|
|
237
|
+
* are no positioned children at all to derive one from — e.g.
|
|
238
|
+
* `Expansion.Word` is nil (omitted from typedjson's output entirely, via
|
|
239
|
+
* `omitempty`) for an empty default like `${a:-}`.
|
|
240
|
+
*/
|
|
241
|
+
function walkSynthetic(node, type, fallback) {
|
|
242
|
+
const { fields, children } = buildFields(node, type, fallback);
|
|
243
|
+
const start = children.length > 0 ? children.map(startOf).reduce(earlier) : fallback.start;
|
|
244
|
+
const end = children.length > 0 ? children.map(endOf).reduce(later) : fallback.end;
|
|
245
|
+
return {
|
|
246
|
+
type,
|
|
247
|
+
range: [start.index, end.index],
|
|
248
|
+
loc: {
|
|
249
|
+
start: { line: start.line, column: start.column },
|
|
250
|
+
end: { line: end.line, column: end.column },
|
|
251
|
+
},
|
|
252
|
+
...fields,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
if (!isRawNode(root)) {
|
|
256
|
+
throw new Error('normalize: root value is not a node (missing Pos/End)');
|
|
257
|
+
}
|
|
258
|
+
return walkNode(root, 'File');
|
|
259
|
+
}
|
|
260
|
+
//# sourceMappingURL=normalize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file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AC;AAChC,CAAC"}
|
package/dist/parse.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type JsonValue } from './normalize.js';
|
|
2
|
+
import type { ParseOptions, ShFile } from './types.js';
|
|
3
|
+
interface ParseErrorPayload {
|
|
4
|
+
message: string;
|
|
5
|
+
filename: string;
|
|
6
|
+
line: number;
|
|
7
|
+
column: number;
|
|
8
|
+
offset: number;
|
|
9
|
+
}
|
|
10
|
+
interface ErrorPayload {
|
|
11
|
+
message: string;
|
|
12
|
+
}
|
|
13
|
+
interface ResultEnvelope {
|
|
14
|
+
file?: JsonValue;
|
|
15
|
+
parseError?: ParseErrorPayload;
|
|
16
|
+
error?: ErrorPayload;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
export declare function isResultEnvelope(value: unknown): value is ResultEnvelope;
|
|
22
|
+
/**
|
|
23
|
+
* Synchronously parses shell source into a normalized AST.
|
|
24
|
+
*
|
|
25
|
+
* The first call instantiates the WASM shim synchronously (Node-only); the
|
|
26
|
+
* instance is reused across subsequent calls, so repeated calls do not
|
|
27
|
+
* re-instantiate (see design/ARCHITECTURE.md).
|
|
28
|
+
*
|
|
29
|
+
* Throws {@link ShParseError} on a shell syntax error, carrying the position
|
|
30
|
+
* mvdan/sh reported: `.line` is 1-based exactly as mvdan/sh reports it;
|
|
31
|
+
* `.column` is converted to UTF-16 code units so it agrees with
|
|
32
|
+
* {@link ShNode.loc}'s columns (mvdan/sh itself reports `column` as a byte
|
|
33
|
+
* count — see {@link ShParseErrorInfo.column}). That unconverted, raw
|
|
34
|
+
* position appears only inside `.message`, which is mvdan/sh's own
|
|
35
|
+
* formatted string, verbatim. Throws {@link ShInvalidDialectError} when
|
|
36
|
+
* {@link ParseOptions.dialect} is not a supported dialect. Throws
|
|
37
|
+
* {@link ShBridgeInternalError} for failures that should never happen given
|
|
38
|
+
* a correctly-behaving shim (malformed envelope, unexpected root node
|
|
39
|
+
* type, shim contract violations).
|
|
40
|
+
*
|
|
41
|
+
* @public
|
|
42
|
+
*/
|
|
43
|
+
export declare function parseSync(text: string, options?: ParseOptions): ShFile;
|
|
44
|
+
export {};
|
|
45
|
+
//# sourceMappingURL=parse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AACA,OAAO,EAA4B,KAAK,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE1E,OAAO,KAAK,EAAE,YAAY,EAAgB,MAAM,EAAE,MAAM,YAAY,CAAC;AAcrE,UAAU,iBAAiB;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,UAAU,YAAY;IACpB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,UAAU,cAAc;IACtB,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,UAAU,CAAC,EAAE,iBAAiB,CAAC;IAC/B,KAAK,CAAC,EAAE,YAAY,CAAC;CACtB;AAoCD;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,cAAc,CAKxE;AAcD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,MAAM,CAoCtE"}
|
package/dist/parse.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { ShBridgeInternalError, ShInvalidDialectError, ShParseError } from './errors.js';
|
|
2
|
+
import { normalize, toUtf16Column } from './normalize.js';
|
|
3
|
+
import { callParse } from './wasm-instance.js';
|
|
4
|
+
/**
|
|
5
|
+
* The full set of dialects {@link parseSync} accepts, in the order surfaced
|
|
6
|
+
* by {@link ShInvalidDialectError}'s message. Kept as a runtime value
|
|
7
|
+
* distinct from the {@link ShellDialect} union so `parseSync` can validate
|
|
8
|
+
* `options.dialect` before ever calling into the WASM shim.
|
|
9
|
+
*/
|
|
10
|
+
const SUPPORTED_SHELL_DIALECTS = ['bash', 'posix', 'mksh', 'bats', 'zsh'];
|
|
11
|
+
function isSupportedShellDialect(value) {
|
|
12
|
+
return SUPPORTED_SHELL_DIALECTS.includes(value);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Checks that `value` is a plain object — rejects arrays, `null`, wrapped
|
|
16
|
+
* primitives, class instances, and symbol-keyed objects. `isResultEnvelope`
|
|
17
|
+
* and its field-level helpers below all validate `unknown` input coming
|
|
18
|
+
* straight out of `JSON.parse`, so this is the full-rigor check, not just
|
|
19
|
+
* `typeof value === 'object'`.
|
|
20
|
+
*
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
function isPlainObject(value) {
|
|
24
|
+
return (typeof value === 'object' &&
|
|
25
|
+
value !== null &&
|
|
26
|
+
!Array.isArray(value) &&
|
|
27
|
+
Object.getPrototypeOf(value) === Object.prototype &&
|
|
28
|
+
Object.getOwnPropertySymbols(value).length === 0);
|
|
29
|
+
}
|
|
30
|
+
function isParseErrorPayload(value) {
|
|
31
|
+
return (isPlainObject(value) &&
|
|
32
|
+
typeof value.message === 'string' &&
|
|
33
|
+
typeof value.filename === 'string' &&
|
|
34
|
+
typeof value.line === 'number' &&
|
|
35
|
+
typeof value.column === 'number' &&
|
|
36
|
+
typeof value.offset === 'number');
|
|
37
|
+
}
|
|
38
|
+
function isErrorPayload(value) {
|
|
39
|
+
return isPlainObject(value) && typeof value.message === 'string';
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
export function isResultEnvelope(value) {
|
|
45
|
+
if (!isPlainObject(value))
|
|
46
|
+
return false;
|
|
47
|
+
if (value.parseError !== undefined && !isParseErrorPayload(value.parseError))
|
|
48
|
+
return false;
|
|
49
|
+
if (value.error !== undefined && !isErrorPayload(value.error))
|
|
50
|
+
return false;
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
function asShFile(node) {
|
|
54
|
+
if (node.type !== 'File') {
|
|
55
|
+
throw new ShBridgeInternalError(`bridge: expected root node of type "File", got "${node.type}"`);
|
|
56
|
+
}
|
|
57
|
+
if (!Array.isArray(node.stmts)) {
|
|
58
|
+
throw new ShBridgeInternalError('bridge: expected root node to have an array "stmts" field');
|
|
59
|
+
}
|
|
60
|
+
return node;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Synchronously parses shell source into a normalized AST.
|
|
64
|
+
*
|
|
65
|
+
* The first call instantiates the WASM shim synchronously (Node-only); the
|
|
66
|
+
* instance is reused across subsequent calls, so repeated calls do not
|
|
67
|
+
* re-instantiate (see design/ARCHITECTURE.md).
|
|
68
|
+
*
|
|
69
|
+
* Throws {@link ShParseError} on a shell syntax error, carrying the position
|
|
70
|
+
* mvdan/sh reported: `.line` is 1-based exactly as mvdan/sh reports it;
|
|
71
|
+
* `.column` is converted to UTF-16 code units so it agrees with
|
|
72
|
+
* {@link ShNode.loc}'s columns (mvdan/sh itself reports `column` as a byte
|
|
73
|
+
* count — see {@link ShParseErrorInfo.column}). That unconverted, raw
|
|
74
|
+
* position appears only inside `.message`, which is mvdan/sh's own
|
|
75
|
+
* formatted string, verbatim. Throws {@link ShInvalidDialectError} when
|
|
76
|
+
* {@link ParseOptions.dialect} is not a supported dialect. Throws
|
|
77
|
+
* {@link ShBridgeInternalError} for failures that should never happen given
|
|
78
|
+
* a correctly-behaving shim (malformed envelope, unexpected root node
|
|
79
|
+
* type, shim contract violations).
|
|
80
|
+
*
|
|
81
|
+
* @public
|
|
82
|
+
*/
|
|
83
|
+
export function parseSync(text, options) {
|
|
84
|
+
const dialect = options?.dialect ?? 'bash';
|
|
85
|
+
const filename = options?.filename ?? 'input.sh';
|
|
86
|
+
if (!isSupportedShellDialect(dialect)) {
|
|
87
|
+
throw new ShInvalidDialectError(dialect, SUPPORTED_SHELL_DIALECTS);
|
|
88
|
+
}
|
|
89
|
+
const raw = callParse(text, dialect, filename);
|
|
90
|
+
const result = JSON.parse(raw);
|
|
91
|
+
if (!isResultEnvelope(result)) {
|
|
92
|
+
throw new ShBridgeInternalError('bridge: WASM shim returned an unexpected payload shape');
|
|
93
|
+
}
|
|
94
|
+
if (result.parseError) {
|
|
95
|
+
// mvdan/sh reports `column` as a byte count on the source line, not a
|
|
96
|
+
// UTF-16 code unit count — inconsistent with ShNode.loc.column, which is
|
|
97
|
+
// always UTF-16 (see design/ARCHITECTURE.md's "Byte→UTF-16 conversion").
|
|
98
|
+
// Recompute it from `offset` (a byte offset from the start of the file,
|
|
99
|
+
// which every position mvdan/sh reports carries) via the same
|
|
100
|
+
// conversion `normalize` applies to every node, so a thrown
|
|
101
|
+
// ShParseError's column always agrees with the column a ShNode at that
|
|
102
|
+
// position would report.
|
|
103
|
+
const column = toUtf16Column(text, result.parseError.offset, result.parseError.line);
|
|
104
|
+
throw new ShParseError(result.parseError.message, {
|
|
105
|
+
line: result.parseError.line,
|
|
106
|
+
column,
|
|
107
|
+
filename: result.parseError.filename,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
if (result.error) {
|
|
111
|
+
throw new ShBridgeInternalError(result.error.message);
|
|
112
|
+
}
|
|
113
|
+
if (result.file === undefined) {
|
|
114
|
+
throw new ShBridgeInternalError('bridge: WASM shim returned neither a file nor an error');
|
|
115
|
+
}
|
|
116
|
+
return asShFile(normalize(result.file, text));
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=parse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACzF,OAAO,EAAE,SAA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