Skip to content

Commit a676703

Browse files
committed
add dirty flag, update tests
1 parent f651f2e commit a676703

File tree

8 files changed

+59
-67
lines changed

8 files changed

+59
-67
lines changed
Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +0,0 @@
1-
function App() {
2-
const [theme, setTheme] = useState('light');
3-
4-
return (
5-
<Context value={theme}>
6-
<Page />
7-
</Context>
8-
);
9-
}
Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +0,0 @@
1-
function App({ url }: { url: string }) {
2-
const [theme, setTheme] = useState<'light' | 'dark'>('light');
3-
4-
return (
5-
<Context value={theme}>
6-
<Page />
7-
</Context>
8-
);
9-
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
const theme = trpc.useContext();
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +0,0 @@
1-
function Component({
2-
appUrl,
3-
}: {
4-
appUrl: string;
5-
}) {
6-
const theme = trpc.useContext();
7-
return <div />;
8-
};

transforms/remove-context-provider.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export default function transform(
77
const j = api.jscodeshift;
88
const root = j(file.source);
99

10+
let isDirty = false;
11+
1012
root.findJSXElements().forEach((elementPath) => {
1113
const { value } = elementPath;
1214
const elements = [value.openingElement, value.closingElement];
@@ -29,9 +31,10 @@ export default function transform(
2931
propertyName === 'Provider'
3032
) {
3133
element.name = element.name.object;
34+
isDirty = true;
3235
}
3336
});
3437
});
3538

36-
return root.toSource();
37-
};
39+
return isDirty ? root.toSource() : undefined;
40+
}

transforms/remove-forward-ref.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export default function transform(file: FileInfo, api: API) {
9797

9898
const root = j(file.source);
9999

100-
let dirtyFlag = false;
100+
let isDirty = false;
101101

102102
root
103103
.find(j.CallExpression, {
@@ -148,6 +148,8 @@ export default function transform(file: FileInfo, api: API) {
148148
value: j.identifier(refArgName),
149149
}),
150150
);
151+
152+
isDirty = true;
151153
}
152154

153155
// if props arg is Identifier, push ref variable declaration to the function body
@@ -157,6 +159,8 @@ export default function transform(file: FileInfo, api: API) {
157159
refArg.name,
158160
propsArg.name,
159161
);
162+
163+
isDirty = true;
160164
}
161165

162166
/**
@@ -175,6 +179,7 @@ export default function transform(file: FileInfo, api: API) {
175179
propsArgTypeReference,
176180
refArgTypeReference,
177181
);
182+
isDirty = true;
178183
}
179184

180185
/**
@@ -198,17 +203,18 @@ export default function transform(file: FileInfo, api: API) {
198203
) {
199204
renderFunction.params[0].typeAnnotation =
200205
buildPropsAndRefIntersectionTypeAnnotation(j, propType, refType);
206+
207+
isDirty = true;
201208
}
202209
}
203210

204-
dirtyFlag = true;
205211
return renderFunction;
206212
});
207213

208214
/**
209215
* handle import
210216
*/
211-
if (dirtyFlag) {
217+
if (isDirty) {
212218
root
213219
.find(j.ImportDeclaration, {
214220
source: {
@@ -222,24 +228,19 @@ export default function transform(file: FileInfo, api: API) {
222228
return;
223229
}
224230

225-
if (specifiers === undefined) {
226-
return;
227-
}
231+
const specifiersWithoutForwardRef =
232+
specifiers?.filter(
233+
(s) =>
234+
j.ImportSpecifier.check(s) && s.imported.name !== 'forwardRef',
235+
) ?? [];
228236

229-
const forwardRefImportSpecifierIndex = specifiers.findIndex(
230-
(s) => j.ImportSpecifier.check(s) && s.imported.name === 'forwardRef',
231-
);
237+
if (specifiersWithoutForwardRef.length === 0) {
238+
j(importDeclarationPath).remove();
239+
}
232240

233-
specifiers.splice(forwardRefImportSpecifierIndex, 1);
234-
})
235-
.filter((importDeclarationPath) => {
236-
const { specifiers } = importDeclarationPath.node;
237-
// remove the import if there are no more specifiers left after removing forwardRef
238-
return specifiers === undefined || specifiers.length === 0;
239-
})
240-
.remove();
241+
importDeclarationPath.node.specifiers = specifiersWithoutForwardRef;
242+
});
241243
}
242244

243-
return root.toSource();
245+
return isDirty ? root.toSource() : undefined;
244246
}
245-

transforms/remove-memoization-hooks.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ export default function transform(
77
const j = api.jscodeshift;
88
const root = j(file.source);
99

10+
let isDirty = false;
11+
1012
const hooksToRemove = ['useMemo', 'useCallback', 'memo'];
1113

1214
root.find(j.ImportDeclaration).forEach((path) => {
1315
if (path.node.specifiers?.length === 0) {
1416
return;
1517
}
1618

17-
const specifiers = path.node.specifiers?.filter((specifier) => {
18-
if (specifier.type === 'ImportSpecifier') {
19-
return !hooksToRemove.includes(specifier.imported.name);
20-
}
21-
return specifier;
22-
}) ?? [];
19+
const specifiers =
20+
path.node.specifiers?.filter((specifier) => {
21+
if (specifier.type === 'ImportSpecifier') {
22+
return !hooksToRemove.includes(specifier.imported.name);
23+
}
24+
return specifier;
25+
}) ?? [];
2326

2427
if (specifiers.length === 0) {
2528
j(path).remove();
@@ -36,7 +39,10 @@ export default function transform(
3639
name: hook,
3740
},
3841
})
39-
.replaceWith((path) => path.value.arguments[0]);
42+
.replaceWith((path) => {
43+
isDirty = true;
44+
return path.value.arguments[0];
45+
});
4046
});
4147

4248
hooksToRemove.forEach((hook) => {
@@ -48,8 +54,11 @@ export default function transform(
4854
property: { name: hook },
4955
},
5056
})
51-
.replaceWith((path) => path.value.arguments[0]);
57+
.replaceWith((path) => {
58+
isDirty = true;
59+
return path.value.arguments[0];
60+
});
5261
});
5362

54-
return root.toSource();
63+
return isDirty ? root.toSource() : undefined;
5564
}

transforms/use-context-hook.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ export default function transform(
88
const j = api.jscodeshift;
99
const root = j(file.source);
1010

11+
let isDirty = false;
12+
1113
// Get default import from react
12-
const defaultReactImport = root
13-
.find(j.ImportDeclaration, {
14-
source: { value: 'react' },
15-
specifiers: [{ type: 'ImportDefaultSpecifier' }],
16-
})
17-
.paths()
18-
.at(0)
19-
?.node.specifiers?.at(0)?.local?.name ?? 'React';
14+
const defaultReactImport =
15+
root
16+
.find(j.ImportDeclaration, {
17+
source: { value: 'react' },
18+
specifiers: [{ type: 'ImportDefaultSpecifier' }],
19+
})
20+
.paths()
21+
.at(0)
22+
?.node.specifiers?.at(0)?.local?.name ?? 'React';
2023

2124
// For usages like `import React from 'react'; React.useContext(ThemeContext)`
2225
root
@@ -32,7 +35,8 @@ export default function transform(
3235

3336
const newIdentifier = j.identifier.from({ name: 'use' });
3437

35-
identifierPath?.replace(newIdentifier);
38+
identifierPath?.replace(newIdentifier);
39+
isDirty = true;
3640
});
3741

3842
// Get useContext import name
@@ -60,9 +64,11 @@ export default function transform(
6064
const newIdentifier = j.identifier.from({ name: 'use' });
6165

6266
path.replace(newIdentifier);
67+
68+
isDirty = true;
6369
}
6470
});
6571
}
6672

67-
return root.toSource();
73+
return isDirty ? root.toSource() : undefined;
6874
}

0 commit comments

Comments
 (0)