-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add x-if x-else x-elseif #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andycall
wants to merge
7
commits into
main
Choose a base branch
from
feat/add-x-if
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
24461aa
feat: add x-if condition
andycall e64684c
beta: 0.1.0-beta.1
andycall 4329886
feat: auto test
andycall 07b125d
feat: add x-else and x-elseif
andycall 87e6649
feat: done
andycall 6d0497a
chore: fix format
andycall 084b686
fix: update deps
andycall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| .idea | ||
| lib | ||
|
|
||
| # Logs | ||
| logs | ||
| *.log | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,64 @@ | ||
| const swc = require('@swc/core') | ||
| const path = require('path') | ||
| const ConsoleStripper = require(path.join(__dirname, '../lib/index.js')).default; | ||
| const JSXConditionTransformPlugin = require(path.join(__dirname, '../lib/index.js')).default; | ||
|
|
||
| it('should strip console call', () => { | ||
| const output = swc.transformSync(`console.log('Foo')`, { | ||
| plugin: (m) => (new ConsoleStripper()).visitModule(m), | ||
| }); | ||
| it('should convert jsx x-if condition', () => { | ||
|
|
||
| expect(output.code.replace(/\n/g, '')).toBe('void 0;') | ||
| }) | ||
| let input = `import { createElement } from 'react'; | ||
| function Foo(props) { | ||
| return ( | ||
| <View {...props} x-if={true} className="container"> | ||
| <View x-if={condition}>First</View> | ||
| </View> | ||
| ) | ||
| }`; | ||
|
|
||
| const transformedOutput = swc.transformSync(input, { | ||
| jsc: { | ||
| parser: { | ||
| jsx: true | ||
| }, | ||
| }, | ||
| plugin: JSXConditionTransformPlugin | ||
| }); | ||
|
|
||
| const output = `function _extends() { | ||
| _extends = Object.assign || function(target) { | ||
| for(var i = 1; i < arguments.length; i++){ | ||
| var source = arguments[i]; | ||
| for(var key in source){ | ||
| if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
| target[key] = source[key]; | ||
| } | ||
| } | ||
| } | ||
| return target; | ||
| }; | ||
| return _extends.apply(this, arguments); | ||
| } | ||
| import { createCondition as __create_condition__ } from "babel-runtime-jsx-plus"; | ||
| import { createElement } from "react"; | ||
| function Foo(props) { | ||
| return __create_condition__([ | ||
| function() { | ||
| return true; | ||
| }, | ||
| function() { | ||
| return React.createElement(View, _extends({}, props, { | ||
| className: "container" | ||
| }), __create_condition__([ | ||
| function() { | ||
| return condition; | ||
| }, | ||
| function() { | ||
| return React.createElement(View, null, "First"); | ||
| } | ||
| ])); | ||
| } | ||
| ]); | ||
| } | ||
| `; | ||
|
|
||
| expect(transformedOutput.code).toBe(output); | ||
| }); | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,155 @@ | ||
| import {CallExpression, Expression} from '@swc/core'; | ||
| import Visitor from '@swc/core/Visitor' | ||
| import { | ||
| Expression, | ||
| JSXElement, Program, Statement, | ||
| } from '@swc/core'; | ||
| import Visitor from "@swc/core/Visitor"; | ||
| import { | ||
| ExprOrSpread | ||
| } from "@swc/core/types"; | ||
andycall marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import { | ||
| buildArrayExpression, | ||
| buildArrowFunctionExpression, buildCallExpression, buildIdentifier, buildImportDeclaration, | ||
| buildJSXElement, | ||
| buildJSXExpressionContainer, buildNamedImportSpecifier, buildNullLiteral, buildStringLiteral | ||
| } from "./utils"; | ||
|
|
||
| export default class ConsoleStripper extends Visitor { | ||
| visitCallExpression(e: CallExpression): Expression { | ||
| if (e.callee.type !== 'MemberExpression') { | ||
| return e; | ||
| enum JSXConditionType { | ||
| if = 'x-if', | ||
| else = 'x-else', | ||
| elseif = 'x-elseif' | ||
| } | ||
|
|
||
| function isJSXCondition(n: JSXElement) { | ||
| let opening = n.opening; | ||
| let openingAttributes = opening.attributes; | ||
|
|
||
| if (openingAttributes) { | ||
| for (let attribute of openingAttributes) { | ||
| if (attribute.type === 'JSXAttribute' && attribute.name.type === 'Identifier') { | ||
| switch (attribute.name.value) { | ||
| case JSXConditionType.if: | ||
| case JSXConditionType.else: | ||
| case JSXConditionType.elseif: { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| type JSXCondition = { | ||
| type: 'x-if' | 'x-elseif' | 'x-else', | ||
| expression?: Expression; | ||
| } | ||
|
|
||
| if (e.callee.object.type === 'Identifier' && e.callee.object.value === 'console') { | ||
| if (e.callee.property.type === 'Identifier') { | ||
| return { | ||
| type: "UnaryExpression", | ||
| span: e.span, | ||
| operator: 'void', | ||
| argument: { | ||
| type: 'NumericLiteral', | ||
| span: e.span, | ||
| value: 0 | ||
| function getJSXCondition(n: JSXElement): JSXCondition | undefined { | ||
| let opening = n.opening; | ||
| let openingAttributes = opening.attributes; | ||
| if (!openingAttributes) return undefined; | ||
|
|
||
| for (let attribute of openingAttributes) { | ||
| if (attribute.type === 'JSXAttribute' && attribute.name.type === 'Identifier') { | ||
| switch (attribute.name.value) { | ||
| case JSXConditionType.if: | ||
| case JSXConditionType.else: | ||
| case JSXConditionType.elseif: { | ||
| if (attribute.value?.type === 'JSXExpressionContainer') { | ||
| return { | ||
| type: attribute.name.value, | ||
| expression: attribute.value.expression | ||
| }; | ||
| } | ||
| if (attribute.value === null) { | ||
| return { | ||
| type: attribute.name.value, | ||
| expression: buildNullLiteral() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| return e | ||
| function JSXConditionToStandard(n: JSXElement) { | ||
| let openingAttributes = n.opening.attributes; | ||
|
|
||
| if (openingAttributes) { | ||
| openingAttributes = openingAttributes.filter((attribute) => { | ||
| if (attribute.type === 'JSXAttribute' && attribute.name.type === 'Identifier') { | ||
| switch (attribute.name.value) { | ||
| case JSXConditionType.if: | ||
| case JSXConditionType.else: | ||
| case JSXConditionType.elseif: { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| }); | ||
| } | ||
| return buildJSXElement({ | ||
| ...n.opening, | ||
| attributes: openingAttributes | ||
| }, n.children, n.closing) | ||
| } | ||
|
|
||
|
|
||
| function transformJSXCondition(n: JSXElement, isChild: boolean): JSXElement { | ||
| n.children = n.children.map((n) => { | ||
| if (n.type === 'JSXElement') { | ||
| return transformJSXCondition(n, true); | ||
| } | ||
| return n; | ||
| }); | ||
|
|
||
| if (!isJSXCondition(n)) { | ||
| return n; | ||
| } | ||
|
|
||
| let condition = getJSXCondition(n)!; | ||
|
|
||
| let elements: ExprOrSpread[] = [ | ||
| { | ||
| expression: buildArrowFunctionExpression([], getJSXCondition(n)!.expression!) | ||
| }, | ||
| { | ||
| expression: buildArrowFunctionExpression([], JSXConditionToStandard(n)) | ||
| } | ||
| ]; | ||
|
|
||
| let body = buildCallExpression(buildIdentifier('__create_condition__', false), [ | ||
| { | ||
| expression: buildArrayExpression(elements) | ||
| } | ||
| ]) as any; | ||
|
|
||
| return isChild ? buildJSXExpressionContainer(body) : body; | ||
| } | ||
|
|
||
| class JSXConditionTransformer extends Visitor { | ||
| visitJSXElement(n: JSXElement): JSXElement { | ||
| if (isJSXCondition(n)) { | ||
| return transformJSXCondition(n, false); | ||
| } | ||
|
|
||
| return n; | ||
| } | ||
| } | ||
|
|
||
| export default function JSXConditionTransformPlugin(m: Program): Program { | ||
| let result = new JSXConditionTransformer().visitProgram(m); | ||
| let babelImport = buildImportDeclaration([ | ||
| buildNamedImportSpecifier( | ||
| buildIdentifier('__create_condition__', false), | ||
| buildIdentifier('createCondition', false) | ||
| ) | ||
| ], buildStringLiteral('babel-runtime-jsx-plus')); | ||
| result.body.unshift(babelImport as any); | ||
|
|
||
| return result; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.