babel-plugin-react-require
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -1,7 +0,7 @@ | ||
import React from 'react' | ||
import React from 'react'; | ||
export default class Component { | ||
render() { | ||
return <div /> | ||
return <div />; | ||
} | ||
} |
export default class Component { | ||
render() { | ||
return <div /> | ||
return <div />; | ||
} | ||
} |
export default class Component { | ||
render() { | ||
return null | ||
return null; | ||
} | ||
} |
@@ -8,8 +8,8 @@ 'use strict'; | ||
exports.default = function (_ref) { | ||
let t = _ref.types; | ||
var t = _ref.types; | ||
return { | ||
visitor: { | ||
JSXOpeningElement(path, _ref2) { | ||
let file = _ref2.file; | ||
JSXOpeningElement: function (path, _ref2) { | ||
var file = _ref2.file; | ||
@@ -20,13 +20,12 @@ file.set('hasJSX', true); | ||
Program: { | ||
enter(path, _ref3) { | ||
let file = _ref3.file; | ||
enter: function (path, _ref3) { | ||
var file = _ref3.file; | ||
file.set('hasJSX', false); | ||
}, | ||
exit: function (_ref4, _ref5) { | ||
var node = _ref4.node; | ||
var scope = _ref4.scope; | ||
var file = _ref5.file; | ||
exit(_ref4, _ref5) { | ||
let node = _ref4.node; | ||
let scope = _ref4.scope; | ||
let file = _ref5.file; | ||
if (!(file.get('hasJSX') && !scope.hasBinding('React'))) { | ||
@@ -36,3 +35,3 @@ return; | ||
const reactImportDeclaration = t.importDeclaration([t.importDefaultSpecifier(t.identifier('React'))], t.stringLiteral('react')); | ||
var reactImportDeclaration = t.importDeclaration([t.importDefaultSpecifier(t.identifier('React'))], t.stringLiteral('react')); | ||
@@ -39,0 +38,0 @@ node.body.unshift(reactImportDeclaration); |
{ | ||
"name": "babel-plugin-react-require", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Babel plugin that adds React import declaration if file contains JSX tags.", | ||
@@ -41,17 +41,20 @@ "keywords": [ | ||
"devDependencies": { | ||
"babel-cli": "^6.1.2", | ||
"babel-core": "^6.1.2", | ||
"babel-eslint": "3.1.17", | ||
"babel-plugin-syntax-jsx": "^6.0.14", | ||
"babel-plugin-transform-es2015-classes": "^6.1.2", | ||
"babel-plugin-transform-es2015-destructuring": "^6.0.18", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.1.3", | ||
"babel-plugin-transform-es2015-parameters": "^6.0.18", | ||
"chai": "3.0.0", | ||
"eslint": "0.23.0", | ||
"eslint-plugin-react": "2.5.2", | ||
"istanbul": "0.3.15", | ||
"mocha": "2.2.5", | ||
"mocha-istanbul": "0.2.0", | ||
"rimraf": "^2.4.3" | ||
"babel-cli": "^6.2.0", | ||
"babel-core": "^6.3.2", | ||
"babel-eslint": "^5.0.0-beta4", | ||
"babel-plugin-syntax-jsx": "^6.1.18", | ||
"babel-plugin-transform-es2015-block-scoping": "^6.1.18", | ||
"babel-plugin-transform-es2015-classes": "^6.3.2", | ||
"babel-plugin-transform-es2015-destructuring": "^6.1.18", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.2.0", | ||
"babel-plugin-transform-es2015-parameters": "^6.1.18", | ||
"babel-plugin-transform-es2015-shorthand-properties": "^6.1.18", | ||
"chai": "^3.4.1", | ||
"eslint": "^1.10.3", | ||
"eslint-config-airbnb": "^2.0.0", | ||
"eslint-plugin-react": "^3.11.2", | ||
"istanbul": "^0.4.1", | ||
"mocha": "^2.3.4", | ||
"mocha-istanbul": "^0.2.0", | ||
"rimraf": "^2.4.4" | ||
}, | ||
@@ -58,0 +61,0 @@ "peerDependencies": {}, |
@@ -5,3 +5,3 @@ # babel-plugin-react-require | ||
This plugin is only about dumb components that doesn't extends `React.Component`. | ||
This plugin is only about stateless components that doesn't extends `React.Component`. | ||
If you want to use any other React functions then you should import their by yourself. | ||
@@ -14,6 +14,6 @@ | ||
```js | ||
export default class Component { | ||
render() { | ||
return <div /> | ||
} | ||
export default function Component() { | ||
return ( | ||
<div /> | ||
); | ||
} | ||
@@ -25,9 +25,9 @@ ``` | ||
```js | ||
import React from 'react' | ||
import React from 'react'; | ||
export default class Component { | ||
render() { | ||
/* this part will be transpiled by babel itself as usual */ | ||
return React.createElement('div') | ||
} | ||
export default function Component() { | ||
/* this part will be transpiled by babel itself as usual */ | ||
return ( | ||
React.createElement('div') | ||
); | ||
} | ||
@@ -46,3 +46,3 @@ ``` | ||
``` | ||
```json | ||
{ | ||
@@ -49,0 +49,0 @@ "plugins": [ |
@@ -19,10 +19,10 @@ export default function ({ types: t }) { | ||
const reactImportDeclaration = t.importDeclaration([ | ||
t.importDefaultSpecifier(t.identifier('React')) | ||
t.importDefaultSpecifier(t.identifier('React')), | ||
], t.stringLiteral('react')); | ||
node.body.unshift(reactImportDeclaration); | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
}; | ||
} |
@@ -1,43 +0,47 @@ | ||
/*eslint-disable no-var*/ | ||
'use strict'; | ||
var babel = require('babel-core') | ||
var assert = require('assert') | ||
var reactPlugin | ||
var transform | ||
const babel = require('babel-core'); | ||
const assert = require('assert'); | ||
let reactPlugin; | ||
let transform; | ||
try { | ||
reactPlugin = require('../lib-cov/index').default | ||
reactPlugin = require('../lib-cov/index').default; | ||
} catch (error) { | ||
reactPlugin = require('../src/index').default | ||
reactPlugin = require('../src/index').default; | ||
} | ||
describe('babel-plugin-react', function() { | ||
beforeEach(function() { | ||
transform = function(code) { | ||
describe('babel-plugin-react', () => { | ||
beforeEach(() => { | ||
transform = (code) => { | ||
return babel.transform(code, { | ||
plugins: ["syntax-jsx", reactPlugin] | ||
}).code | ||
} | ||
}) | ||
it('should return transpiled code with required React', function() { | ||
var transformed = transform('export default class Component {render() {return <div />}}') | ||
plugins: ['syntax-jsx', reactPlugin], | ||
}).code; | ||
}; | ||
}); | ||
assert.equal(transformed, 'import React from "react";\nexport default class Component {\n render() {\n return <div />;\n }\n}') | ||
}) | ||
it('should return not transpiled code', function() { | ||
var transformed = transform('console.log("hello world")') | ||
it('should return transpiled code with required React', () => { | ||
const transformed = transform('export default class Component {render() {return <div />}}'); | ||
assert.equal(transformed, 'console.log("hello world");') | ||
}) | ||
it('should check that plugin does not import React twice', function() { | ||
var transformed = transform('class Component{render(){return <div/>}} class Component2{render(){return <div />}}') | ||
assert.equal(transformed, 'import React from "react";\nexport default class Component {\n render() {\n return <div />;\n }\n}'); | ||
}); | ||
it('should return not transpiled code', () => { | ||
const transformed = transform('console.log("hello world")'); | ||
assert.equal(transformed, 'console.log("hello world");'); | ||
}); | ||
it('should check that plugin does not import React twice', () => { | ||
const transformed = transform('class Component{render(){return <div/>}} class Component2{render(){return <div />}}'); | ||
assert.equal(transformed, 'import React from "react";\nclass Component {\n render() {\n return <div />;\n }\n}' | ||
+ 'class Component2 {\n render() {\n return <div />;\n }\n}') | ||
}) | ||
it('should does not replace users import on plugins import', function() { | ||
var transformed = transform('import React from"react/addons"\nclass Component{render(){return <div/>}}') | ||
+ 'class Component2 {\n render() {\n return <div />;\n }\n}'); | ||
}); | ||
assert.equal(transformed, 'import React from "react/addons";\nclass Component {\n render() {\n return <div />;\n }\n}') | ||
}) | ||
}) | ||
it('should does not replace users import on plugins import', () => { | ||
const transformed = transform('import React from"react/addons"\nclass Component{render(){return <div/>}}'); | ||
assert.equal(transformed, 'import React from "react/addons";\nclass Component {\n render() {\n return <div />;\n }\n}'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
22
129
8487
18