Skip to content

Commit 5f48c4e

Browse files
authored
Fix middleware codemod (#106)
1 parent 7d20a3c commit 5f48c4e

File tree

8 files changed

+46
-60
lines changed

8 files changed

+46
-60
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/application/project/code/transformation/javascript/nextJsMiddlewareCodemod.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ export type MiddlewareConfiguration = {
2525
module: string,
2626
middlewareFactoryName: string,
2727
middlewareName: string,
28-
configName: string,
29-
matcherName: string,
3028
},
3129
};
3230

@@ -48,37 +46,45 @@ export class NextJsMiddlewareCodemod implements Codemod<t.File> {
4846
public apply(input: t.File): Promise<ResultCode<t.File>> {
4947
const {body} = input.program;
5048

51-
const isConfigReexported = hasReexport(input, {
52-
moduleName: this.configuration.import.module,
53-
importName: this.configuration.import.configName,
54-
});
55-
5649
const isMiddlewareReexported = hasReexport(input, {
5750
moduleName: this.configuration.import.module,
5851
importName: this.configuration.import.middlewareName,
5952
});
6053

61-
const localConfig = isConfigReexported ? null : NextJsMiddlewareCodemod.findConfig(input);
54+
const localConfig = NextJsMiddlewareCodemod.findConfig(input);
55+
56+
const addConfigExport = (): void => {
57+
// Add a configuration object with the matcher pattern
58+
body.push(t.exportNamedDeclaration(
59+
t.variableDeclaration('const', [
60+
t.variableDeclarator(
61+
t.identifier('config'),
62+
t.objectExpression([
63+
t.objectProperty(
64+
t.identifier('matcher'),
65+
t.stringLiteral(this.configuration.matcherPattern),
66+
),
67+
]),
68+
),
69+
]),
70+
));
71+
};
6272

6373
if (isMiddlewareReexported) {
64-
if (isConfigReexported || localConfig !== null) {
65-
// Middleware is re-exported and config is re-exported or found in the source code,
74+
if (localConfig !== null) {
75+
// Middleware is re-exported and config found in the source code,
6676
// consider the middleware configured
6777
return Promise.resolve({
6878
modified: false,
6979
result: input,
7080
});
7181
}
7282

73-
// No config found in the source code, add a re-export
74-
const modified = addReexport(input, {
75-
type: 'value',
76-
moduleName: this.configuration.import.module,
77-
importName: this.configuration.import.configName,
78-
});
83+
// Add a configuration object with the matcher pattern
84+
addConfigExport();
7985

8086
return Promise.resolve({
81-
modified: modified,
87+
modified: true,
8288
result: input,
8389
});
8490
}
@@ -120,21 +126,17 @@ export class NextJsMiddlewareCodemod implements Codemod<t.File> {
120126
if (middlewareNode === null) {
121127
if (localConfig === null) {
122128
// No middleware found or configuration object,
123-
// just add re-exports
124-
const middlewareReexport = addReexport(input, {
129+
// just add middleware re-export
130+
addReexport(input, {
125131
type: 'value',
126132
moduleName: this.configuration.import.module,
127133
importName: this.configuration.import.middlewareName,
128134
});
129135

130-
const configReexport = addReexport(input, {
131-
type: 'value',
132-
moduleName: this.configuration.import.module,
133-
importName: this.configuration.import.configName,
134-
});
136+
addConfigExport();
135137

136138
return Promise.resolve({
137-
modified: middlewareReexport || configReexport,
139+
modified: true,
138140
result: input,
139141
});
140142
}
@@ -149,7 +151,7 @@ export class NextJsMiddlewareCodemod implements Codemod<t.File> {
149151
t.identifier('matcher'),
150152
t.memberExpression(
151153
t.identifier(localConfig.name),
152-
t.identifier(this.configuration.import.matcherName),
154+
t.identifier('matcher'),
153155
),
154156
),
155157
]),

src/infrastructure/application/cli/cli.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,8 +1793,6 @@ export class Cli {
17931793
module: '@croct/plug-next/middleware',
17941794
middlewareName: 'middleware',
17951795
middlewareFactoryName: 'withCroct',
1796-
configName: 'config',
1797-
matcherName: 'matcher',
17981796
},
17991797
}),
18001798
}),

test/application/project/code/transformation/fixtures/nextjs-middleware/existingConfigReexport.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/application/project/code/transformation/fixtures/nextjs-middleware/existingLocalConfigAndMiddlewareReexport.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/application/project/code/transformation/fixtures/nextjs-middleware/existingMiddlewareAndConfigReexport.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/application/project/code/transformation/javascript/__snapshots__/nextJsMiddlewareCodemod.test.ts.snap

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,11 @@ exports[`NextJsMiddlewareCodemod should correctly transform defaultExportClass.t
272272
// invalid
273273
}
274274
275-
export { middleware, config } from "@croct/plug-next/middleware";
275+
export { middleware } from "@croct/plug-next/middleware";
276+
277+
export const config = {
278+
matcher: "/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)"
279+
};
276280
"
277281
`;
278282

@@ -319,7 +323,13 @@ export default something;
319323
"
320324
`;
321325

322-
exports[`NextJsMiddlewareCodemod should correctly transform empty.ts: empty.ts 1`] = `"export { middleware, config } from "@croct/plug-next/middleware";"`;
326+
exports[`NextJsMiddlewareCodemod should correctly transform empty.ts: empty.ts 1`] = `
327+
"export { middleware } from "@croct/plug-next/middleware";
328+
329+
export const config = {
330+
matcher: "/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)"
331+
};"
332+
`;
323333

324334
exports[`NextJsMiddlewareCodemod should correctly transform existingAliasedHofCall.ts: existingAliasedHofCall.ts 1`] = `
325335
"import { withCroct as croctMiddleware } from "@croct/plug-next/middleware";
@@ -381,11 +391,6 @@ export default withCroct({
381391
"
382392
`;
383393

384-
exports[`NextJsMiddlewareCodemod should correctly transform existingConfigReexport.ts: existingConfigReexport.ts 1`] = `
385-
"export { config, middleware } from "@croct/plug-next/middleware";
386-
"
387-
`;
388-
389394
exports[`NextJsMiddlewareCodemod should correctly transform existingHofCall.ts: existingHofCall.ts 1`] = `
390395
"import { withCroct } from "@croct/plug-next/middleware";
391396
@@ -435,20 +440,6 @@ export const middleware = croctMiddleware({
435440
"
436441
`;
437442

438-
exports[`NextJsMiddlewareCodemod should correctly transform existingLocalConfigAndMiddlewareReexport.ts: existingLocalConfigAndMiddlewareReexport.ts 1`] = `
439-
"export { middleware } from "@croct/plug-next/middleware";
440-
441-
export const config = {
442-
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
443-
}
444-
"
445-
`;
446-
447-
exports[`NextJsMiddlewareCodemod should correctly transform existingMiddlewareAndConfigReexport.ts: existingMiddlewareAndConfigReexport.ts 1`] = `
448-
"export { middleware, config } from "@croct/plug-next/middleware";
449-
"
450-
`;
451-
452443
exports[`NextJsMiddlewareCodemod should correctly transform existingMiddlewareCall.ts: existingMiddlewareCall.ts 1`] = `
453444
"import { middleware as croctMiddleware } from "@croct/plug-next/middleware";
454445
@@ -459,7 +450,11 @@ export default croctMiddleware(function () {
459450
`;
460451

461452
exports[`NextJsMiddlewareCodemod should correctly transform existingMiddlewareReexport.ts: existingMiddlewareReexport.ts 1`] = `
462-
"export { middleware, config } from "@croct/plug-next/middleware";
453+
"export { middleware } from "@croct/plug-next/middleware";
454+
455+
export const config = {
456+
matcher: "/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)"
457+
};
463458
"
464459
`;
465460

test/application/project/code/transformation/javascript/nextJsMiddlewareCodemod.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ describe('NextJsMiddlewareCodemod', () => {
1313
module: '@croct/plug-next/middleware',
1414
middlewareFactoryName: 'withCroct',
1515
middlewareName: 'middleware',
16-
matcherName: 'matcher',
17-
configName: 'config',
1816
},
1917
};
2018

0 commit comments

Comments
 (0)