@openpeepshq/rn-components 0.20.0 → 0.21.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,281 @@
1
+ import { readdirSync, readFileSync } from 'fs';
2
+ import { join } from 'path';
3
+
4
+ /**
5
+ * Regression guard for the glob 9/10 -> 14.0.6 override (GHSA-3g3m-2fjr-6pw4,
6
+ * issue #2255).
7
+ *
8
+ * babel-plugin-module-resolver reads glob through
9
+ * `interopRequireDefault(require('glob')).default`. glob >= 10 (CJS) sets
10
+ * `__esModule: true` but exports no `default`, so the plugin's
11
+ * `normalizeRoot` (its `glob.default.hasMagic` / `glob.default.sync` calls)
12
+ * throws `Cannot read properties of undefined` whenever module-resolver is
13
+ * configured with a `root` option. The 14.0.6 override therefore makes a
14
+ * module-resolver `root` option a hard crash. No first-party babel / metro /
15
+ * bob config may pass `root` to that plugin, so this guard scans every such
16
+ * config and fails the suite if any of them does.
17
+ *
18
+ * It intentionally does NOT evaluate the configs (they require their runtime
19
+ * deps); it is a static source check. Detection is name-based, not
20
+ * resolver-based: both the quoted (`'root'` / `"root"`) and unquoted
21
+ * spellings of a top-level `root` key are handled, but only the textual
22
+ * plugin-reference-followed-by-options shape is detectable — a `root`
23
+ * passed through a variable or computed key would not be flagged.
24
+ *
25
+ * Name coverage: `babel.config.*`, `.babelrc*`, `metro.config.*`, and
26
+ * `metro.js` (the plain-JS Metro filename used in-repo by
27
+ * `platform/rn-components`) are all matched. `bob.config.*` currently
28
+ * matches no file — bob 0.35.2 reads its config from the `package.json`
29
+ * `bob` field — so that pattern is a placeholder, not a live detection.
30
+ */
31
+
32
+ const REPO_ROOT = join(__dirname, '..', '..', '..', '..');
33
+
34
+ const SKIP_DIRS = new Set([
35
+ 'node_modules',
36
+ '.git',
37
+ 'dist',
38
+ 'lib',
39
+ 'build',
40
+ 'coverage',
41
+ '.next',
42
+ '.turbo',
43
+ 'android',
44
+ 'ios',
45
+ ]);
46
+
47
+ function isConfigName(name: string): boolean {
48
+ return (
49
+ name.startsWith('babel.config.') ||
50
+ name === '.babelrc' ||
51
+ name.startsWith('.babelrc.') ||
52
+ name.startsWith('metro.config.') ||
53
+ name === 'metro.js' ||
54
+ name.startsWith('bob.config.')
55
+ );
56
+ }
57
+
58
+ function collectConfigFiles(dir: string, found: string[]): void {
59
+ let entries;
60
+ try {
61
+ entries = readdirSync(dir, { withFileTypes: true });
62
+ } catch {
63
+ return;
64
+ }
65
+ for (const entry of entries) {
66
+ if (entry.name === 'node_modules' || entry.name === '.git') continue;
67
+ // Do not follow symlinked dependency stores (pnpm).
68
+ if (entry.isSymbolicLink()) continue;
69
+ const p = join(dir, entry.name);
70
+ if (entry.isDirectory()) {
71
+ if (!SKIP_DIRS.has(entry.name)) collectConfigFiles(p, found);
72
+ } else if (entry.isFile() && isConfigName(entry.name)) {
73
+ found.push(p);
74
+ }
75
+ }
76
+ }
77
+
78
+ /** Strip /* ... *\/ and // ... line comments so they can't match as code. */
79
+ function stripComments(src: string): string {
80
+ return src
81
+ .replace(/\/\*[\s\S]*?\*\//g, '')
82
+ .replace(/(^|[^:\\])\/\/[^\n]*/g, '$1');
83
+ }
84
+
85
+ /**
86
+ * Matches a module-resolver plugin reference, with or without a
87
+ * require / require.resolve wrapper, e.g. `'module-resolver'`,
88
+ * `"babel-plugin-module-resolver"`, or
89
+ * `require.resolve('babel-plugin-module-resolver')`.
90
+ */
91
+ const PLUGIN_REF =
92
+ /(?:require\(\s*['"]|require\.resolve\(\s*['"])?(?:@?[\w.-]+\/)?(?:babel-plugin-)?module-resolver['"]?\s*\)?/g;
93
+
94
+ /** Return the balanced `{...}` options object that immediately follows a
95
+ * plugin reference (the `,{ ... }` after it), or null if there is none. */
96
+ function extractFollowingOptions(code: string, refEnd: number): string | null {
97
+ const after = code.slice(refEnd);
98
+ const comma = after.indexOf(',');
99
+ if (comma === -1) return null;
100
+ const seg = after.slice(comma + 1);
101
+ const braceIdx = seg.indexOf('{');
102
+ if (braceIdx === -1) return null;
103
+ if (!/^\s*$/.test(seg.slice(0, braceIdx))) return null;
104
+ const openIdx = comma + 1 + braceIdx;
105
+ let depth = 0;
106
+ for (let i = openIdx; i < after.length; i++) {
107
+ if (after[i] === '{') depth++;
108
+ else if (after[i] === '}') {
109
+ depth--;
110
+ if (depth === 0) return after.slice(openIdx, i + 1);
111
+ }
112
+ }
113
+ return null;
114
+ }
115
+
116
+ /**
117
+ * True if a top-level (depth 0) `root` key starts at `inner[i]` — the
118
+ * unquoted or the quoted (`'root'` / `"root"`) spelling, in the colon form
119
+ * (`root: …` / `"root": …`) or the unquoted shorthand form (`root,` /
120
+ * `root }` / end). A key position is one whose previous non-whitespace
121
+ * character is `{` or `,` (or the start of the body) — so a *value* named
122
+ * `root` (after `:`) is never matched.
123
+ */
124
+ function isRootKeyAt(inner: string, i: number): boolean {
125
+ let isKeyStart = true;
126
+ for (let j = i - 1; j >= 0; j--) {
127
+ const ch = inner[j];
128
+ if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r') continue;
129
+ isKeyStart = ch === '{' || ch === ',';
130
+ break;
131
+ }
132
+ if (!isKeyStart) return false;
133
+ const c = inner[i];
134
+ let end: number;
135
+ if (c === '"' || c === "'") {
136
+ if (!inner.startsWith(c + 'root' + c, i)) return false;
137
+ end = i + 6; // quote + "root" + quote
138
+ } else {
139
+ if (c !== 'r' || !inner.startsWith('root', i)) return false;
140
+ end = i + 4;
141
+ }
142
+ const rest = inner.slice(end);
143
+ return /^\s*:/.test(rest) || /^\s*[,}]/.test(rest) || /^\s*$/.test(rest);
144
+ }
145
+
146
+ /**
147
+ * True if `obj` (a `{...}` literal) has a top-level (depth 0) `root` key —
148
+ * the colon form `{ root: ... }`, the quoted-key form `{ "root": ... }`
149
+ * (as in a JSON `.babelrc`), or the shorthand `{ root }`. Nested
150
+ * occurrences (e.g. `alias: { root: ... }`) do not count.
151
+ */
152
+ function hasTopLevelRoot(obj: string): boolean {
153
+ const inner = obj.slice(1, obj.length - 1);
154
+ let depth = 0;
155
+ let inStr: string | null = null;
156
+ for (let i = 0; i < inner.length; i++) {
157
+ const c = inner[i];
158
+ if (inStr) {
159
+ if (c === '\\') i++;
160
+ else if (c === inStr) inStr = null;
161
+ continue;
162
+ }
163
+ if (
164
+ depth === 0 &&
165
+ (c === 'r' || c === '"' || c === "'") &&
166
+ isRootKeyAt(inner, i)
167
+ ) {
168
+ return true;
169
+ }
170
+ if (c === '"' || c === "'" || c === '`') {
171
+ inStr = c;
172
+ continue;
173
+ }
174
+ if (c === '{' || c === '[') {
175
+ depth++;
176
+ continue;
177
+ }
178
+ if (c === '}' || c === ']') {
179
+ depth--;
180
+ continue;
181
+ }
182
+ }
183
+ return false;
184
+ }
185
+
186
+ function moduleResolverHasRoot(code: string): boolean {
187
+ PLUGIN_REF.lastIndex = 0;
188
+ let m: RegExpExecArray | null;
189
+ while ((m = PLUGIN_REF.exec(code)) !== null) {
190
+ const options = extractFollowingOptions(code, m.index + m[0].length);
191
+ if (options && hasTopLevelRoot(options)) return true;
192
+ if (m[0].length === 0) PLUGIN_REF.lastIndex++;
193
+ }
194
+ return false;
195
+ }
196
+
197
+ describe('module-resolver `root` regression guard (#2255 glob 14.0.6)', () => {
198
+ it('discovers at least one first-party babel/metro/bob config (guard is active)', () => {
199
+ const configs: string[] = [];
200
+ collectConfigFiles(REPO_ROOT, configs);
201
+ expect(configs.length).toBeGreaterThan(0);
202
+ });
203
+
204
+ it('no first-party babel/metro/bob config passes `root` to module-resolver', () => {
205
+ const configs: string[] = [];
206
+ collectConfigFiles(REPO_ROOT, configs);
207
+ const violations = configs.filter((file) => {
208
+ const src = readFileSync(file, 'utf8');
209
+ return moduleResolverHasRoot(stripComments(src));
210
+ });
211
+ expect(violations).toEqual([]);
212
+ });
213
+ });
214
+
215
+ describe('module-resolver `root` guard: scanner unit checks', () => {
216
+ it('flags a top-level `root` key on a module-resolver options object', () => {
217
+ const code = `plugins: [['module-resolver', { root: ['./src'], alias: { x: 'y' } }]]`;
218
+ expect(moduleResolverHasRoot(stripComments(code))).toBe(true);
219
+ });
220
+
221
+ it('flags the require.resolve-wrapped reference with `root`', () => {
222
+ const code = `plugins: [[require.resolve('babel-plugin-module-resolver'), { root: ['.'] }]]`;
223
+ expect(moduleResolverHasRoot(stripComments(code))).toBe(true);
224
+ });
225
+
226
+ it('flags a single-quoted `root` key on a module-resolver options object', () => {
227
+ const code = `plugins: [['module-resolver', { 'root': ['./src'], alias: { x: 'y' } }]]`;
228
+ expect(moduleResolverHasRoot(stripComments(code))).toBe(true);
229
+ });
230
+
231
+ it('flags a double-quoted (JSON .babelrc) `root` key on a module-resolver options object', () => {
232
+ const code = `plugins: [["module-resolver", { "root": "./src" }]]`;
233
+ expect(moduleResolverHasRoot(stripComments(code))).toBe(true);
234
+ });
235
+
236
+ it('does not flag a module-resolver reference without `root`', () => {
237
+ const code = `plugins: [['babel-plugin-module-resolver', { alias: { x: 'y' } }]]`;
238
+ expect(moduleResolverHasRoot(stripComments(code))).toBe(false);
239
+ });
240
+
241
+ it('does not flag a value named `root` (a key, not a value)', () => {
242
+ const code = `plugins: [['module-resolver', { x: 'root', alias: { x: 'y' } }]]`;
243
+ expect(moduleResolverHasRoot(stripComments(code))).toBe(false);
244
+ });
245
+
246
+ it('does not flag a nested `root` (e.g. under `alias`)', () => {
247
+ const code = `plugins: [['module-resolver', { alias: { root: './src' } }]]`;
248
+ expect(moduleResolverHasRoot(stripComments(code))).toBe(false);
249
+ });
250
+
251
+ it('does not flag a module-resolver mention in a comment', () => {
252
+ const code = `// module-resolver root: do not use\nplugins: []`;
253
+ expect(moduleResolverHasRoot(stripComments(code))).toBe(false);
254
+ });
255
+
256
+ it('isConfigName: accepts the plain-JS `metro.js` filename, rejects near-misses', () => {
257
+ expect(isConfigName('metro.js')).toBe(true);
258
+ expect(isConfigName('metro.config.js')).toBe(true);
259
+ expect(isConfigName('babel.config.cjs')).toBe(true);
260
+ expect(isConfigName('bob.config.js')).toBe(true);
261
+ expect(isConfigName('metro.js.map')).toBe(false);
262
+ expect(isConfigName('x-metro.js')).toBe(false);
263
+ });
264
+
265
+ it('stripComments: drops `// module-resolver root: …` comment text', () => {
266
+ const out = stripComments(
267
+ "plugins: [] // module-resolver root: './src' would crash under glob 14\nx = 1"
268
+ );
269
+ expect(out).not.toContain('module-resolver');
270
+ expect(out).not.toContain('would crash');
271
+ expect(out).toContain('plugins: []');
272
+ expect(out).toContain('x = 1');
273
+ });
274
+
275
+ it('stripComments: does not truncate URL-ish values (`//` after `:` or `\\`)', () => {
276
+ const url = "url = 'https://openpeeps.org/docs/glob';";
277
+ expect(stripComments(url)).toBe(url);
278
+ const winPath = "root = 'c:\\//share';";
279
+ expect(stripComments(winPath)).toBe(winPath);
280
+ });
281
+ });