Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.idea
lib

# Logs
logs
*.log
Expand Down
11 changes: 11 additions & 0 deletions __tests__/__fixtures__/x-if-else/actual.js
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>
)
}
67 changes: 67 additions & 0 deletions __tests__/__fixtures__/x-if-else/expected.js
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"));
}
]
]);
}

9 changes: 9 additions & 0 deletions __tests__/__fixtures__/x-if/actual.js
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>
)
}
40 changes: 40 additions & 0 deletions __tests__/__fixtures__/x-if/expected.js
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");
}
]
]));
}
]
]);
}

30 changes: 23 additions & 7 deletions __tests__/usage.js
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());
});
});
});
8 changes: 8 additions & 0 deletions jest.config.js
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;
5 changes: 0 additions & 5 deletions lib/index.d.ts

This file was deleted.

29 changes: 0 additions & 29 deletions lib/index.js

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
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",
Expand All @@ -18,7 +19,7 @@
"typescript": "^4.7.3"
},
"dependencies": {},
"author": "andycall",
"author": "jsx-plus <[email protected]>",
Copy link
Member

Choose a reason for hiding this comment

The 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"
Expand Down
Loading