babel-plugin-transform-react-remove-prop-types
Advanced tools
Comparing version 0.2.10 to 0.2.11
@@ -8,4 +8,4 @@ 'use strict'; | ||
exports.default = function (_ref) { | ||
var template = _ref.template; | ||
var types = _ref.types; | ||
var template = _ref.template, | ||
types = _ref.types; | ||
@@ -51,4 +51,4 @@ var wrapperIfTemplate = template('\n if (process.env.NODE_ENV !== "production") {\n NODE;\n }\n '); | ||
ClassProperty: function ClassProperty(path) { | ||
var node = path.node; | ||
var scope = path.scope; | ||
var node = path.node, | ||
scope = path.scope; | ||
@@ -72,4 +72,4 @@ | ||
AssignmentExpression: function AssignmentExpression(path) { | ||
var node = path.node; | ||
var scope = path.scope; | ||
var node = path.node, | ||
scope = path.scope; | ||
@@ -152,7 +152,7 @@ | ||
function remove(path, options) { | ||
var visitedKey = options.visitedKey; | ||
var wrapperIfTemplate = options.wrapperIfTemplate; | ||
var mode = options.mode; | ||
var type = options.type; | ||
var types = options.types; | ||
var visitedKey = options.visitedKey, | ||
wrapperIfTemplate = options.wrapperIfTemplate, | ||
mode = options.mode, | ||
type = options.type, | ||
types = options.types; | ||
@@ -177,23 +177,25 @@ | ||
case 'class static': | ||
var ref = void 0; | ||
var pathClassDeclaration = options.pathClassDeclaration; | ||
{ | ||
var ref = void 0; | ||
var pathClassDeclaration = options.pathClassDeclaration; | ||
if (!pathClassDeclaration.isClassExpression() && pathClassDeclaration.node.id) { | ||
ref = pathClassDeclaration.node.id; | ||
} else { | ||
// Class without name not supported | ||
return; | ||
} | ||
if (!pathClassDeclaration.isClassExpression() && pathClassDeclaration.node.id) { | ||
ref = pathClassDeclaration.node.id; | ||
} else { | ||
// Class without name not supported | ||
return; | ||
} | ||
var node = types.expressionStatement(types.assignmentExpression('=', types.memberExpression(ref, path.node.key), path.node.value)); | ||
var node = types.expressionStatement(types.assignmentExpression('=', types.memberExpression(ref, path.node.key), path.node.value)); | ||
// We need to append the node at the parent level in this case. | ||
if (pathClassDeclaration.parentPath.isExportDeclaration()) { | ||
pathClassDeclaration = pathClassDeclaration.parentPath; | ||
// We need to append the node at the parent level in this case. | ||
if (pathClassDeclaration.parentPath.isExportDeclaration()) { | ||
pathClassDeclaration = pathClassDeclaration.parentPath; | ||
} | ||
pathClassDeclaration.insertAfter(node); | ||
path.remove(); | ||
break; | ||
} | ||
pathClassDeclaration.insertAfter(node); | ||
path.remove(); | ||
break; | ||
case 'class assign': | ||
@@ -205,2 +207,5 @@ case 'stateless': | ||
break; | ||
default: | ||
break; | ||
} | ||
@@ -207,0 +212,0 @@ } else { |
@@ -9,23 +9,25 @@ 'use strict'; | ||
function isJSXElementOrReactCreateElement(node) { | ||
var type = node.type; | ||
var callee = node.callee; | ||
function isJSXElementOrReactCreateElement(path) { | ||
var visited = false; | ||
path.traverse({ | ||
CallExpression: function CallExpression(path2) { | ||
var callee = path2.node.callee; | ||
if (type === 'JSXElement') { | ||
return true; | ||
} | ||
if (callee && callee.object && callee.object.name === 'React' && callee.property.name === 'createElement') { | ||
return true; | ||
} | ||
if (callee && callee.object && callee.object.name === 'React' && callee.property.name === 'createElement') { | ||
visited = true; | ||
} | ||
}, | ||
JSXElement: function JSXElement() { | ||
visited = true; | ||
} | ||
}); | ||
return false; | ||
return visited; | ||
} | ||
function isReturningJSXElement(path) { | ||
/** | ||
* Early exit for ArrowFunctionExpressions, there is no ReturnStatement node. | ||
*/ | ||
if (path.node.init && path.node.init.body && isJSXElementOrReactCreateElement(path.node.init.body)) { | ||
// Early exit for ArrowFunctionExpressions, there is no ReturnStatement node. | ||
if (path.node.init && path.node.init.body && isJSXElementOrReactCreateElement(path)) { | ||
return true; | ||
@@ -50,3 +52,3 @@ } | ||
if (isJSXElementOrReactCreateElement(argument.node)) { | ||
if (isJSXElementOrReactCreateElement(path2)) { | ||
visited = true; | ||
@@ -74,11 +76,7 @@ return; | ||
var validPossibleStatelessComponentTypes = ['VariableDeclarator', 'FunctionDeclaration']; | ||
var VALID_POSSIBLE_STATELESS_COMPONENT_TYPES = ['VariableDeclarator', 'FunctionDeclaration']; | ||
/** | ||
* Returns `true` if the path represents a function which returns a JSXElement | ||
*/ | ||
// Returns `true` if the path represents a function which returns a JSXElement | ||
function isStatelessComponent(path) { | ||
var node = path.node; | ||
if (validPossibleStatelessComponentTypes.indexOf(node.type) === -1) { | ||
if (VALID_POSSIBLE_STATELESS_COMPONENT_TYPES.indexOf(path.node.type) === -1) { | ||
return false; | ||
@@ -85,0 +83,0 @@ } |
{ | ||
"name": "babel-plugin-transform-react-remove-prop-types", | ||
"version": "0.2.10", | ||
"version": "0.2.11", | ||
"description": "Remove unnecessary React propTypes from the production build", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"lint": "eslint src test", | ||
"lint": "eslint . && echo \"eslint: no lint errors\"", | ||
"test:unit": "babel-node test/index.js", | ||
@@ -14,3 +14,3 @@ "test:unit:watch": "babel-node test/watch.js", | ||
"flow": "flow", | ||
"prepublish": "npm run build" | ||
"prepublish": "npm run build && pkgfiles" | ||
}, | ||
@@ -39,19 +39,23 @@ "repository": { | ||
"devDependencies": { | ||
"babel-cli": "^6.14.0", | ||
"babel-core": "^6.14.0", | ||
"babel-eslint": "^7.0.0", | ||
"babel-preset-es2015": "^6.14.0", | ||
"babel-preset-react": "^6.11.1", | ||
"babel-preset-stage-0": "^6.3.13", | ||
"eslint": "^3.4.0", | ||
"eslint-plugin-flowtype": "^2.14.3", | ||
"eslint-plugin-import": "^2.0.0", | ||
"eslint-plugin-mocha": "^4.5.1", | ||
"eslint-plugin-react": "^6.2.0", | ||
"flow-bin": "^0.33.0", | ||
"glob": "^7.0.6", | ||
"babel-cli": "^6.18.0", | ||
"babel-core": "^6.18.2", | ||
"babel-eslint": "^7.1.0", | ||
"babel-preset-es2015": "^6.18.0", | ||
"babel-preset-react": "^6.16.0", | ||
"babel-preset-stage-0": "^6.16.0", | ||
"eslint": "^3.10.2", | ||
"eslint-config-airbnb": "^13.0.0", | ||
"eslint-plugin-babel": "^4.0.0", | ||
"eslint-plugin-flowtype": "^2.25.0", | ||
"eslint-plugin-import": "^2.2.0", | ||
"eslint-plugin-jsx-a11y": "^2.2.3", | ||
"eslint-plugin-mocha": "^4.7.0", | ||
"eslint-plugin-react": "^6.7.1", | ||
"flow-bin": "^0.35.0", | ||
"glob": "^7.1.1", | ||
"minimist": "^1.2.0", | ||
"mocha": "^3.0.2", | ||
"nodemon": "^1.10.2" | ||
"mocha": "^3.1.2", | ||
"nodemon": "^1.11.0", | ||
"pkgfiles": "^2.3.0" | ||
} | ||
} |
@@ -64,3 +64,3 @@ // @flow weak | ||
// Inspired from babel-plugin-transform-class-properties. | ||
case 'class static': | ||
case 'class static': { | ||
let ref; | ||
@@ -77,3 +77,3 @@ let pathClassDeclaration = options.pathClassDeclaration; | ||
const node = types.expressionStatement( | ||
types.assignmentExpression('=', types.memberExpression(ref, path.node.key), path.node.value) | ||
types.assignmentExpression('=', types.memberExpression(ref, path.node.key), path.node.value), | ||
); | ||
@@ -89,2 +89,3 @@ | ||
break; | ||
} | ||
@@ -96,5 +97,8 @@ case 'class assign': | ||
NODE: path.node, | ||
} | ||
}, | ||
)); | ||
break; | ||
default: | ||
break; | ||
} | ||
@@ -106,3 +110,3 @@ } else { | ||
export default function({template, types}) { | ||
export default function ({ template, types }) { | ||
const wrapperIfTemplate = template(` | ||
@@ -142,4 +146,4 @@ if (process.env.NODE_ENV !== "production") { | ||
visitedKey: VISITED_KEY, | ||
wrapperIfTemplate: wrapperIfTemplate, | ||
mode: mode, | ||
wrapperIfTemplate, | ||
mode, | ||
type: 'createClass', | ||
@@ -163,7 +167,7 @@ }); | ||
visitedKey: VISITED_KEY, | ||
wrapperIfTemplate: wrapperIfTemplate, | ||
mode: mode, | ||
wrapperIfTemplate, | ||
mode, | ||
type: 'class static', | ||
types: types, | ||
pathClassDeclaration: pathClassDeclaration, | ||
types, | ||
pathClassDeclaration, | ||
}); | ||
@@ -196,4 +200,4 @@ } | ||
visitedKey: VISITED_KEY, | ||
wrapperIfTemplate: wrapperIfTemplate, | ||
mode: mode, | ||
wrapperIfTemplate, | ||
mode, | ||
type: 'class assign', | ||
@@ -205,4 +209,4 @@ }); | ||
visitedKey: VISITED_KEY, | ||
wrapperIfTemplate: wrapperIfTemplate, | ||
mode: mode, | ||
wrapperIfTemplate, | ||
mode, | ||
type: 'stateless', | ||
@@ -209,0 +213,0 @@ }); |
// @flow weak | ||
function isJSXElementOrReactCreateElement(node) { | ||
const { | ||
type, | ||
callee, | ||
} = node; | ||
function isJSXElementOrReactCreateElement(path) { | ||
let visited = false; | ||
if (type === 'JSXElement') { | ||
return true; | ||
} | ||
path.traverse({ | ||
CallExpression(path2) { | ||
const { | ||
callee, | ||
} = path2.node; | ||
if (callee && callee.object && callee.object.name === 'React' && | ||
callee.property.name === 'createElement') { | ||
return true; | ||
} | ||
if (callee && callee.object && callee.object.name === 'React' && | ||
callee.property.name === 'createElement') { | ||
visited = true; | ||
} | ||
}, | ||
JSXElement() { | ||
visited = true; | ||
}, | ||
}); | ||
return false; | ||
return visited; | ||
} | ||
function isReturningJSXElement(path) { | ||
/** | ||
* Early exit for ArrowFunctionExpressions, there is no ReturnStatement node. | ||
*/ | ||
if (path.node.init && path.node.init.body && isJSXElementOrReactCreateElement(path.node.init.body)) { | ||
// Early exit for ArrowFunctionExpressions, there is no ReturnStatement node. | ||
if (path.node.init && path.node.init.body && isJSXElementOrReactCreateElement(path)) { | ||
return true; | ||
@@ -45,3 +47,3 @@ } | ||
if (isJSXElementOrReactCreateElement(argument.node)) { | ||
if (isJSXElementOrReactCreateElement(path2)) { | ||
visited = true; | ||
@@ -69,3 +71,3 @@ return; | ||
const validPossibleStatelessComponentTypes = [ | ||
const VALID_POSSIBLE_STATELESS_COMPONENT_TYPES = [ | ||
'VariableDeclarator', | ||
@@ -75,9 +77,5 @@ 'FunctionDeclaration', | ||
/** | ||
* Returns `true` if the path represents a function which returns a JSXElement | ||
*/ | ||
// Returns `true` if the path represents a function which returns a JSXElement | ||
export default function isStatelessComponent(path) { | ||
const node = path.node; | ||
if (validPossibleStatelessComponentTypes.indexOf(node.type) === -1) { | ||
if (VALID_POSSIBLE_STATELESS_COMPONENT_TYPES.indexOf(path.node.type) === -1) { | ||
return false; | ||
@@ -84,0 +82,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18152
478
20