Skip to content

Commit dc0e124

Browse files
committed
fix(features): fix magicast function body manipulation
magicast's $body.push() doesn't work for function-expression types. Created addStatementToFunctionBody helper that directly manipulates the AST to properly add plugin registrations to registerPlugins.
1 parent 7a9e54e commit dc0e124

4 files changed

Lines changed: 38 additions & 8 deletions

File tree

packages/shared/src/features/i18n.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { writeFile } from 'node:fs/promises'
44
import { join } from 'node:path'
55
import { loadFile } from 'magicast'
66
import { getDefaultExportOptions } from 'magicast/helpers'
7+
import { addStatementToFunctionBody, isFunction } from '../utils/magicast'
78
import rootPkg from './dependencies/package.json' with { type: 'json' }
89

910
export const i18n: Feature = {
@@ -56,8 +57,8 @@ export const i18n: Feature = {
5657
})
5758

5859
const registerPlugins = mod.exports.registerPlugins
59-
if (registerPlugins && registerPlugins.$type === 'function') {
60-
registerPlugins.$body.push('app.use(i18n)')
60+
if (isFunction(registerPlugins)) {
61+
addStatementToFunctionBody(registerPlugins, 'app.use(i18n)')
6162
}
6263

6364
await writeFile(pluginsPath, mod.generate().code)

packages/shared/src/features/pinia.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { writeFile } from 'node:fs/promises'
44
import { join } from 'node:path'
55
import { loadFile } from 'magicast'
66
import { getDefaultExportOptions } from 'magicast/helpers'
7+
import { addStatementToFunctionBody, isFunction } from '../utils/magicast'
78
import rootPkg from './dependencies/package.json' with { type: 'json' }
89

910
export const pinia: Feature = {
@@ -62,8 +63,8 @@ export const pinia: Feature = {
6263
})
6364

6465
const registerPlugins = mod.exports.registerPlugins
65-
if (registerPlugins && registerPlugins.$type === 'function') {
66-
registerPlugins.$body.push('app.use(createPinia())')
66+
if (isFunction(registerPlugins)) {
67+
addStatementToFunctionBody(registerPlugins, 'app.use(createPinia())')
6768
}
6869

6970
await writeFile(pluginsPath, mod.generate().code)

packages/shared/src/features/router.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { writeFile } from 'node:fs/promises'
44
import { join } from 'node:path'
55
import { builders, loadFile } from 'magicast'
66
import { installFeature } from '../utils/installFeature'
7+
import { addStatementToFunctionBody, isFunction } from '../utils/magicast'
78
import rootPkg from './dependencies/package.json' with { type: 'json' }
89

910
export const router: Feature = {
@@ -33,8 +34,8 @@ export const router: Feature = {
3334
})
3435

3536
const registerPlugins = mod.exports.registerPlugins
36-
if (registerPlugins && registerPlugins.$type === 'function') {
37-
registerPlugins.$body.push('app.use(router)')
37+
if (isFunction(registerPlugins)) {
38+
addStatementToFunctionBody(registerPlugins, 'app.use(router)')
3839
}
3940

4041
await writeFile(pluginsPath, mod.generate().code)
@@ -70,8 +71,8 @@ export const fileRouter: Feature = {
7071
})
7172

7273
const registerPlugins = mod.exports.registerPlugins
73-
if (registerPlugins && registerPlugins.$type === 'function') {
74-
registerPlugins.$body.push('app.use(router)')
74+
if (isFunction(registerPlugins)) {
75+
addStatementToFunctionBody(registerPlugins, 'app.use(router)')
7576
}
7677

7778
await writeFile(pluginsPath, mod.generate().code)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { parseExpression } from 'magicast'
2+
3+
/**
4+
* Adds a statement to a function body using AST manipulation.
5+
* magicast's $body.push() doesn't work for function-expression types,
6+
* so we need to manipulate the AST directly.
7+
*/
8+
export function addStatementToFunctionBody (fn: any, statement: string) {
9+
if (!fn || !fn.$ast?.body?.body) return false
10+
11+
const expr = parseExpression(statement)
12+
const newStatement = {
13+
type: 'ExpressionStatement',
14+
expression: expr.$ast,
15+
loc: null,
16+
}
17+
18+
fn.$ast.body.body.push(newStatement)
19+
return true
20+
}
21+
22+
/**
23+
* Checks if the export is a function (either 'function' or 'function-expression')
24+
*/
25+
export function isFunction (fn: any): boolean {
26+
return fn && (fn.$type === 'function' || fn.$type === 'function-expression')
27+
}

0 commit comments

Comments
 (0)