refactor: improve types

This commit is contained in:
三咲智子 Kevin Deng 2023-08-01 15:00:51 +08:00
parent 86ba932a03
commit 768fb3ec2a
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
6 changed files with 18 additions and 16 deletions

View File

@ -6,7 +6,7 @@
"test": "vitest",
"lint": "eslint --cache .",
"format": "prettier --write .",
"typecheck": "tsc --noEmit",
"typecheck": "tsc",
"release": "bumpp -r"
},
"license": "MIT",

View File

@ -1,2 +0,0 @@
<div id="app"></div>
<script src="/dist/main.js"></script>

View File

@ -31,14 +31,14 @@ const hasJSX = (parentPath: NodePath<t.Program>) => {
const JSX_ANNOTATION_REGEX = /\*?\s*@jsx\s+([^\s]+)/;
export default ({ types }: typeof BabelCore) => ({
export default ({ types }: typeof BabelCore): BabelCore.PluginObj<State> => ({
name: 'babel-plugin-jsx',
inherits: syntaxJsx,
visitor: {
...transformVueJSX,
...sugarFragment,
Program: {
enter(path: NodePath<t.Program>, state: State) {
enter(path, state) {
if (hasJSX(path)) {
const importNames = [
'createVNode',
@ -168,7 +168,7 @@ export default ({ types }: typeof BabelCore) => ({
}
}
},
exit(path: NodePath<t.Program>) {
exit(path) {
const body = path.get('body') as NodePath[];
const specifiersMap = new Map<string, t.ImportSpecifier>();

View File

@ -1,10 +1,10 @@
import * as t from '@babel/types';
import { type NodePath } from '@babel/traverse';
import { type NodePath, type Visitor } from '@babel/traverse';
import type { State } from './interface';
import { FRAGMENT, createIdentifier } from './utils';
const transformFragment = (
path: NodePath<t.JSXElement>,
path: NodePath<t.JSXFragment>,
Fragment: t.JSXIdentifier | t.JSXMemberExpression
) => {
const children = path.get('children') || [];
@ -16,9 +16,9 @@ const transformFragment = (
);
};
export default {
const visitor: Visitor<State> = {
JSXFragment: {
enter(path: NodePath<t.JSXElement>, state: State) {
enter(path, state) {
const fragmentCallee = createIdentifier(state, FRAGMENT);
path.replaceWith(
transformFragment(
@ -34,3 +34,5 @@ export default {
},
},
};
export default visitor;

View File

@ -1,5 +1,5 @@
import * as t from '@babel/types';
import { type NodePath } from '@babel/traverse';
import { type NodePath, type Visitor } from '@babel/traverse';
// @ts-expect-error
import { addDefault } from '@babel/helper-module-imports';
import {
@ -559,10 +559,12 @@ const transformJSXElement = (
]);
};
export default {
const visitor: Visitor<State> = {
JSXElement: {
exit(path: NodePath<t.JSXElement>, state: State) {
exit(path, state) {
path.replaceWith(transformJSXElement(path, state));
},
},
};
export default visitor;

View File

@ -1,6 +1,5 @@
{
"compilerOptions": {
"sourceMap": true,
"target": "ESNext",
"module": "ESNext",
"lib": ["ES2015", "DOM", "DOM.Iterable"],
@ -10,13 +9,14 @@
"noUnusedLocals": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"removeComments": false,
"jsx": "preserve",
"types": ["vitest/globals"],
"skipLibCheck": true,
"paths": {
"@vue/babel-plugin-jsx": ["./packages/babel-plugin-jsx/src"]
}
},
"noEmit": true,
"incremental": true
},
"include": ["packages/*/src", "packages/*/test"]
}