Skip to content

Commit

Permalink
fix(stateless-to-stateful): fixed crash when converting component wit…
Browse files Browse the repository at this point in the history
…h JSX behind logical opetator
  • Loading branch information
Boris Litvinsky committed Nov 8, 2018
1 parent da18600 commit 9427e5e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/modules/statless-to-stateful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function getRenderFunctionBody(statelessComponentBody) {
} else if(t.isJSXElement(statelessComponentBody)) {
const body = t.isParenthesizedExpression(statelessComponentBody) ? statelessComponentBody : t.parenthesizedExpression(statelessComponentBody);
return t.blockStatement([t.returnStatement(body)]);
} else {
return t.blockStatement([t.returnStatement(statelessComponentBody)]);
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/test/jsx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,28 @@ describe('jsx module', function () {
expect(fileSystem.replaceTextInFile).to.have.been.calledWith('class foo extends Component {\n constructor(props) {\n super(props);\n }\n\n render() {\n return (<div></div>);\n }\n\n}', selectedTextStart, selectedTextEnd, '/source.js');
});

it('creates stateful component from arrow function', async () => {
sandbox.stub(editor, 'selectedText').returns(`
const foo = (props) => {
return (<div></div>);
}
`);

await statelessToStatefulComponent();

expect(fileSystem.replaceTextInFile).to.have.been.calledWith('class foo extends Component {\n constructor(props) {\n super(props);\n }\n\n render() {\n return (<div></div>);\n }\n\n}', selectedTextStart, selectedTextEnd, '/source.js');
});

it('creates stateful component from arrow function with JSX element being behind an AND operator', async () => {
sandbox.stub(editor, 'selectedText').returns(`
const foo = (props) => true && <div></div>;
`);

await statelessToStatefulComponent();

expect(fileSystem.replaceTextInFile).to.have.been.calledWith('class foo extends Component {\n constructor(props) {\n super(props);\n }\n\n render() {\n return true && <div></div>;\n }\n\n}', selectedTextStart, selectedTextEnd, '/source.js');
});

it('wraps returned JSX in parenthesis if they are missing ', async () => {
sandbox.stub(editor, 'selectedText').returns(`
const foo = (props) => {
Expand Down

0 comments on commit 9427e5e

Please sign in to comment.