Skip to content

Commit a433902

Browse files
committed
feat: add emits declaration
1 parent 121c571 commit a433902

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

Diff for: generator/codemods/vue/add-emit-declaration.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @param {Object} context
3+
* @param {import('jscodeshift').JSCodeshift} context.j
4+
* @param {ReturnType<import('jscodeshift').Core>} context.root
5+
*/
6+
module.exports = function addEmitDeclaration(context) {
7+
const { j, root } = context
8+
9+
// this.$emit('xxx') => emits: ['xxx']
10+
const this$emits = root.find(j.CallExpression, {
11+
callee: {
12+
type: 'MemberExpression',
13+
object: { type: 'ThisExpression' },
14+
property: {
15+
type: 'Identifier',
16+
name: '$emit'
17+
}
18+
}
19+
})
20+
21+
const emits = []
22+
for (let i = 0; i < this$emits.length; i++) {
23+
const arg = this$emits.at(i).get().node.arguments[0]
24+
if (arg.type === 'StringLiteral') {
25+
emits.push(arg.value)
26+
}
27+
}
28+
29+
if (emits.length === 0) {
30+
return
31+
}
32+
33+
const defaultObject = root
34+
.find(j.ExportDefaultDeclaration)
35+
.at(0)
36+
.find(j.ObjectExpression)
37+
.at(0)
38+
39+
let oldEmits = emits
40+
let emitsProperty = defaultObject.find(j.ObjectProperty, {
41+
key: {
42+
type: 'Identifier',
43+
name: 'emits'
44+
}
45+
})
46+
if (emitsProperty.length > 0) {
47+
oldEmits = emitsProperty
48+
.at(0)
49+
.get()
50+
.node.value.elements.map(el => el.value)
51+
52+
let hasChange = false
53+
for (const el of emits) {
54+
if (!oldEmits.includes(el)) {
55+
oldEmits.push(el)
56+
hasChange = true
57+
}
58+
}
59+
if (!hasChange) {
60+
return
61+
}
62+
emitsProperty.remove()
63+
}
64+
65+
defaultObject.replaceWith(({ node }) => {
66+
node.properties.unshift(
67+
j.objectProperty(
68+
j.identifier('emits'),
69+
j.arrayExpression(oldEmits.map(el => j.stringLiteral(el)))
70+
)
71+
)
72+
return node
73+
})
74+
}

Diff for: generator/codemods/vue/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/** @type {import('jscodeshift').Transform} */
2+
module.exports = function(fileInfo, api) {
3+
const j = api.jscodeshift
4+
const root = j(fileInfo.source)
5+
const context = { j, root }
6+
7+
require('./add-emit-declaration')(context)
8+
9+
return root.toSource({ lineTerminator: '\n' })
10+
}
11+
12+
module.exports.parser = 'babylon'

Diff for: generator/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ module.exports = (api) => {
2222
const globalAPITransform = require('./codemods/global-api')
2323
api.transformScript(api.entryFile, globalAPITransform)
2424

25+
const vueTransform = require('./codemods/vue')
26+
const vueFiles = Object.keys(api.generator.files).filter(el =>
27+
el.endsWith('.vue')
28+
)
29+
for (let i = 0; i < vueFiles.length; i++) {
30+
api.transformScript(vueFiles[i], vueTransform)
31+
}
32+
2533
if (api.hasPlugin('eslint')) {
2634
api.extendPackage({
2735
devDependencies: {

0 commit comments

Comments
 (0)