@openpeepshq/rn-components 0.19.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.
- package/package.json +4 -5
- package/src/components/conversations/ConversationMessageBubble.test.tsx +158 -0
- package/src/components/conversations/ConversationMessageBubble.tsx +43 -25
- package/src/components/jams/JamCallParticipant.tsx +84 -16
- package/src/components/jams/JamChatMessage.test.tsx +139 -0
- package/src/components/jams/JamChatMessage.tsx +13 -0
- package/src/components/jams/JamPeopleDrawer.test.tsx +77 -3
- package/src/components/jams/JamPeopleDrawer.tsx +94 -3
- package/src/components/jams/JamReactionMenu.tsx +10 -31
- package/src/components/jams/JamRoom.tsx +15 -0
- package/src/components/markdown/OpenpeepsMarkdown.tsx +10 -3
- package/src/components/markdown/theme.test.ts +78 -0
- package/src/components/markdown/theme.ts +30 -12
- package/src/components/post/CompactReplyParent.test.tsx +337 -0
- package/src/components/post/CompactReplyParent.tsx +21 -5
- package/src/components/post/TruncatedMarkdown.test.tsx +201 -0
- package/src/components/post/TruncatedMarkdown.tsx +98 -10
- package/src/components/post/pieces/PostActions.test.tsx +27 -19
- package/src/components/post/pieces/PostActions.tsx +7 -11
- package/src/components/post/pieces/ReactionButton.tsx +41 -0
- package/src/components/post/pieces/ReactionPicker.test.tsx +80 -161
- package/src/components/post/pieces/ReactionPicker.tsx +32 -33
- package/src/components/post/pieces/index.ts +1 -0
- package/src/components/preview-link/local/WelcomePreview.tsx +1 -0
- package/src/components/resources/CategoryPathPicker.tsx +8 -2
- package/src/components/resources/ResourceCard.test.tsx +48 -4
- package/src/components/resources/ResourceCard.tsx +8 -2
- package/src/components/resources/ResourceLibrary.test.tsx +40 -2
- package/src/components/resources/ResourceLibrary.tsx +11 -6
- package/src/lib/moduleResolverRootGuard.test.ts +281 -0
|
@@ -1,58 +1,57 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import {
|
|
3
|
-
import { REACTION_TYPES } from '@openpeepshq/common';
|
|
2
|
+
import { View } from 'react-native';
|
|
4
3
|
import type { ReactionData } from '@openpeepshq/common/types';
|
|
5
4
|
import { useTranslation } from 'react-i18next';
|
|
6
|
-
import {
|
|
7
|
-
import { XIcon } from '../../icons/index';
|
|
8
|
-
import { cn } from '../../../lib/utils';
|
|
5
|
+
import { ReactionButton } from './ReactionButton';
|
|
9
6
|
|
|
10
7
|
export interface ReactionPickerProps {
|
|
11
8
|
/** Reactions the current profile already attached to this post. */
|
|
12
9
|
myReactions: string[];
|
|
10
|
+
/** Emojis the community allows on posts (from useAllowedReactions). */
|
|
11
|
+
reactions: readonly string[];
|
|
13
12
|
onToggle: (reaction: ReactionData['reaction']) => void;
|
|
14
|
-
/** Closes the picker (mirrors the web popover closing on outside click). */
|
|
15
|
-
onClose: () => void;
|
|
16
13
|
}
|
|
17
14
|
|
|
18
15
|
/**
|
|
19
|
-
* Post reaction selection: a row of the
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* picker's outside-click dismissal.
|
|
16
|
+
* Post reaction selection: a row of the community's allowed reactions, plus
|
|
17
|
+
* any of the profile's own reactions that are no longer allowed (orphans) so
|
|
18
|
+
* they stay visible and retractable. Tapping an emoji toggles the current
|
|
19
|
+
* profile's reaction for the post (mirrors the jam reaction menu).
|
|
24
20
|
*/
|
|
25
21
|
export const ReactionPicker = ({
|
|
26
22
|
myReactions,
|
|
23
|
+
reactions,
|
|
27
24
|
onToggle,
|
|
28
|
-
onClose,
|
|
29
25
|
}: ReactionPickerProps) => {
|
|
30
26
|
const { t } = useTranslation();
|
|
31
|
-
|
|
27
|
+
const orphans = myReactions.filter(
|
|
28
|
+
(reaction) => !reactions.includes(reaction)
|
|
29
|
+
);
|
|
32
30
|
return (
|
|
33
31
|
<View className="flex-row items-center gap-1">
|
|
34
|
-
{
|
|
35
|
-
<
|
|
32
|
+
{reactions.map((reaction) => (
|
|
33
|
+
<ReactionButton
|
|
36
34
|
key={reaction}
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
reaction={reaction}
|
|
36
|
+
active={myReactions.includes(reaction)}
|
|
39
37
|
onPress={() => onToggle(reaction)}
|
|
40
|
-
|
|
41
|
-
'shrink-0 rounded-md p-2',
|
|
42
|
-
myReactions.includes(reaction) && 'bg-border'
|
|
43
|
-
)}
|
|
44
|
-
>
|
|
45
|
-
<ThemedText className="text-lg">{reaction}</ThemedText>
|
|
46
|
-
</Pressable>
|
|
38
|
+
/>
|
|
47
39
|
))}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
40
|
+
{orphans.length > 0 ? (
|
|
41
|
+
<>
|
|
42
|
+
<View className="mx-1 h-8 w-px shrink-0 bg-border" />
|
|
43
|
+
{orphans.map((reaction) => (
|
|
44
|
+
<ReactionButton
|
|
45
|
+
key={reaction}
|
|
46
|
+
reaction={reaction}
|
|
47
|
+
orphan
|
|
48
|
+
active
|
|
49
|
+
title={t('posts.actions.orphanedReaction')}
|
|
50
|
+
onPress={() => onToggle(reaction)}
|
|
51
|
+
/>
|
|
52
|
+
))}
|
|
53
|
+
</>
|
|
54
|
+
) : null}
|
|
56
55
|
</View>
|
|
57
56
|
);
|
|
58
57
|
};
|
|
@@ -19,6 +19,7 @@ export * from './PostInfoHeader';
|
|
|
19
19
|
export * from './PostMenu';
|
|
20
20
|
export * from './PostReactionHeader';
|
|
21
21
|
export * from './ReactionPicker';
|
|
22
|
+
export * from './ReactionButton';
|
|
22
23
|
export * from './ShareMenu';
|
|
23
24
|
export * from './UnreadPostIndicator';
|
|
24
25
|
export * from './VideoPlayer';
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { useState } from 'react';
|
|
2
2
|
import { Pressable, View } from 'react-native';
|
|
3
3
|
import type { CategoryNode } from '@openpeepshq/common/lib';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
RESOURCE_CATEGORY_NAME_MAX_LENGTH,
|
|
6
|
+
categoryPathKey,
|
|
7
|
+
} from '@openpeepshq/common/lib';
|
|
5
8
|
import { Input } from '../ui/input';
|
|
6
9
|
import { ThemedText } from '../ui/themed-text';
|
|
7
10
|
import { useTranslation } from 'react-i18next';
|
|
@@ -76,6 +79,7 @@ export const CategoryPathPicker = ({
|
|
|
76
79
|
<Input
|
|
77
80
|
value={level}
|
|
78
81
|
onChangeText={setLevel}
|
|
82
|
+
maxLength={RESOURCE_CATEGORY_NAME_MAX_LENGTH}
|
|
79
83
|
placeholder={t('resources.form.addLevel', {
|
|
80
84
|
defaultValue: 'Add a folder',
|
|
81
85
|
})}
|
|
@@ -83,7 +87,9 @@ export const CategoryPathPicker = ({
|
|
|
83
87
|
<Pressable
|
|
84
88
|
className="border-border rounded-md border bg-background px-3 py-1.5"
|
|
85
89
|
onPress={() => {
|
|
86
|
-
const name = level
|
|
90
|
+
const name = level
|
|
91
|
+
.trim()
|
|
92
|
+
.slice(0, RESOURCE_CATEGORY_NAME_MAX_LENGTH);
|
|
87
93
|
if (!name) return;
|
|
88
94
|
onChange([...path, name]);
|
|
89
95
|
setLevel('');
|
|
@@ -96,8 +96,16 @@ jest.mock('../custom/common', () => ({
|
|
|
96
96
|
}));
|
|
97
97
|
|
|
98
98
|
jest.mock('../ui/themed-text', () => ({
|
|
99
|
-
ThemedText: (props: {
|
|
100
|
-
|
|
99
|
+
ThemedText: (props: {
|
|
100
|
+
children?: unknown;
|
|
101
|
+
className?: string;
|
|
102
|
+
numberOfLines?: number;
|
|
103
|
+
}) =>
|
|
104
|
+
React.createElement(
|
|
105
|
+
'ThemedText',
|
|
106
|
+
{ className: props.className, numberOfLines: props.numberOfLines },
|
|
107
|
+
props.children
|
|
108
|
+
),
|
|
101
109
|
}));
|
|
102
110
|
|
|
103
111
|
jest.mock('../post/VideoPlayOverlay', () => ({
|
|
@@ -188,19 +196,34 @@ const linkPost = resourcePost({
|
|
|
188
196
|
url: 'https://example.com/docs',
|
|
189
197
|
});
|
|
190
198
|
|
|
191
|
-
const renderCard = (
|
|
199
|
+
const renderCard = (
|
|
200
|
+
post: ResourceCardProps['post'],
|
|
201
|
+
layout: 'card' | 'list' = 'list'
|
|
202
|
+
) => {
|
|
192
203
|
let tree: ReturnType<typeof ReactTestRenderer.create>;
|
|
193
204
|
ReactTestRenderer.act(() => {
|
|
194
205
|
tree = ReactTestRenderer.create(
|
|
195
206
|
React.createElement(ResourceCard, {
|
|
196
207
|
post,
|
|
197
|
-
layout
|
|
208
|
+
layout,
|
|
198
209
|
})
|
|
199
210
|
);
|
|
200
211
|
});
|
|
201
212
|
return tree;
|
|
202
213
|
};
|
|
203
214
|
|
|
215
|
+
// A single unbroken word: on RN a Text won't shrink by default, so the
|
|
216
|
+
// category line needs `shrink` + `numberOfLines` to stay inside the card
|
|
217
|
+
// (issue #2174, mirrored from the web break-words fix).
|
|
218
|
+
const LONG_CATEGORY = 'a'.repeat(200);
|
|
219
|
+
|
|
220
|
+
const longCategoryPost = resourcePost({
|
|
221
|
+
title: 'Long path',
|
|
222
|
+
resourceKind: 'link',
|
|
223
|
+
url: 'https://example.com/docs',
|
|
224
|
+
categoryPath: ['Root', LONG_CATEGORY],
|
|
225
|
+
});
|
|
226
|
+
|
|
204
227
|
// The bookmark button is also a Pressable; it renders disabled for the
|
|
205
228
|
// anonymous profile the mock returns, so enabled Pressables are the
|
|
206
229
|
// card's actions only.
|
|
@@ -272,3 +295,24 @@ describe('ResourceCard list-layout actions', () => {
|
|
|
272
295
|
tree.unmount();
|
|
273
296
|
});
|
|
274
297
|
});
|
|
298
|
+
|
|
299
|
+
describe('ResourceCard long category path (issue #2174)', () => {
|
|
300
|
+
it.each(['card', 'list'] as const)(
|
|
301
|
+
'constrains the category line to two shrinkable lines in the %s layout',
|
|
302
|
+
(layout) => {
|
|
303
|
+
const tree = renderCard(longCategoryPost, layout);
|
|
304
|
+
|
|
305
|
+
const pathText = tree.root
|
|
306
|
+
.findAllByType('ThemedText')
|
|
307
|
+
.find(
|
|
308
|
+
(node: { props: { children?: unknown } }) =>
|
|
309
|
+
typeof node.props.children === 'string' &&
|
|
310
|
+
node.props.children.includes(LONG_CATEGORY)
|
|
311
|
+
);
|
|
312
|
+
expect(pathText).toBeTruthy();
|
|
313
|
+
expect(pathText!.props.className).toContain('shrink');
|
|
314
|
+
expect(pathText!.props.numberOfLines).toBe(2);
|
|
315
|
+
tree.unmount();
|
|
316
|
+
}
|
|
317
|
+
);
|
|
318
|
+
});
|
|
@@ -211,7 +211,10 @@ export const ResourceCard = ({
|
|
|
211
211
|
{resource.title}
|
|
212
212
|
</ThemedText>
|
|
213
213
|
{resource.categoryPath?.length ? (
|
|
214
|
-
<ThemedText
|
|
214
|
+
<ThemedText
|
|
215
|
+
className="shrink text-xs text-muted-foreground"
|
|
216
|
+
numberOfLines={2}
|
|
217
|
+
>
|
|
215
218
|
{resource.categoryPath.join(' / ')}
|
|
216
219
|
</ThemedText>
|
|
217
220
|
) : null}
|
|
@@ -330,7 +333,10 @@ export const ResourceCard = ({
|
|
|
330
333
|
{resource.title}
|
|
331
334
|
</ThemedText>
|
|
332
335
|
{resource.categoryPath?.length ? (
|
|
333
|
-
<ThemedText
|
|
336
|
+
<ThemedText
|
|
337
|
+
className="shrink text-xs text-muted-foreground"
|
|
338
|
+
numberOfLines={2}
|
|
339
|
+
>
|
|
334
340
|
{resource.categoryPath.join(' / ')}
|
|
335
341
|
</ThemedText>
|
|
336
342
|
) : null}
|
|
@@ -62,6 +62,7 @@ jest.mock('@openpeepshq/common/lib', () => ({
|
|
|
62
62
|
// Mirrored from platform/common/src/lib/resources.ts — the built
|
|
63
63
|
// package ships ESM which Jest's node transform doesn't cover.
|
|
64
64
|
categoryPathKey: (path: readonly string[]): string => JSON.stringify(path),
|
|
65
|
+
RESOURCE_CATEGORY_NAME_MAX_LENGTH: 120,
|
|
65
66
|
RESOURCE_KINDS: ['video', 'file', 'gallery', 'audio', 'link'],
|
|
66
67
|
}));
|
|
67
68
|
|
|
@@ -79,8 +80,16 @@ jest.mock('../ui/loading-spinner', () => ({
|
|
|
79
80
|
}));
|
|
80
81
|
|
|
81
82
|
jest.mock('../ui/themed-text', () => ({
|
|
82
|
-
ThemedText: (props: {
|
|
83
|
-
|
|
83
|
+
ThemedText: (props: {
|
|
84
|
+
children?: unknown;
|
|
85
|
+
className?: string;
|
|
86
|
+
numberOfLines?: number;
|
|
87
|
+
}) =>
|
|
88
|
+
React.createElement(
|
|
89
|
+
'ThemedText',
|
|
90
|
+
{ className: props.className, numberOfLines: props.numberOfLines },
|
|
91
|
+
props.children
|
|
92
|
+
),
|
|
84
93
|
}));
|
|
85
94
|
|
|
86
95
|
jest.mock('../icons', () => ({
|
|
@@ -258,6 +267,35 @@ describe('ResourceLibrary folder tree collision (issue #2148)', () => {
|
|
|
258
267
|
});
|
|
259
268
|
});
|
|
260
269
|
|
|
270
|
+
describe('ResourceLibrary breadcrumb long path (issue #2174)', () => {
|
|
271
|
+
it('constrains the breadcrumb to a single shrinkable line', () => {
|
|
272
|
+
mockTranslations = {};
|
|
273
|
+
// A non-empty pathPrefix so the breadcrumb path span renders.
|
|
274
|
+
mockLibrary = {
|
|
275
|
+
...libraryFor([slashPost]),
|
|
276
|
+
pathPrefix: ['[TEST] A/B'],
|
|
277
|
+
hierarchy: false,
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
const tree = render(
|
|
281
|
+
React.createElement(ResourceLibrary, {
|
|
282
|
+
groupId: 'g1',
|
|
283
|
+
} as ResourceLibraryProps)
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
const crumb = tree.root
|
|
287
|
+
.findAllByType('ThemedText')
|
|
288
|
+
.find(
|
|
289
|
+
(node: { props: { children?: unknown } }) =>
|
|
290
|
+
typeof node.props.children === 'string' &&
|
|
291
|
+
node.props.children.includes('[TEST] A/B')
|
|
292
|
+
);
|
|
293
|
+
expect(crumb).toBeTruthy();
|
|
294
|
+
expect(crumb!.props.className).toContain('shrink');
|
|
295
|
+
expect(crumb!.props.numberOfLines).toBe(1);
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
|
|
261
299
|
describe('CategoryPathPicker RN parity', () => {
|
|
262
300
|
beforeEach(() => {
|
|
263
301
|
mockTranslations = {};
|
|
@@ -74,9 +74,9 @@ const Tree = ({
|
|
|
74
74
|
}`}
|
|
75
75
|
/>
|
|
76
76
|
</Pressable>
|
|
77
|
-
<Pressable onPress={() => onSelect(node.path)}>
|
|
77
|
+
<Pressable className="flex-1" onPress={() => onSelect(node.path)}>
|
|
78
78
|
<ThemedText
|
|
79
|
-
className={`text-sm ${
|
|
79
|
+
className={`shrink text-sm ${
|
|
80
80
|
isSelected
|
|
81
81
|
? 'font-semibold text-foreground'
|
|
82
82
|
: 'text-muted-foreground'
|
|
@@ -268,12 +268,17 @@ export const ResourceLibrary = ({ groupId }: ResourceLibraryProps) => {
|
|
|
268
268
|
</Pressable>
|
|
269
269
|
{library.pathPrefix.length > 0 ? (
|
|
270
270
|
<>
|
|
271
|
-
<ThemedText className="text-sm text-muted-foreground">
|
|
271
|
+
<ThemedText className="shrink-0 text-sm text-muted-foreground">
|
|
272
272
|
/
|
|
273
273
|
</ThemedText>
|
|
274
|
-
<
|
|
275
|
-
|
|
276
|
-
|
|
274
|
+
<View className="flex-1">
|
|
275
|
+
<ThemedText
|
|
276
|
+
className="shrink text-sm text-muted-foreground"
|
|
277
|
+
numberOfLines={1}
|
|
278
|
+
>
|
|
279
|
+
{library.pathPrefix.join(' / ')}
|
|
280
|
+
</ThemedText>
|
|
281
|
+
</View>
|
|
277
282
|
</>
|
|
278
283
|
) : null}
|
|
279
284
|
<Pressable
|
|
@@ -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
|
+
});
|