Skip to content

Commit

Permalink
feature(putout) add ability to determine jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jul 3, 2019
1 parent 591044b commit 48cbe32
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 7 deletions.
6 changes: 5 additions & 1 deletion packages/plugin-react-hooks/lib/remove-this/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ test('plugin-react-hooks: remove-this: transform', (t) => {
t.end();
});

test('plugin-react-hooks: remove-this: transform', (t) => {
test('plugin-react-hooks: remove-this: transform: code', (t) => {
const from = `
const {Component} = require('react');
class Hello extends Component {
render() {
return (
Expand All @@ -27,6 +29,8 @@ test('plugin-react-hooks: remove-this: transform', (t) => {
`;

const to = `
const {Component} = require('react');
class Hello extends Component {
render() {
return <button onClick={setEnabled}/>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const {PureComponent} = require('react');

export function wrap(HelloComponent) {
return class Hello extends PureComponent {
render() {
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-remove-unused-variables/test/get-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -1158,9 +1158,10 @@ test('remove-unused-variables: get-vars: class return', (t) => {
const result = getVars(ast).map(dutify);

const expected = [{
PureComponent: _u,
PureComponent: du,
React: _u,
wrap: du,
require: _u,
}, {
HelloComponent: du,
}, {
Expand Down
4 changes: 2 additions & 2 deletions packages/putout/help.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"--enable ": "enable rule by name in .putout.json",
"--disable ": "disable rule by name in .putout.json",
"--enable-all ": "enable all rules in .putout.json",
"--disable-all ": "disable all rules in .putout.json"
" --jsx ": "enable jsx (default)",
"--disable-all ": "disable all rules in .putout.json",
" --jsx ": "enable jsx (try to determine by default)",
" --flow ": "enable flow",
" --no-jsx ": "disable jsx",
" --no-flow ": "disable flow (default)"
Expand Down
11 changes: 8 additions & 3 deletions packages/putout/lib/custom-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ module.exports = (source, {parser, isTS, isFlow, isJSX}) => {

const clean = (a) => a.filter(Boolean);
const getFlow = (a) => !a.indexOf('// @flow');
const getJSX = (a) => a.includes('react');

function babelParse(source, {isTS, isFlow = getFlow(source), isJSX}) {
function babelParse(source, {isTS, isFlow = getFlow(source), isJSX = getJSX(source)}) {
const {parse} = initBabel();

return parse(source, {
Expand All @@ -51,7 +52,11 @@ function babelParse(source, {isTS, isFlow = getFlow(source), isJSX}) {
'classProperties',
'numericSeparator',
'exportDefaultFrom',
...getBabelLangExts({isTS, isFlow, isJSX, source}),
...getBabelLangExts({
isTS,
isFlow,
isJSX,
}),
]),
});
}
Expand Down Expand Up @@ -94,7 +99,7 @@ function acornParse(source) {
};
}

function getBabelLangExts({isTS, isFlow, isJSX = true}) {
function getBabelLangExts({isTS, isFlow, isJSX}) {
const langs = [
isJSX && 'jsx',
];
Expand Down
5 changes: 5 additions & 0 deletions packages/putout/test/fixture/not-jsx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class AccessibilityManager {
private _onBoundaryFocus(): void {
const boundaryElement = <HTMLElement>e.target;
}
}
17 changes: 17 additions & 0 deletions packages/putout/test/putout.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const fixture = readFixtures([
'overlap',
'overlap-fix',
'jsx',
'not-jsx',
]);

test('putout: no vars', (t) => {
Expand Down Expand Up @@ -381,6 +382,13 @@ test('putout: overlap', (t) => {
t.end();
});

test('putout: isJSX', (t) => {
const [e] = tryCatch(putout, fixture.jsx);

t.notOk(e, 'should not be an error');
t.end();
});

test('putout: isJSX: false', (t) => {
const [e] = tryCatch(putout, fixture.jsx, {
isJSX: false,
Expand All @@ -390,3 +398,12 @@ test('putout: isJSX: false', (t) => {
t.end();
});

test('putout: typescript: not jsx', (t) => {
const [e] = tryCatch(putout, fixture.notJsx, {
isTS: true,
});

t.notOk(e, 'should not be an error');
t.end();
});

0 comments on commit 48cbe32

Please sign in to comment.