-
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 all 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { createElement } from 'react'; | ||
|
||
function Foo(props) { | ||
return ( | ||
<View {...props} x-if={true} className="container"> | ||
<View x-if={condition}>First</View> | ||
<View x-if={condition}>First</View><View x-elseif={another}>Second</View><View x-else>Third</View> | ||
<View x-else>Third</View> | ||
</View> | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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"); | ||
} | ||
] | ||
]), __create_condition__([ | ||
[ | ||
function() { | ||
return condition; | ||
}, | ||
function() { | ||
return React.createElement(View, null, "First"); | ||
} | ||
], | ||
[ | ||
function() { | ||
return another; | ||
}, | ||
function() { | ||
return React.createElement(View, null, "Second"); | ||
} | ||
], | ||
[ | ||
function() { | ||
return true; | ||
}, | ||
function() { | ||
return React.createElement(View, null, "Third"); | ||
} | ||
] | ||
]), /*#__PURE__*/ React.createElement(View, { | ||
"x-else": true | ||
}, "Third")); | ||
} | ||
] | ||
]); | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createElement } from 'react'; | ||
|
||
function Foo(props) { | ||
return ( | ||
<View {...props} x-if={true} className="container"> | ||
<View x-if={condition}>First</View> | ||
</View> | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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"); | ||
} | ||
] | ||
])); | ||
} | ||
] | ||
]); | ||
} | ||
|
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,27 @@ | ||
const swc = require('@swc/core') | ||
const path = require('path') | ||
const ConsoleStripper = require(path.join(__dirname, '../lib/index.js')).default; | ||
const fs = require('fs'); | ||
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), | ||
}); | ||
describe('', () => { | ||
const fixturesDir = path.join(__dirname, '__fixtures__'); | ||
fs.readdirSync(fixturesDir).map((caseName) => { | ||
it(`should ${caseName.split('-').join(' ')}`, () => { | ||
const fixtureDir = path.join(fixturesDir, caseName); | ||
const actualPath = path.join(fixtureDir, 'actual.js'); | ||
const actualCode = fs.readFileSync(actualPath, {encoding: 'utf-8'}); | ||
const expectedCode = fs.readFileSync(path.join(fixtureDir, 'expected.js'), { encoding: 'utf-8' }); | ||
|
||
const transformedOutput = swc.transformSync(actualCode, { | ||
jsc: { | ||
parser: { | ||
jsx: true | ||
}, | ||
}, | ||
plugin: JSXConditionTransformPlugin | ||
}); | ||
|
||
expect(output.code.replace(/\n/g, '')).toBe('void 0;') | ||
}) | ||
expect(transformedOutput.code.trim()).toBe(expectedCode.trim()); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Sync object | ||
/** @type {import('@jest/types').Config.InitialOptions} */ | ||
const config = { | ||
verbose: true, | ||
testMatch: ['!**/__fixtures__/**', '**/__tests__/**/*.js'] | ||
}; | ||
|
||
module.exports = config; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
{ | ||
"name": "swc-plugin-transform-jsx-condition", | ||
"version": "0.1.0", | ||
"version": "0.1.0-beta.1", | ||
"description": "Support of transform jsx condition directive based on SWC", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"build": "tsc -d", | ||
"build:watch": "tsc -d --watch", | ||
"test": "jest" | ||
}, | ||
"types": "./lib/index.d.ts", | ||
|
@@ -13,12 +14,13 @@ | |
"url": "git+https://github.com/jsx-plus/swc-plugin-transform-jsx-condition.git" | ||
}, | ||
"devDependencies": { | ||
"@swc/core": "^1.2.203", | ||
"jest": "^24.9.0", | ||
"typescript": "^4.7.3" | ||
}, | ||
"dependencies": {}, | ||
"author": "andycall", | ||
"dependencies": { | ||
"@swc/core": "^1.2.203" | ||
}, | ||
"author": "jsx-plus <[email protected]>", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 直接删掉就好了, 不用写 |
||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/jsx-plus/swc-plugin-transform-jsx-condition/issues" | ||
|
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.