Skip to content

Commit

Permalink
fix: switch logic
Browse files Browse the repository at this point in the history
  • Loading branch information
theajack committed Sep 21, 2023
1 parent f61e183 commit 6c33611
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
7 changes: 2 additions & 5 deletions packages/compiler-core/src/component/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* @Description: Coding something
*/
import type { NodePath } from '@babel/traverse';
import type { Identifier, JSXAttribute, JSXElement, JSXOpeningElement, Node } from '@babel/types';
import type { Identifier, JSXAttribute, JSXElement, JSXOpeningElement } from '@babel/types';
import { AlinsVar, ImportManager } from '../controller/import-manager';
import { isOriginJSXElement } from '../is';
import { isEmptyText, isOriginJSXElement } from '../is';
import { createCtxCall, findAttributes, getT, parseAttributes } from '../parse-utils';
import { transformSwitchChildren } from './logic-attribute';

Expand Down Expand Up @@ -41,9 +41,6 @@ function wrapChildren (
return t.arrowFunctionExpression(args, content);
}

function isEmptyText (node: Node) {
return node?.type === 'JSXText' && node.value.trim() === '';
}

function createNext (path: NodePath<any>) {
let index = 0;
Expand Down
11 changes: 8 additions & 3 deletions packages/compiler-core/src/component/logic-attribute.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NodePath } from '@babel/traverse';
import type { JSXAttribute, JSXElement } from '@babel/types';
import { findAttributes, getT } from '../parse-utils';
import { isMountAttr } from '../is';
import { isEmptyText, isMountAttr } from '../is';

/*
* @Author: chenzhongsheng
Expand Down Expand Up @@ -55,7 +55,7 @@ function transformIfAttr (

for (;;) {
next = next.getNextSibling();
if (!next.node || (next.type === 'JSXText' && !!next.node.value.trim())) {
if (!next.node || (isEmptyText(next.node))) {
break;
}
if (next.type === 'JSXElement') {
Expand Down Expand Up @@ -116,6 +116,11 @@ export function transformSwitchChildren (
for (let i = 0; i < children.length; i++) {
const child = children[i];

if (child.type === 'JSXText') {
if (isEmptyText(child)) continue;
throw new Error('switch 中只能包含jsxElement');
}

const attrs = child.openingElement?.attributes;

if (!attrs) continue;
Expand Down Expand Up @@ -149,7 +154,7 @@ export function transformSwitchChildren (
child
);
children[i] = newNode;
return children;
if (!isCase) return children;
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/compiler-core/src/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,8 @@ export function isMemberExp (node: Node) {

export function isMountAttr (name: string) {
return name === '$mount' || name.startsWith('$$');
}

export function isEmptyText (node: Node) {
return node?.type === 'JSXText' && node.value.trim() === '';
}
6 changes: 5 additions & 1 deletion scripts/build/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ function parseBuildConfig () {
packageName: 'eslint-config-alins',
format: 'cjs',
},
'plugin-babel-preset': {
'plugin-babel-preset': isDev ? {
packageName: 'babel-preset-alins',
format: 'cjs',
external: false,
} : {
packageName: 'babel-preset-alins',
type: 'node',
// format: 'esm cjs',
Expand Down
14 changes: 10 additions & 4 deletions scripts/dev/samples/playground.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<Frag $$App
$created={e => console.log('cc', e)}
$mounted={e => console.log('mm', e)}
>11</Frag>;
function Main () {
let count = 0;
const add = () => {count++;};
return <div $switch={count}>
<button $case={1} $break={false} onclick={add}>Count is 1</button>
<button $case={2} onclick={add}>Count is 1 or 2:{count}</button>
<button $default onclick={add}>Other Count:{count}</button>
</div>;
}
<Main $$App/>;

0 comments on commit 6c33611

Please sign in to comment.