|
| 1 | +import { Node } from 'vue-eslint-parser/ast/nodes' |
| 2 | +import * as OperationUtils from '../src/operationUtils' |
| 3 | +import type { Operation } from '../src/operationUtils' |
| 4 | +import type { VueASTTransformation } from '../src/wrapVueTransformation' |
| 5 | +import * as parser from 'vue-eslint-parser' |
| 6 | +import wrap from '../src/wrapVueTransformation' |
| 7 | + |
| 8 | +export const transformAST: VueASTTransformation = (context) => { |
| 9 | + var fixOperations: Operation[] = [] |
| 10 | + const toFixNodes: Node[] = findNodes(context) |
| 11 | + toFixNodes.forEach((node) => { |
| 12 | + fixOperations = fixOperations.concat(fix(node)) |
| 13 | + }) |
| 14 | + return fixOperations |
| 15 | +} |
| 16 | + |
| 17 | +export default wrap(transformAST) |
| 18 | + |
| 19 | +function findNodes(context: any): Node[] { |
| 20 | + const { file } = context |
| 21 | + const source = file.source |
| 22 | + const options = { sourceType: 'module' } |
| 23 | + const ast = parser.parse(source, options) |
| 24 | + var toFixNodes: Node[] = [] |
| 25 | + var root: Node = <Node>ast.templateBody |
| 26 | + parser.AST.traverseNodes(root, { |
| 27 | + enterNode(node: Node) { |
| 28 | + if ( |
| 29 | + node.type === 'VAttribute' && |
| 30 | + node.key.type === 'VDirectiveKey' && |
| 31 | + node.key.name.name === 'slot' && |
| 32 | + node.key.argument?.type === 'VIdentifier' && |
| 33 | + node.key.argument?.name === 'default' && |
| 34 | + node.parent.parent.type == 'VElement' && |
| 35 | + node.parent.parent.name == 'template' |
| 36 | + ) { |
| 37 | + toFixNodes.push(node) |
| 38 | + } |
| 39 | + }, |
| 40 | + leaveNode(node: Node) {}, |
| 41 | + }) |
| 42 | + return toFixNodes |
| 43 | +} |
| 44 | + |
| 45 | +function fix(node: Node): Operation[] { |
| 46 | + var fixOperations: Operation[] = [] |
| 47 | + |
| 48 | + const target: any = node!.parent!.parent |
| 49 | + const targetParent: any = target.parent |
| 50 | + |
| 51 | + targetParent.children |
| 52 | + .filter((el: any) => el.type == 'VElement' && el.name != 'template') |
| 53 | + .forEach((element: any) => { |
| 54 | + fixOperations.push(OperationUtils.remove(element)) |
| 55 | + }) |
| 56 | + return fixOperations |
| 57 | +} |
0 commit comments