Skip to content

Commit 99adfaa

Browse files
committed
babel 8
1 parent e5169ff commit 99adfaa

File tree

14 files changed

+1339
-1009
lines changed

14 files changed

+1339
-1009
lines changed

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
"jsx"
2121
],
2222
"devDependencies": {
23-
"@babel/plugin-syntax-typescript": "^7.27.1",
23+
"@babel/plugin-syntax-typescript": "^8.0.0-beta.2",
24+
"@babel/plugin-syntax-jsx": "^8.0.0-beta.2",
2425
"@eslint/js": "^9.33.0",
2526
"@oxc-project/runtime": "^0.81.0",
2627
"@rollup/plugin-babel": "^6.0.4",
27-
"@types/babel__core": "^7.20.5",
28-
"@types/babel__helper-module-imports": "^7.18.3",
29-
"@types/babel__helper-plugin-utils": "^7.10.3",
3028
"@types/node": "^24.2.1",
3129
"@vitest/coverage-v8": "^3.2.4",
3230
"@vue/babel-plugin-jsx": "workspace:*",

packages/babel-plugin-jsx/package.json

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,25 @@
2727
"dist"
2828
],
2929
"dependencies": {
30-
"@babel/helper-module-imports": "^7.27.1",
31-
"@babel/helper-plugin-utils": "^7.27.1",
32-
"@babel/plugin-syntax-jsx": "^7.27.1",
33-
"@babel/template": "^7.27.2",
34-
"@babel/traverse": "^7.28.0",
35-
"@babel/types": "^7.28.2",
30+
"@babel/helper-module-imports": "^8.0.0-beta.2",
31+
"@babel/helper-plugin-utils": "^8.0.0-beta.2",
32+
"@babel/plugin-syntax-jsx": "^8.0.0-beta.2",
33+
"@babel/template": "^8.0.0-beta.2",
34+
"@babel/traverse": "^8.0.0-beta.2",
35+
"@babel/types": "^8.0.0-beta.2",
3636
"@vue/babel-helper-vue-transform-on": "workspace:*",
3737
"@vue/babel-plugin-resolve-type": "workspace:*",
3838
"@vue/shared": "^3.5.18"
3939
},
4040
"devDependencies": {
41-
"@babel/core": "^7.28.0",
42-
"@babel/preset-env": "^7.28.0",
43-
"@types/babel__template": "^7.4.4",
44-
"@types/babel__traverse": "^7.28.0",
41+
"@babel/core": "^8.0.0-beta.2",
42+
"@babel/preset-env": "^8.0.0-beta.2",
4543
"@vue/test-utils": "^2.4.6",
4644
"regenerator-runtime": "^0.14.1",
4745
"vue": "catalog:"
4846
},
4947
"peerDependencies": {
50-
"@babel/core": "^7.0.0-0"
48+
"@babel/core": "^8.0.0-beta.2"
5149
},
5250
"peerDependenciesMeta": {
5351
"@babel/core": {

packages/babel-plugin-jsx/src/index.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import * as t from '@babel/types';
2-
import type * as BabelCore from '@babel/core';
2+
import type { PluginAPI, PluginObject, PluginPass } from '@babel/core';
33
import _template from '@babel/template';
4-
// @ts-expect-error
54
import _syntaxJsx from '@babel/plugin-syntax-jsx';
65
import { addNamed, addNamespace, isModule } from '@babel/helper-module-imports';
7-
import { type NodePath, type Visitor } from '@babel/traverse';
6+
import type { NodePath, VisitorBase } from '@babel/traverse';
87
import ResolveType from '@vue/babel-plugin-resolve-type';
98
import { declare } from '@babel/helper-plugin-utils';
109
import transformVueJSX from './transform-vue-jsx';
@@ -14,20 +13,11 @@ import type { State, VueJSXPluginOptions } from './interface';
1413
export { VueJSXPluginOptions };
1514

1615
const hasJSX = (parentPath: NodePath<t.Program>) => {
17-
let fileHasJSX = false;
18-
parentPath.traverse({
19-
JSXElement(path) {
20-
// skip ts error
21-
fileHasJSX = true;
22-
path.stop();
23-
},
24-
JSXFragment(path) {
25-
fileHasJSX = true;
26-
path.stop();
27-
},
16+
return t.traverseFast(parentPath.node, (node) => {
17+
if (t.isJSXElement(node) || t.isJSXFragment(node)) {
18+
return t.traverseFast.stop;
19+
}
2820
});
29-
30-
return fileHasJSX;
3121
};
3222

3323
const JSX_ANNOTATION_REGEX = /\*?\s*@jsx\s+([^\s]+)/;
@@ -41,15 +31,15 @@ const syntaxJsx = /*#__PURE__*/ interopDefault(_syntaxJsx);
4131
const template = /*#__PURE__*/ interopDefault(_template);
4232

4333
const plugin: (
44-
api: object,
34+
api: PluginAPI,
4535
options: VueJSXPluginOptions | null | undefined,
4636
dirname: string
47-
) => BabelCore.PluginObj<State> = declare<
48-
VueJSXPluginOptions,
49-
BabelCore.PluginObj<State>
37+
) => PluginObject<State & PluginPass> = declare<
38+
State,
39+
VueJSXPluginOptions | null | undefined
5040
>((api, opt, dirname) => {
5141
const { types } = api;
52-
let resolveType: BabelCore.PluginObj<BabelCore.PluginPass> | undefined;
42+
let resolveType: PluginObject<PluginPass> | undefined;
5343
if (opt.resolveType) {
5444
if (typeof opt.resolveType === 'boolean') opt.resolveType = {};
5545
resolveType = ResolveType(api, opt.resolveType, dirname);
@@ -59,7 +49,7 @@ const plugin: (
5949
name: 'babel-plugin-jsx',
6050
inherits: /*#__PURE__*/ interopDefault(syntaxJsx),
6151
visitor: {
62-
...(resolveType?.visitor as Visitor<State>),
52+
...(resolveType?.visitor as VisitorBase<State & PluginPass>),
6353
...transformVueJSX,
6454
...sugarFragment,
6555
Program: {
@@ -133,7 +123,7 @@ const plugin: (
133123
if (!sourceName) {
134124
sourceName = addNamespace(path, 'vue', {
135125
ensureLiveReference: true,
136-
});
126+
}) as t.Identifier;
137127
}
138128
return t.memberExpression(sourceName, t.identifier(name));
139129
});

packages/babel-plugin-jsx/src/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type State = {
88
get: (name: string) => any;
99
set: (name: string, value: any) => any;
1010
opts: VueJSXPluginOptions;
11-
file: BabelCore.BabelFile;
11+
file: BabelCore.File;
1212
};
1313

1414
export interface VueJSXPluginOptions {

packages/babel-plugin-jsx/src/parseDirectives.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ const resolveDirective = (
186186
}
187187
const referenceName =
188188
'v' + directiveName[0].toUpperCase() + directiveName.slice(1);
189-
if (path.scope.references[referenceName]) {
189+
if (path.scope.getProgramParent().referencesSet.has(referenceName)) {
190190
return t.identifier(referenceName);
191191
}
192192
return t.callExpression(createIdentifier(state, 'resolveDirective'), [

packages/babel-plugin-jsx/src/sugar-fragment.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import * as t from '@babel/types';
2-
import { type NodePath, type Visitor } from '@babel/traverse';
2+
import type { NodePath, VisitorBase } from '@babel/traverse';
33
import type { State } from './interface';
44
import { FRAGMENT, createIdentifier } from './utils';
55

66
const transformFragment = (
77
path: NodePath<t.JSXFragment>,
88
Fragment: t.JSXIdentifier | t.JSXMemberExpression
99
) => {
10-
const children = path.get('children') || [];
1110
return t.jsxElement(
1211
t.jsxOpeningElement(Fragment, []),
1312
t.jsxClosingElement(Fragment),
14-
children.map(({ node }) => node),
15-
false
13+
path.node.children.slice()
1614
);
1715
};
1816

19-
const visitor: Visitor<State> = {
17+
const visitor: VisitorBase<State> = {
2018
JSXFragment: {
2119
enter(path, state) {
2220
const fragmentCallee = createIdentifier(state, FRAGMENT);

packages/babel-plugin-jsx/src/transform-vue-jsx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as t from '@babel/types';
2-
import { type NodePath, type Visitor } from '@babel/traverse';
2+
import type { NodePath, VisitorBase } from '@babel/traverse';
33
import { addDefault } from '@babel/helper-module-imports';
44
import {
55
buildIIFE,
@@ -577,7 +577,7 @@ const transformJSXElement = (
577577
]);
578578
};
579579

580-
const visitor: Visitor<State> = {
580+
const visitor: VisitorBase<State> = {
581581
JSXElement: {
582582
exit(path, state) {
583583
path.replaceWith(transformJSXElement(path, state));

packages/babel-plugin-jsx/test/resolve-type.test.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { transformAsync } from '@babel/core';
2-
// @ts-expect-error missing types
32
import typescript from '@babel/plugin-syntax-typescript';
3+
import jsx from '@babel/plugin-syntax-jsx';
4+
45
import VueJsx from '../src';
56

67
describe('resolve type', () => {
@@ -12,10 +13,7 @@ describe('resolve type', () => {
1213
const App = defineComponent((props: Props) => <div />)
1314
`,
1415
{
15-
plugins: [
16-
[typescript, { isTSX: true }],
17-
[VueJsx, { resolveType: true }],
18-
],
16+
plugins: [typescript, jsx, [VueJsx, { resolveType: true }]],
1917
}
2018
);
2119
expect(result!.code).toMatchSnapshot();

packages/babel-plugin-jsx/test/snapshot.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const transpile = (source: string, options: VueJSXPluginOptions = {}) =>
1212
source,
1313
{
1414
filename: '',
15-
presets: null,
15+
presets: [],
1616
plugins: [[JSX, options]],
1717
configFile: false,
1818
},

packages/babel-plugin-resolve-type/package.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,17 @@
3838
"dist"
3939
],
4040
"peerDependencies": {
41-
"@babel/core": "^7.0.0-0"
41+
"@babel/core": "^8.0.0-beta.2"
4242
},
4343
"dependencies": {
44-
"@babel/code-frame": "^7.27.1",
45-
"@babel/helper-module-imports": "^7.27.1",
46-
"@babel/helper-plugin-utils": "^7.27.1",
47-
"@babel/parser": "^7.28.0",
44+
"@babel/code-frame": "^8.0.0-beta.2",
45+
"@babel/helper-module-imports": "^8.0.0-beta.2",
46+
"@babel/helper-plugin-utils": "^8.0.0-beta.2",
47+
"@babel/parser": "^8.0.0-beta.2",
4848
"@vue/compiler-sfc": "^3.5.18"
4949
},
5050
"devDependencies": {
51-
"@babel/core": "^7.28.0",
52-
"@types/babel__code-frame": "^7.0.6",
51+
"@babel/core": "^8.0.0-beta.2",
5352
"vue": "catalog:"
5453
}
5554
}

0 commit comments

Comments
 (0)