Skip to content

Commit

Permalink
feat(code-gen): insert new line
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Jul 5, 2023
1 parent 0b6bb47 commit c4f59f7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
11 changes: 10 additions & 1 deletion __tests__/code-gen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ test('exports loose source and re named exported name', async (t) => {
t.is(res.code, 'export const version = Vue.version;\nexport default Vue.version;')
})

test('exports loose source and export self module', async (t) => {
const code = 'import { version , ref } from \'vue\';\n const t = \'nonzzz\';\n export { t, version, ref as default };'
const scanner = createScanner(['vue'])
await scanner.scanAllDependencies()
const codeGen = createCodeGenerator()
codeGen.injectDependencies(scanner.dependencies)
const res = await codeGen.transform(code)
t.is(res.code, 'const t = \'nonzzz\';\nexport { t };\nconst version = Vue.version;\nexport default Vue.ref;')
})

test('exports with source', async (t) => {
const code = 'export { ref , version } from \'vue\''
const scanner = createScanner(['vue'])
Expand All @@ -79,7 +89,6 @@ test('exports with source and re named local name', async (t) => {
enter: (path) => {
path.node.key.type === 'Identifier' && keys.add(path.node.key.name)
}

}
})
t.is(keys.size, scanner.dependencies.get('vue').bindings.size)
Expand Down
14 changes: 9 additions & 5 deletions src/code-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,21 @@ export class CodeGen {
// export {default as A } from 'module'
// export { B as default } from 'module'
// export { A , B } from 'module'
// const cap = len(path.node.specifiers)

const variableDeclaratorNodes = nodes.filter((node):node is t.VariableDeclarator => node.type === 'VariableDeclarator')
const objectOrMemberExpression = nodes.filter((node):node is t.ObjectExpression | t.MemberExpression => node.type !== 'VariableDeclarator')
if (objectOrMemberExpression.length) {
if (len(objectOrMemberExpression)) {
const exportDefaultDeclaration = t.exportDefaultDeclaration(objectOrMemberExpression[0])
path.insertAfter(exportDefaultDeclaration)
}
if (variableDeclaratorNodes.length) {
if (len(variableDeclaratorNodes)) {
const variableDeclaration = t.variableDeclaration('const', variableDeclaratorNodes)
path.replaceWith(t.exportNamedDeclaration(variableDeclaration, []))
if (len(natives)) {
const exportNamedDeclaration = t.exportNamedDeclaration(null, natives)
path.replaceWith(exportNamedDeclaration)
path.insertAfter(variableDeclaration)
} else {
path.replaceWith(t.exportNamedDeclaration(variableDeclaration, []))
}
}
}

Expand Down

0 comments on commit c4f59f7

Please sign in to comment.