babel-plugin-transform-react-remove-prop-types
Advanced tools
Comparing version 0.4.14 to 0.4.15
@@ -67,2 +67,16 @@ "use strict"; | ||
function areSetsEqual(set1, set2) { | ||
if (set1 === set2) { | ||
return true; | ||
} | ||
if (set1.size !== set2.size) { | ||
return false; | ||
} | ||
return !Array.from(set1).some(function (item) { | ||
return !set2.has(item); | ||
}); | ||
} | ||
function _default(api) { | ||
@@ -72,2 +86,20 @@ var template = api.template, | ||
traverse = api.traverse; | ||
var nestedIdentifiers = new Set(); | ||
var removedPaths = new Set(); | ||
var collectNestedIdentifiers = { | ||
Identifier: function Identifier(path) { | ||
if (path.parent.type === 'MemberExpression') { | ||
// foo.bar | ||
return; | ||
} | ||
if (path.parent.type === 'ObjectProperty' && (path.parent.key === path.node || path.parent.shorthand)) { | ||
// { foo: 'bar' } | ||
// { foo } | ||
return; | ||
} | ||
nestedIdentifiers.add(path.node.name); | ||
} | ||
}; | ||
return { | ||
@@ -146,2 +178,4 @@ visitor: { | ||
if (parent) { | ||
path.traverse(collectNestedIdentifiers); | ||
removedPaths.add(path); | ||
(0, _remove.default)(path, globalOptions, { | ||
@@ -162,2 +196,4 @@ type: 'createClass' | ||
if (isReactClass(pathClassDeclaration.get('superClass'), scope, globalOptions)) { | ||
path.traverse(collectNestedIdentifiers); | ||
removedPaths.add(path); | ||
(0, _remove.default)(path, globalOptions, { | ||
@@ -181,2 +217,4 @@ type: 'class static', | ||
if (forceRemoval) { | ||
path.traverse(collectNestedIdentifiers); | ||
removedPaths.add(path); | ||
(0, _remove.default)(path, globalOptions, { | ||
@@ -199,2 +237,4 @@ type: 'assign' | ||
if (isReactClass(superClass, scope, globalOptions)) { | ||
path.traverse(collectNestedIdentifiers); | ||
removedPaths.add(path); | ||
(0, _remove.default)(path, globalOptions, { | ||
@@ -205,2 +245,4 @@ type: 'assign' | ||
} else if ((0, _isStatelessComponent.default)(binding.path)) { | ||
path.traverse(collectNestedIdentifiers); | ||
removedPaths.add(path); | ||
(0, _remove.default)(path, globalOptions, { | ||
@@ -212,3 +254,46 @@ type: 'assign' | ||
}); | ||
var skippedIdentifiers = 0; | ||
var removeNewlyUnusedIdentifiers = { | ||
VariableDeclarator: function VariableDeclarator(path) { | ||
if (!nestedIdentifiers.has(path.node.id.name)) { | ||
return; | ||
} | ||
var _path$scope$getBindin = path.scope.getBinding(path.node.id.name), | ||
referencePaths = _path$scope$getBindin.referencePaths; // Count the number of referencePaths that are not in the | ||
// removedPaths Set. We need to do this in order to support the wrap | ||
// option, which doesn't actually remove the references. | ||
var hasRemainingReferencePaths = referencePaths.some(function (referencePath) { | ||
var found = referencePath.find(function (p) { | ||
return removedPaths.has(p); | ||
}); | ||
return !found; | ||
}); | ||
if (hasRemainingReferencePaths) { | ||
// There are still references to this identifier, so we need to | ||
// skip over it for now. | ||
skippedIdentifiers += 1; | ||
return; | ||
} | ||
removedPaths.add(path); | ||
nestedIdentifiers.delete(path.node.id.name); | ||
path.get('init').traverse(collectNestedIdentifiers); | ||
(0, _remove.default)(path, globalOptions, { | ||
type: 'declarator' | ||
}); | ||
} | ||
}; | ||
var lastNestedIdentifiers = new Set(); | ||
while (!areSetsEqual(nestedIdentifiers, lastNestedIdentifiers) && nestedIdentifiers.size > 0 && skippedIdentifiers < nestedIdentifiers.size) { | ||
lastNestedIdentifiers = new Set(nestedIdentifiers); | ||
skippedIdentifiers = 0; | ||
programPath.scope.crawl(); | ||
programPath.traverse(removeNewlyUnusedIdentifiers); | ||
} | ||
if (globalOptions.removeImport) { | ||
@@ -230,4 +315,4 @@ if (globalOptions.mode === 'remove') { | ||
var _path$scope$getBindin = path.scope.getBinding(importedIdentifierName), | ||
referencePaths = _path$scope$getBindin.referencePaths; | ||
var _path$scope$getBindin2 = path.scope.getBinding(importedIdentifierName), | ||
referencePaths = _path$scope$getBindin2.referencePaths; | ||
@@ -234,0 +319,0 @@ return referencePaths.length > 0; |
@@ -101,2 +101,17 @@ "use strict"; | ||
case 'declarator': | ||
if (mode === 'unsafe-wrap') { | ||
path.replaceWith(unsafeWrapTemplate({ | ||
NODE: path.node | ||
})); | ||
} else { | ||
path.replaceWith(wrapTemplate({ | ||
LEFT: path.node.id, | ||
RIGHT: path.node.init | ||
})); | ||
} | ||
path.node[visitedKey] = true; | ||
break; | ||
default: | ||
@@ -103,0 +118,0 @@ break; |
{ | ||
"name": "babel-plugin-transform-react-remove-prop-types", | ||
"version": "0.4.14", | ||
"version": "0.4.15", | ||
"description": "Remove unnecessary React propTypes from the production build", | ||
@@ -39,2 +39,3 @@ "main": "lib/index.js", | ||
"@babel/plugin-proposal-class-properties": "7.0.0-beta.42", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.0.0-rc.1", | ||
"@babel/plugin-transform-flow-strip-types": "7.0.0-beta.42", | ||
@@ -41,0 +42,0 @@ "@babel/preset-env": "7.0.0-beta.42", |
@@ -56,5 +56,39 @@ // @flow weak | ||
function areSetsEqual(set1, set2) { | ||
if (set1 === set2) { | ||
return true | ||
} | ||
if (set1.size !== set2.size) { | ||
return false | ||
} | ||
return !Array.from(set1).some(item => !set2.has(item)) | ||
} | ||
export default function(api) { | ||
const { template, types, traverse } = api | ||
const nestedIdentifiers = new Set() | ||
const removedPaths = new Set() | ||
const collectNestedIdentifiers = { | ||
Identifier(path) { | ||
if (path.parent.type === 'MemberExpression') { | ||
// foo.bar | ||
return | ||
} | ||
if ( | ||
path.parent.type === 'ObjectProperty' && | ||
(path.parent.key === path.node || path.parent.shorthand) | ||
) { | ||
// { foo: 'bar' } | ||
// { foo } | ||
return | ||
} | ||
nestedIdentifiers.add(path.node.name) | ||
}, | ||
} | ||
return { | ||
@@ -150,2 +184,4 @@ visitor: { | ||
if (parent) { | ||
path.traverse(collectNestedIdentifiers) | ||
removedPaths.add(path) | ||
remove(path, globalOptions, { | ||
@@ -165,2 +201,4 @@ type: 'createClass', | ||
if (isReactClass(pathClassDeclaration.get('superClass'), scope, globalOptions)) { | ||
path.traverse(collectNestedIdentifiers) | ||
removedPaths.add(path) | ||
remove(path, globalOptions, { | ||
@@ -187,2 +225,4 @@ type: 'class static', | ||
if (forceRemoval) { | ||
path.traverse(collectNestedIdentifiers) | ||
removedPaths.add(path) | ||
remove(path, globalOptions, { type: 'assign' }) | ||
@@ -203,5 +243,9 @@ return | ||
if (isReactClass(superClass, scope, globalOptions)) { | ||
path.traverse(collectNestedIdentifiers) | ||
removedPaths.add(path) | ||
remove(path, globalOptions, { type: 'assign' }) | ||
} | ||
} else if (isStatelessComponent(binding.path)) { | ||
path.traverse(collectNestedIdentifiers) | ||
removedPaths.add(path) | ||
remove(path, globalOptions, { type: 'assign' }) | ||
@@ -212,2 +256,45 @@ } | ||
let skippedIdentifiers = 0 | ||
const removeNewlyUnusedIdentifiers = { | ||
VariableDeclarator(path) { | ||
if (!nestedIdentifiers.has(path.node.id.name)) { | ||
return | ||
} | ||
const { referencePaths } = path.scope.getBinding(path.node.id.name) | ||
// Count the number of referencePaths that are not in the | ||
// removedPaths Set. We need to do this in order to support the wrap | ||
// option, which doesn't actually remove the references. | ||
const hasRemainingReferencePaths = referencePaths.some(referencePath => { | ||
const found = referencePath.find(p => removedPaths.has(p)) | ||
return !found | ||
}) | ||
if (hasRemainingReferencePaths) { | ||
// There are still references to this identifier, so we need to | ||
// skip over it for now. | ||
skippedIdentifiers += 1 | ||
return | ||
} | ||
removedPaths.add(path) | ||
nestedIdentifiers.delete(path.node.id.name) | ||
path.get('init').traverse(collectNestedIdentifiers) | ||
remove(path, globalOptions, { type: 'declarator' }) | ||
}, | ||
} | ||
let lastNestedIdentifiers = new Set() | ||
while ( | ||
!areSetsEqual(nestedIdentifiers, lastNestedIdentifiers) && | ||
nestedIdentifiers.size > 0 && | ||
skippedIdentifiers < nestedIdentifiers.size | ||
) { | ||
lastNestedIdentifiers = new Set(nestedIdentifiers) | ||
skippedIdentifiers = 0 | ||
programPath.scope.crawl() | ||
programPath.traverse(removeNewlyUnusedIdentifiers) | ||
} | ||
if (globalOptions.removeImport) { | ||
@@ -214,0 +301,0 @@ if (globalOptions.mode === 'remove') { |
@@ -104,2 +104,20 @@ // @flow weak | ||
case 'declarator': | ||
if (mode === 'unsafe-wrap') { | ||
path.replaceWith( | ||
unsafeWrapTemplate({ | ||
NODE: path.node, | ||
}) | ||
) | ||
} else { | ||
path.replaceWith( | ||
wrapTemplate({ | ||
LEFT: path.node.id, | ||
RIGHT: path.node.init, | ||
}) | ||
) | ||
} | ||
path.node[visitedKey] = true | ||
break | ||
default: | ||
@@ -106,0 +124,0 @@ break |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
42099
911
29
1