forked from LedgerHQ/ledger-live
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbabel-plugin-inject-collapsable.js
More file actions
74 lines (66 loc) · 2.43 KB
/
Copy pathbabel-plugin-inject-collapsable.js
File metadata and controls
74 lines (66 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Babel plugin to inject `collapsable={false}` to all React Native components
*
* This prevents view collapsing in Detox tests, making elements more reliably
* findable. This plugin should only be enabled when `process.env.DETOX` is set
* (see `babel.config.js`). When enabled, it injects `collapsable={false}` to
* disable view flattening entirely for Detox tests.
*/
module.exports = function collapsablePlugin({ types: t }) {
// Whitelist of packages to transform (workspace packages)
const ALLOWED_PACKAGES = [
"@ledgerhq/native-ui",
// Add more @ledgerhq packages here if needed
];
return {
name: "inject-collapsable-prop",
visitor: {
Program: {
enter(path, state) {
const filename = state.file.opts.filename;
if (!filename) {
state.skipFile = true;
return;
}
// Skip if file is in node_modules and not a whitelisted package
// For non-node_modules files, only process src/ directory
if (filename.includes("node_modules")) {
const isWhitelisted = ALLOWED_PACKAGES.some(
pkg =>
filename.includes(`node_modules/${pkg}`) ||
filename.includes(`node_modules\\${pkg}`),
);
if (!isWhitelisted) {
state.skipFile = true;
return;
}
} else if (!filename.includes("/src/") && !filename.includes("\\src\\")) {
state.skipFile = true;
return;
}
},
},
JSXOpeningElement(path, state) {
if (state.skipFile) {
return;
}
const elementName = path.node.name.name;
if (!elementName) {
return;
}
// Inject `collapsable={false}` into ALL components without exclusion
// React Native will ignore the prop on components that don't support
// it (e.g. Text, Image, etc.). We need to check if `collapsable` prop
// already exists to avoid injecting it multiple times
const hasCollapsable = path.node.attributes.some(
attr => attr?.name?.name === "collapsable",
);
if (!hasCollapsable) {
const collapsableValue = t.jsxExpressionContainer(t.booleanLiteral(false));
const newAttribute = t.jsxAttribute(t.jsxIdentifier("collapsable"), collapsableValue);
path.node.attributes.push(newAttribute);
}
},
},
};
};