Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
add code for ast generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mubaidr committed Dec 6, 2019
1 parent fbb8a68 commit 8b0bd34
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 4 deletions.
3 changes: 2 additions & 1 deletion __tests__/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"plugins": ["jest", "@typescript-eslint"],
"rules": {
"@typescript-eslint/no-explicit-any": 0
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/explicit-function-return-type": 0
}
}
2 changes: 1 addition & 1 deletion __tests__/lib/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as lib from '../../src/lib'
import * as lib from '../../src/'

describe('vue2-migration-helper', () => {
test('should be defined', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/no-explicit-any": 0
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/explicit-function-return-type": 0
}
}
47 changes: 46 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
export default {}
import { types } from '@babel/core'
import traverse from '@babel/traverse'

import { getAst } from './lib/ast-utilities'
import { getTemplate } from './lib/template-utilities'

// TODO: add reactive properties definitions
// TODO: convert computed syntax
// TODO: convert watch syntax
// TODO: convert methods syntax
// TODO: convert life-cycle hooks
// TODO: convert props syntax
// TODO: update this.$event usage

export async function vue2MigrationHelper(options: {
path: string
}): Promise<void> {
const template = await getTemplate(options.path)
const ast = getAst(template.script)

traverse(ast, {
enter(path) {
const { type } = path.node

if (type === 'Program') {
// insert vue 3 imports
const importNode = types.importDeclaration(
[
types.importSpecifier(
types.identifier('ref'),
types.identifier('ref')
)
// types.importSpecifier('watch'),
// types.importSpecifier('computed'),
// types.importSpecifier('onMounted '),
],
types.stringLiteral('vue')
)

// path.node.body.unshift(types.importNode(importNode))

console.log(path.node)
}
}
})
}
20 changes: 20 additions & 0 deletions src/lib/ast-utilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import babelParser from '@babel/parser'

export function getAst(source: string) {
const ast = babelParser.parse(source, {
sourceType: 'module',
plugins: [
'typescript',
'dynamicImport',
'objectRestSpread',
'logicalAssignment',
'estree'
]
})

return ast
}

export default {
getAst
}
21 changes: 21 additions & 0 deletions src/lib/template-utilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import fs from 'fs'
import xml2js from 'xml2js'

export async function getTemplate(path: string) {
const source = fs.readFileSync(path, 'utf-8')
const template: {
root: {
template: string[]
style: string[]
script: string[]
}
} = await xml2js.parseStringPromise(`<root>${source}</root>`)

return {
template: template.root.template.join(''),
script: template.root.script.join(''),
style: template.root.style.join('')
}
}

export default { getTemplate }

0 comments on commit 8b0bd34

Please sign in to comment.