cra-audit 0.1.0 → 1.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.
@@ -0,0 +1,225 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Minimal YAML parser covering the subset used by `pnpm-lock.yaml` and
5
+ * Yarn Berry (v2+) lockfiles: indentation-based maps and sequences, inline
6
+ * flow collections (`{ ... }` and `[ ... ]`) and single/double quoted scalars.
7
+ *
8
+ * It intentionally does NOT support anchors, aliases, block scalars (`|` / `>`),
9
+ * multi-document streams or complex keys. cra-audit ships with zero runtime
10
+ * dependencies, so this keeps us from pulling in a full YAML library just to
11
+ * read a lockfile.
12
+ *
13
+ * @param {string} text
14
+ * @returns {object|Array|null}
15
+ */
16
+ function parseYaml(text) {
17
+ const lines = [];
18
+ for (const raw of String(text).split(/\r?\n/)) {
19
+ const withoutComment = stripComment(raw);
20
+ if (withoutComment.trim() === '') continue;
21
+ const trimmedLeft = withoutComment.replace(/^ +/, '');
22
+ const indent = withoutComment.length - trimmedLeft.length;
23
+ lines.push({ indent, content: trimmedLeft.replace(/\s+$/, '') });
24
+ }
25
+
26
+ const state = { lines, pos: 0 };
27
+ if (state.lines.length === 0) return {};
28
+ return parseNode(state, state.lines[0].indent);
29
+ }
30
+
31
+ /** Removes an unquoted `#` comment, preserving `#` inside quoted scalars. */
32
+ function stripComment(line) {
33
+ let inSingle = false;
34
+ let inDouble = false;
35
+ for (let i = 0; i < line.length; i++) {
36
+ const ch = line[i];
37
+ if (ch === "'" && !inDouble) inSingle = !inSingle;
38
+ else if (ch === '"' && !inSingle) inDouble = !inDouble;
39
+ else if (ch === '#' && !inSingle && !inDouble && (i === 0 || /\s/.test(line[i - 1]))) {
40
+ return line.slice(0, i);
41
+ }
42
+ }
43
+ return line;
44
+ }
45
+
46
+ function parseNode(state, indent) {
47
+ const line = state.lines[state.pos];
48
+ if (line.content === '-' || line.content.startsWith('- ')) {
49
+ return parseSequence(state, indent);
50
+ }
51
+ return parseMap(state, indent);
52
+ }
53
+
54
+ function parseMap(state, indent) {
55
+ const map = {};
56
+ while (state.pos < state.lines.length) {
57
+ const line = state.lines[state.pos];
58
+ if (line.indent < indent) break;
59
+ if (line.indent > indent) { state.pos++; continue; }
60
+
61
+ const { key, rest } = splitKey(line.content);
62
+ if (key === null) { state.pos++; continue; }
63
+ state.pos++;
64
+
65
+ if (rest !== '') {
66
+ map[key] = parseScalarOrFlow(rest);
67
+ } else if (state.pos < state.lines.length && state.lines[state.pos].indent > indent) {
68
+ map[key] = parseNode(state, state.lines[state.pos].indent);
69
+ } else {
70
+ map[key] = null;
71
+ }
72
+ }
73
+ return map;
74
+ }
75
+
76
+ function parseSequence(state, indent) {
77
+ const arr = [];
78
+ while (state.pos < state.lines.length) {
79
+ const line = state.lines[state.pos];
80
+ if (line.indent < indent) break;
81
+ if (line.indent > indent) { state.pos++; continue; }
82
+ if (line.content !== '-' && !line.content.startsWith('- ')) break;
83
+
84
+ const after = line.content === '-' ? '' : line.content.slice(2);
85
+ state.pos++;
86
+ if (after === '') {
87
+ if (state.pos < state.lines.length && state.lines[state.pos].indent > indent) {
88
+ arr.push(parseNode(state, state.lines[state.pos].indent));
89
+ } else {
90
+ arr.push(null);
91
+ }
92
+ } else {
93
+ arr.push(parseScalarOrFlow(after));
94
+ }
95
+ }
96
+ return arr;
97
+ }
98
+
99
+ /** Splits `key: value` honoring quoted keys; returns { key, rest }. */
100
+ function splitKey(content) {
101
+ let inSingle = false;
102
+ let inDouble = false;
103
+ for (let i = 0; i < content.length; i++) {
104
+ const ch = content[i];
105
+ if (ch === "'" && !inDouble) inSingle = !inSingle;
106
+ else if (ch === '"' && !inSingle) inDouble = !inDouble;
107
+ else if (ch === ':' && !inSingle && !inDouble) {
108
+ const next = content[i + 1];
109
+ if (next === undefined || next === ' ') {
110
+ return { key: unquote(content.slice(0, i).trim()), rest: content.slice(i + 1).trim() };
111
+ }
112
+ }
113
+ }
114
+ return { key: null, rest: '' };
115
+ }
116
+
117
+ function parseScalarOrFlow(str) {
118
+ const s = str.trim();
119
+ if (s.startsWith('{') || s.startsWith('[')) {
120
+ return parseFlow(s);
121
+ }
122
+ return parseScalar(s);
123
+ }
124
+
125
+ function parseScalar(s) {
126
+ if (s === '' || s === '~' || s === 'null') return null;
127
+ if (s === 'true') return true;
128
+ if (s === 'false') return false;
129
+ return unquote(s);
130
+ }
131
+
132
+ function unquote(s) {
133
+ const t = s.trim();
134
+ if (t.length >= 2) {
135
+ if (t[0] === '"' && t[t.length - 1] === '"') {
136
+ return t.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
137
+ }
138
+ if (t[0] === "'" && t[t.length - 1] === "'") {
139
+ return t.slice(1, -1).replace(/''/g, "'");
140
+ }
141
+ }
142
+ return t;
143
+ }
144
+
145
+ /** Parses an inline flow collection: `{ a: b, c: [d, e] }`. */
146
+ function parseFlow(s) {
147
+ const ctx = { s, i: 0 };
148
+ return parseFlowValue(ctx);
149
+ }
150
+
151
+ function parseFlowValue(ctx) {
152
+ skipWs(ctx);
153
+ const ch = ctx.s[ctx.i];
154
+ if (ch === '{') return parseFlowMap(ctx);
155
+ if (ch === '[') return parseFlowSeq(ctx);
156
+ return parseScalar(readFlowScalar(ctx));
157
+ }
158
+
159
+ function parseFlowMap(ctx) {
160
+ const map = {};
161
+ ctx.i++; // consume '{'
162
+ skipWs(ctx);
163
+ if (ctx.s[ctx.i] === '}') { ctx.i++; return map; }
164
+ while (ctx.i < ctx.s.length) {
165
+ skipWs(ctx);
166
+ const key = unquote(readFlowKey(ctx));
167
+ skipWs(ctx);
168
+ if (ctx.s[ctx.i] === ':') ctx.i++;
169
+ const value = parseFlowValue(ctx);
170
+ map[key] = value;
171
+ skipWs(ctx);
172
+ if (ctx.s[ctx.i] === ',') { ctx.i++; continue; }
173
+ if (ctx.s[ctx.i] === '}') { ctx.i++; break; }
174
+ break;
175
+ }
176
+ return map;
177
+ }
178
+
179
+ function parseFlowSeq(ctx) {
180
+ const arr = [];
181
+ ctx.i++; // consume '['
182
+ skipWs(ctx);
183
+ if (ctx.s[ctx.i] === ']') { ctx.i++; return arr; }
184
+ while (ctx.i < ctx.s.length) {
185
+ arr.push(parseFlowValue(ctx));
186
+ skipWs(ctx);
187
+ if (ctx.s[ctx.i] === ',') { ctx.i++; continue; }
188
+ if (ctx.s[ctx.i] === ']') { ctx.i++; break; }
189
+ break;
190
+ }
191
+ return arr;
192
+ }
193
+
194
+ function readFlowKey(ctx) {
195
+ return readUntil(ctx, [':', ',', '}']);
196
+ }
197
+
198
+ function readFlowScalar(ctx) {
199
+ return readUntil(ctx, [',', '}', ']']);
200
+ }
201
+
202
+ /** Reads a quoted or bare token until one of the stop characters at top level. */
203
+ function readUntil(ctx, stops) {
204
+ skipWs(ctx);
205
+ const start = ctx.i;
206
+ const quote = ctx.s[ctx.i];
207
+ if (quote === '"' || quote === "'") {
208
+ ctx.i++;
209
+ while (ctx.i < ctx.s.length) {
210
+ const ch = ctx.s[ctx.i];
211
+ if (ch === '\\' && quote === '"') { ctx.i += 2; continue; }
212
+ if (ch === quote) { ctx.i++; break; }
213
+ ctx.i++;
214
+ }
215
+ return ctx.s.slice(start, ctx.i).trim();
216
+ }
217
+ while (ctx.i < ctx.s.length && !stops.includes(ctx.s[ctx.i])) ctx.i++;
218
+ return ctx.s.slice(start, ctx.i).trim();
219
+ }
220
+
221
+ function skipWs(ctx) {
222
+ while (ctx.i < ctx.s.length && /\s/.test(ctx.s[ctx.i])) ctx.i++;
223
+ }
224
+
225
+ module.exports = { parseYaml };