Skip to content

Commit

Permalink
refactor: improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Aug 1, 2023
1 parent 86ba932 commit 768fb3e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"test": "vitest",
"lint": "eslint --cache .",
"format": "prettier --write .",
"typecheck": "tsc --noEmit",
"typecheck": "tsc",
"release": "bumpp -r"
},
"license": "MIT",
Expand Down
2 changes: 0 additions & 2 deletions packages/babel-plugin-jsx/index.html

This file was deleted.

6 changes: 3 additions & 3 deletions packages/babel-plugin-jsx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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>();

Expand Down
10 changes: 6 additions & 4 deletions packages/babel-plugin-jsx/src/sugar-fragment.ts
Original file line number Diff line number Diff line change
@@ -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') || [];
Expand All @@ -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(
Expand All @@ -34,3 +34,5 @@ export default {
},
},
};

export default visitor;
8 changes: 5 additions & 3 deletions packages/babel-plugin-jsx/src/transform-vue-jsx.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"compilerOptions": {
"sourceMap": true,
"target": "ESNext",
"module": "ESNext",
"lib": ["ES2015", "DOM", "DOM.Iterable"],
Expand All @@ -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"]
}

0 comments on commit 768fb3e

Please sign in to comment.