Skip to content

Commit

Permalink
feat: support new compostion api spec
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Aug 1, 2020
1 parent 9013a09 commit 7d26539
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export function convertASTResultToImport (astResults: ASTResult<ts.Node>[], opti
if (options.compatible && importMap.has('@vue/composition-api')) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const temp = importMap.get('@vue/composition-api')!
temp.named.add('createComponent')
temp.named.add('defineComponent')
importMap.set('@vue/composition-api', temp)
}

Expand Down Expand Up @@ -252,7 +252,7 @@ export function runPlugins (
log('Make default export object')
const exportDefaultExpr = (options.compatible)
? tsModule.createCall(
tsModule.createIdentifier('createComponent'),
tsModule.createIdentifier('defineComponent'),
undefined,
[tsModule.createObjectLiteral(
[
Expand Down
28 changes: 2 additions & 26 deletions src/plugins/vue-property-decorator/Watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,10 @@ export const convertWatch: ASTConverter<ts.MethodDeclaration> = (node, options)
)
const watchOptions: ts.PropertyAssignment[] = []
if (tsModule.isObjectLiteralExpression(watchArguments)) {
let hasLazy = false
watchArguments.properties.forEach((el) => {
if (tsModule.isPropertyAssignment(el)) {
if (el.name.getText() === 'immediate') {
if (el.initializer.kind === tsModule.SyntaxKind.TrueKeyword || el.initializer.kind === tsModule.SyntaxKind.FalseKeyword) {
hasLazy = true
const valueFn = (el.initializer.kind === tsModule.SyntaxKind.TrueKeyword) ? tsModule.createFalse : tsModule.createTrue
watchOptions.push(
tsModule.createPropertyAssignment(
tsModule.createIdentifier('lazy'),
valueFn()
)
)
}
} else {
watchOptions.push(el)
}
}
if (!tsModule.isPropertyAssignment(el)) return
watchOptions.push(el)
})

if (!hasLazy) {
watchOptions.push(
tsModule.createPropertyAssignment(
tsModule.createIdentifier('lazy'),
tsModule.createTrue()
)
)
}
}

return {
Expand Down
14 changes: 7 additions & 7 deletions tests/__snapshots__/testTSFile.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ exports[`testTSFile compatible 1`] = `
ref,
watch,
onMounted,
createComponent
defineComponent
} from '@vue/composition-api'
/**
* My basic tag
*/
export default createComponent({
export default defineComponent({
model: {
prop: 'checked',
event: 'change'
Expand Down Expand Up @@ -77,7 +77,7 @@ export default createComponent({
console.log(val, newVal)
console.log(anotherComponent.value!)
},
{ deep: true, lazy: false }
{ deep: true, immediate: true }
)
onMounted(() => {
click('oao')
Expand Down Expand Up @@ -128,13 +128,13 @@ exports[`testTSFile compatible and ts config file: stdout 1`] = `
ref,
watch,
onMounted,
createComponent
defineComponent
} from '@vue/composition-api'
/**
* My basic tag
*/
export default createComponent({
export default defineComponent({
model: {
prop: 'checked',
event: 'change'
Expand Down Expand Up @@ -197,7 +197,7 @@ export default createComponent({
console.log(val, newVal)
console.log(anotherComponent.value!)
},
{ deep: true, lazy: false }
{ deep: true, immediate: true }
)
onMounted(() => {
click('oao')
Expand Down Expand Up @@ -308,7 +308,7 @@ export default {
console.log(val, newVal)
console.log(anotherComponent.value!)
},
{ deep: true, lazy: false }
{ deep: true, immediate: true }
)
onMounted(() => {
click('oao')
Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/testVueFile.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ exports[`testVueFile compatible 1`] = `
ref,
provide,
inject,
createComponent
defineComponent
} from '@vue/composition-api'
const symbol = Symbol('baz')
/**
* My basic tag
*/
export default createComponent({
export default defineComponent({
model: {
prop: 'checked',
event: 'change'
Expand Down

0 comments on commit 7d26539

Please sign in to comment.