Skip to content
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

Add Auto Import to Babel Plugin #16626

Merged
merged 43 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
6673515
initial babel
lunaruan Aug 2, 2019
2d6d42b
Merge branch 'master' of https://github.com/facebook/react into add_b…
lunaruan Aug 9, 2019
bef3928
added more stuff
lunaruan Aug 9, 2019
49b90d3
Merge branch 'master' of https://github.com/facebook/react into add_b…
lunaruan Aug 9, 2019
d773b2b
wrote the tests/snapshots
lunaruan Aug 9, 2019
d9450ab
wrote plugin and added tests
lunaruan Aug 13, 2019
79ac078
some more stuff
lunaruan Aug 13, 2019
8c27b24
fixed all the tests
lunaruan Aug 16, 2019
574c661
cleaned up code some
lunaruan Aug 16, 2019
6864091
refactored code
lunaruan Aug 16, 2019
0645bbb
added another test
lunaruan Aug 16, 2019
4fe4163
Merge branch 'master' of https://github.com/facebook/react into add_b…
lunaruan Aug 16, 2019
518c151
cleaned up more stuff
lunaruan Aug 16, 2019
159c7f1
added package.json
lunaruan Aug 16, 2019
938d163
fixed linter
lunaruan Aug 16, 2019
ad5b0ed
updated tests and addressed some comments
lunaruan Aug 19, 2019
1b07249
components with static children use jsxs. made babel plugin part of t…
lunaruan Aug 20, 2019
87e2dc5
fixed isStaticChildren
lunaruan Aug 20, 2019
737ebb5
modified tests to not import source and removed bundle code for separ…
lunaruan Aug 20, 2019
aa83e96
edits from comments
lunaruan Aug 22, 2019
208b291
modified comments
lunaruan Aug 23, 2019
215e16c
packages/
lunaruan Aug 27, 2019
09fa4b1
Merge branch 'master' of https://github.com/facebook/react into add_a…
lunaruan Aug 27, 2019
c693c09
code
lunaruan Aug 28, 2019
7e88cb1
merge
lunaruan Aug 29, 2019
a173675
tests
lunaruan Aug 29, 2019
4fd10db
Merge branch 'master' of https://github.com/facebook/react into add_a…
lunaruan Aug 30, 2019
26215e2
more tests and refactored code
lunaruan Aug 30, 2019
c2c9389
added a test and more cleanup
lunaruan Aug 30, 2019
1755497
fixed sunil's comments
lunaruan Sep 3, 2019
ddfa12a
added shouldCacheImportFns
lunaruan Sep 4, 2019
3ae844f
added shouldCacheImportFns
lunaruan Sep 4, 2019
58b7783
add weird scope tests
lunaruan Sep 5, 2019
502f137
make sure you only add an auto import if there is jsx on the page
lunaruan Sep 23, 2019
ab09943
Merge branch 'master' of https://github.com/facebook/react into add_a…
lunaruan Sep 23, 2019
587a50b
Merge branch 'master' of https://github.com/facebook/react into add_a…
lunaruan Oct 29, 2019
f39ba89
Merge branch 'master' of https://github.com/lunaruan/react into add_a…
lunaruan Feb 12, 2020
fd25995
add @jsxAutoImport and @jsxImportSource as pragmas
lunaruan Feb 12, 2020
0123a37
fixed lint error and cleaned up createElement and shouldCacheImportFns
lunaruan Feb 13, 2020
f488c1c
Merge branch 'add_auto_import' of https://github.com/lunaruan/react i…
lunaruan Feb 13, 2020
2eed53f
Merge branch 'lunaruan-add_auto_import' into add_auto_import
lunaruan Feb 13, 2020
a95099f
git fix build code frame in tests
lunaruan Feb 13, 2020
ac7d715
re-remove create element tests because merge added it back
lunaruan Feb 13, 2020
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
327 changes: 290 additions & 37 deletions packages/babel-plugin-react-jsx/__tests__/TransformJSXToReactJSX-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ const babel = require('@babel/core');
const codeFrame = require('@babel/code-frame');
const {wrap} = require('jest-snapshot-serializer-raw');

function transform(input, options) {
function transform(input, pluginOpts, babelOpts) {
return wrap(
babel.transform(input, {
configFile: false,
sourceType: 'module',
plugins: [
'@babel/plugin-syntax-jsx',
'@babel/plugin-transform-arrow-functions',
...(options && options.development
...(pluginOpts && pluginOpts.development
? [
'@babel/plugin-transform-react-jsx-source',
'@babel/plugin-transform-react-jsx-self',
Expand All @@ -29,15 +30,283 @@ function transform(input, options) {
{
useBuiltIns: true,
useCreateElement: false,
...options,
...pluginOpts,
},
],
],
...babelOpts,
}).code
);
}

describe('transform react to jsx', () => {
it('throws error when sourceType is module and autoImport is require', () => {
const code = `var x = <div><span /></div>`;
expect(() => {
transform(code, {
autoImport: 'require',
});
}).toThrow(
'Babel `sourceType` must be set to `script` for autoImport ' +
'to use `require` syntax. See Babel `sourceType` for details.\n' +
codeFrame.codeFrameColumns(
code,
{start: {line: 1, column: 1}},
{highlightCode: true}
)
);
});

it('throws error when sourceType is script and autoImport is not require', () => {
const code = `var x = <div><span /></div>`;
expect(() => {
transform(
code,
{
autoImport: 'namespace',
},
{sourceType: 'script'}
);
}).toThrow(
'Babel `sourceType` must be set to `module` for autoImport ' +
'to use `namespace` syntax. See Babel `sourceType` for details.\n' +
codeFrame.codeFrameColumns(
code,
{start: {line: 1, column: 1}},
{highlightCode: true}
)
);
});

it("auto import that doesn't exist should throw error", () => {
const code = `var x = <div><span /></div>`;
expect(() => {
transform(code, {
autoImport: 'foo',
});
}).toThrow(
'autoImport must be one of the following: none, require, namespace, default, namedExports\n' +
codeFrame.codeFrameColumns(
code,
{start: {line: 1, column: 1}},
{highlightCode: true}
)
);
});

it('cannot use autoImport with createElement', () => {
const code = `var x = <div><span /></div>`;
expect(() => {
transform(code, {
autoImport: 'namespace',
useCreateElement: true,
});
}).toThrow(
'autoImport cannot be used with createElement. Consider setting ' +
'`useCreateElement` to `false` to use the new `jsx` function instead\n' +
codeFrame.codeFrameColumns(
code,
{start: {line: 1, column: 1}},
{highlightCode: true}
)
);
});

it('auto import can specify source', () => {
expect(
transform(`var x = <div><span /></div>`, {
autoImport: 'namespace',
importSource: 'foobar',
})
).toMatchSnapshot();
lunaruan marked this conversation as resolved.
Show resolved Hide resolved
});

it('auto import require', () => {
expect(
transform(
`var x = (
<>
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
</>
);`,
{
autoImport: 'require',
},
{
sourceType: 'script',
}
)
).toMatchSnapshot();
});

it('auto import namespace', () => {
expect(
transform(
`var x = (
<>
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
</>
);`,
{
autoImport: 'namespace',
}
)
).toMatchSnapshot();
});

it('auto import default', () => {
expect(
transform(
`var x = (
<>
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
</>
);`,
{
autoImport: 'default',
}
)
).toMatchSnapshot();
});

it('auto import named exports', () => {
expect(
transform(
`var x = (
<>
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
</>
);`,
{
autoImport: 'namedExports',
}
)
).toMatchSnapshot();
});

it('auto import in dev', () => {
expect(
transform(
`var x = (
<>
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
</>
);`,
{
autoImport: 'namedExports',
development: true,
}
)
).toMatchSnapshot();
});

it('auto import none', () => {
expect(
transform(
`var x = (
<>
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
</>
);`,
{
autoImport: 'none',
}
)
).toMatchSnapshot();
});

it('auto import undefined', () => {
expect(
transform(
`var x = (
<>
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
</>
);`
)
).toMatchSnapshot();
});

it('auto import with namespaces already defined', () => {
expect(
transform(
`
import * as _react from "foo";
const react = _react(1);
const _react1 = react;
const _react2 = react;
var x = (
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
);`,
{
autoImport: 'namespace',
}
)
).toMatchSnapshot();
});

it('auto import with react already defined', () => {
expect(
transform(
`
import * as react from "react";
var y = react.createElement("div", {foo: 1});
var x = (
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
);`,

{
autoImport: 'namespace',
}
)
).toMatchSnapshot();
});

it('fragment with no children', () => {
expect(transform(`var x = <></>`)).toMatchSnapshot();
});
Expand Down Expand Up @@ -283,23 +552,15 @@ describe('transform react to jsx', () => {
});

it('should disallow spread children', () => {
let _error;
const code = `<div>{...children}</div>;`;
try {
transform(code);
} catch (error) {
_error = error;
}
expect(_error).toEqual(
new SyntaxError(
'undefined: Spread children are not supported in React.' +
'\n' +
codeFrame.codeFrameColumns(
code,
{start: {line: 1, column: 6}},
{highlightCode: true}
)
)
expect(() => transform(code)).toThrow(
'Spread children are not supported in React.' +
'\n' +
codeFrame.codeFrameColumns(
code,
{start: {line: 1, column: 6}},
{highlightCode: true}
)
);
});

Expand Down Expand Up @@ -437,25 +698,17 @@ describe('transform react to jsx', () => {
});

it('should throw error namespaces if not flag', () => {
let _error;
const code = `<f:image />`;
try {
transform(code);
} catch (error) {
_error = error;
}
expect(_error).toEqual(
new SyntaxError(
"undefined: Namespace tags are not supported by default. React's " +
"JSX doesn't support namespace tags. You can turn on the " +
"'throwIfNamespace' flag to bypass this warning." +
'\n' +
codeFrame.codeFrameColumns(
code,
{start: {line: 1, column: 2}},
{highlightCode: true}
)
)
expect(() => transform(code)).toThrow(
"Namespace tags are not supported by default. React's " +
"JSX doesn't support namespace tags. You can turn on the " +
"'throwIfNamespace' flag to bypass this warning." +
'\n' +
codeFrame.codeFrameColumns(
code,
{start: {line: 1, column: 2}},
{highlightCode: true}
)
);
});

Expand Down
Loading