Match-id-8083b7719ad52cfe90d5a4ecfff5bc11c0504335

This commit is contained in:
* 2022-03-10 20:09:33 +08:00 committed by *
parent 5e4c3738cc
commit 0d8c4ce0a5
36 changed files with 10228 additions and 50 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ build/
.vscode
package-lock.json
libs/**/dist
demo

View File

@ -10,7 +10,7 @@ module.exports = {
jsxSingleQuote: false, // 在JSX中使用双引号
trailingComma: 'es5', // 使用尾逗号(对象、数组等)
bracketSpacing: true, // 对象的括号间增加空格
jsxBracketSameLine: false, // 将多行JSX元素的>放在最后一行的末尾
bracketSameLine: false, // 将多行JSX元素的>放在最后一行的末尾
arrowParens: 'avoid', // 在唯一的arrow函数参数周围省略括号
vueIndentScriptAndStyle: false, // 不缩进Vue文件中的<script>和<style>标记内的代码
endOfLine: 'lf', // 仅限换行(\n

View File

@ -1,25 +0,0 @@
{
"plugins": [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-syntax-import-meta",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-json-strings",
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
],
"@babel/plugin-proposal-function-sent",
"@babel/plugin-proposal-export-namespace-from",
"@babel/plugin-proposal-numeric-separator",
"@babel/plugin-proposal-throw-expressions"
],
"env": {
"test": {
"plugins": [
"@babel/plugin-transform-modules-commonjs"
]
}
}
}

View File

@ -2,9 +2,6 @@
module.exports = {
presets: [
'@babel/preset-env',
],
plugins: [
/* eslint-disable-next-line global-require */
[require('./dist/index.js')],
],
'@babel/preset-typescript'
]
};

View File

@ -1,10 +1,5 @@
module.exports = {
transform: {
'\\.(ts|tsx)$': 'ts-jest',
},
globals: {
'ts-jest': {
babelConfig: true,
},
},
'\\.(js|jsx|ts|tsx)$': 'babel-jest',
}
};

View File

@ -31,7 +31,6 @@
"@babel/plugin-transform-modules-commonjs": "^7.2.0",
"@babel/preset-env": "^7.16.11",
"@babel/types": "^7.0.0",
"babel-plugin-tester": "^10.1.0",
"ts-jest": "^26.1.3"
"babel-plugin-tester": "^10.1.0"
}
}

View File

@ -1,9 +1,40 @@
import SyntaxJSX from '@babel/plugin-syntax-jsx';
import * as BabelCore from '@babel/core';
import * as t from '@babel/types';
import {NodePath} from '@babel/traverse';
import { NodePath } from '@babel/traverse';
import { JSXIdentifier, JSXMemberExpression, JSXNamespacedName } from '@babel/types';
function isHTMLTag(tagName: string) {
return tagName && /^[a-z]/.test(tagName);
}
const horizonJsx = t.memberExpression(t.identifier('Horizon'), t.identifier('jsx'));
function getTagNodeName(tagNode: JSXIdentifier | JSXMemberExpression | JSXNamespacedName) {
let tagName;
if (t.isJSXNamespacedName(tagNode)) {
throw 'horizon jsx doesn\'t support JSX namespace: ' + tagNode;
} else if (t.isJSXIdentifier(tagNode)) {
/*
this -> thisExpression
HTML -> stringLiteral
Others -> Identifier
*/
tagName = tagNode.name === 'this' ?
t.thisExpression() : isHTMLTag(tagNode.name) ?
t.stringLiteral(tagNode.name) :
t.identifier(tagNode.name);
} else if (t.isJSXMemberExpression(tagNode)) {
tagName = t.memberExpression(
getTagNodeName(tagNode.object),
getTagNodeName(tagNode.property),
);
}
return tagName;
}
export default ({ types }: typeof BabelCore) => {
export default ({types}: typeof BabelCore) => {
return {
name: 'horizon-jsx-babel-plugin',
inherits: SyntaxJSX,
@ -14,12 +45,13 @@ export default ({types}: typeof BabelCore) => {
},
JSXElement: {
exit(path) {
exit(path: NodePath<t.JSXElement>) {
const openingElement = path.get('openingElement');
const tagName = getTagNodeName(openingElement.node.name);
path.replaceWith(t.callExpression(horizonJsx, [tagName]));
},
},
}
},
};
};

View File

@ -0,0 +1 @@
var div = <Component {...props} foo="bar" />

View File

@ -0,0 +1,5 @@
import { jsx as _jsx } from "react/jsx-runtime";
var div = /*#__PURE__*/_jsx(Component, { ...props,
foo: "bar"
});

View File

@ -0,0 +1,13 @@
var x =
<div>
foo
{"bar"}
baz
<div>
buz
bang
</div>
qux
{null}
quack
</div>

View File

@ -0,0 +1,8 @@
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var x = /*#__PURE__*/_jsxs("div", {
children: ["foo", "bar", "baz", /*#__PURE__*/_jsx("div", {
children: "buz bang"
}), "qux", null, "quack"]
});

View File

@ -0,0 +1,7 @@
<p {...props}>text</p>;
<div {...props}>{contents}</div>;
<img alt="" {...{src, title}} />;
<blockquote {...{cite}}>{items}</blockquote>;

View File

@ -0,0 +1,24 @@
import { jsx as _jsx } from "react/jsx-runtime";
/*#__PURE__*/
_jsx("p", { ...props,
children: "text"
});
/*#__PURE__*/
_jsx("div", { ...props,
children: contents
});
/*#__PURE__*/
_jsx("img", {
alt: "",
src,
title
});
/*#__PURE__*/
_jsx("blockquote", {
cite,
children: items
});

View File

@ -0,0 +1 @@
var x = <><div /></>

View File

@ -0,0 +1,6 @@
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
var x = /*#__PURE__*/_jsx(_Fragment, {
children: /*#__PURE__*/_jsx("div", {})
});

View File

@ -0,0 +1 @@
var es3 = <F aaa new const var default foo-bar/>;

View File

@ -0,0 +1,6 @@
{
"plugins": [
["transform-react-jsx", { "runtime": "automatic" }],
"transform-property-literals"
]
}

View File

@ -0,0 +1,10 @@
import { jsx as _jsx } from "react/jsx-runtime";
var es3 = /*#__PURE__*/_jsx(F, {
aaa: true,
"new": true,
"const": true,
"var": true,
"default": true,
"foo-bar": true
});

View File

@ -0,0 +1 @@
<Component constructor="foo" />;

View File

@ -0,0 +1,6 @@
import { jsx as _jsx } from "react/jsx-runtime";
/*#__PURE__*/
_jsx(Component, {
constructor: "foo"
});

View File

@ -0,0 +1 @@
<Namespace.DeepNamespace.Component />;

View File

@ -0,0 +1,4 @@
import { jsx as _jsx } from "react/jsx-runtime";
/*#__PURE__*/
_jsx(Namespace.DeepNamespace.Component, {});

View File

@ -0,0 +1,6 @@
import { jsx as _jsx } from "react/jsx-runtime";
/*#__PURE__*/
_jsx("div", {
attr: /*#__PURE__*/_jsx("div", {})
});

View File

@ -0,0 +1 @@
var x = <div>text</div>;

View File

@ -0,0 +1,5 @@
import { jsx as _jsx } from "react/jsx-runtime";
var x = /*#__PURE__*/_jsx("div", {
children: "text"
});

View File

@ -0,0 +1 @@
<div>{...children}</div>;

View File

@ -0,0 +1,3 @@
{
"throws": "Spread children are not supported in React."
}

View File

@ -0,0 +1,3 @@
var x = <div></div>;
var y = <Eview.Table></Eview.Table>;
var z = <this></this>;

View File

@ -0,0 +1,3 @@
var x = Horizon.jsx('div');
var y = Horizon.jsx(Eview.Table);
var z = Horizon.jsx(this);

View File

@ -0,0 +1 @@
<div className="123"></div>

View File

@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": [
"src"
]
}

View File

@ -196,11 +196,11 @@ export function calcStartUpdateVNode(treeRoot: VNode) {
}
}
// 得到相等的路径
let startNodePath = toUpdateNodes[0].path.slice(0, idx);
const startNodePath = toUpdateNodes[0].path.slice(0, idx);
let node = treeRoot;
for (let i = 1; i < startNodePath.length; i++) {
let pathIndex = startNodePath[i];
const pathIndex = Number(startNodePath[i]);
node = getChildByIndex(node, pathIndex);
}
@ -232,7 +232,7 @@ function buildVNodeTree(treeRoot: VNode) {
}
parent = parent.parent;
}
// 当在componentWillUnmount中调用setStateparent可能是null因为startVNode会被clear
if (parent !== null) {
resetNamespaceCtx(parent);

View File

@ -6,7 +6,7 @@
"scripts": {
"build": " webpack --config ./scripts/webpack/webpack.config.js",
"build-3rdLib": "node ./scripts/gen3rdLib.js",
"build-3rdLib-dev": "node ./scripts/gen3rdLib.js --dev",
"build-3rdLib-dev": "npm run build & node ./scripts/gen3rdLib.js --dev",
"debug-test": "yarn test --debug",
"test": "jest --config=jest.config.js",
"watch-test": "yarn test --watch --dev"

10052
yarn.lock Normal file

File diff suppressed because it is too large Load Diff