Skip to content

Commit

Permalink
try something else
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Oct 15, 2018
1 parent 1a288fa commit aadbac8
Show file tree
Hide file tree
Showing 18 changed files with 1,181 additions and 974 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
"test:unit": "mocha",
"test:watch": "mocha -w",
"prettier": "find . -name \"*.js\" | grep -v -f .eslintignore | xargs prettier --write",
"test": "npm run lint && npm run test:unit && npm run flow",
"test": "npm run lint && npm run test:unit",
"prebuild": "rm -rf lib/",
"build": "babel src --out-dir lib",
"flow": "flow",
"version": "npm run build && pkgfiles"
},
"repository": {
Expand Down Expand Up @@ -53,7 +52,6 @@
"eslint-plugin-mocha": "^4.11.0",
"eslint-plugin-prettier": "^2.3.1",
"eslint-plugin-react": "^7.4.0",
"flow-bin": "^0.59.0",
"mocha": "^4.0.1",
"path-exists": "^3.0.0",
"pkgfiles": "^2.3.2",
Expand Down
33 changes: 12 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow weak
/* eslint-disable global-require, import/no-dynamic-require */

// import generate from 'babel-generator';
Expand Down Expand Up @@ -153,25 +152,12 @@ export default function(api) {
`,
{ placeholderPattern: /^NODE$/ }
),
wrapTemplate: ({ LEFT, RIGHT }, options = {}) => {
const { as = 'assignmentExpression' } = options

const right = template.expression(
`
process.env.NODE_ENV !== "production" ? RIGHT : {}
`,
{ placeholderPattern: /^(LEFT|RIGHT)$/ }
)({ RIGHT })

switch (as) {
case 'variableDeclarator':
return types.variableDeclarator(LEFT, right)
case 'assignmentExpression':
return types.assignmentExpression('=', LEFT, right)
default:
throw new Error(`unrecognized template type ${as}`)
}
},
wrapTemplate: template(
`
LEFT = process.env.NODE_ENV !== "production" ? RIGHT : {}
`,
{ placeholderPattern: /^(LEFT|RIGHT)$/ }
),
mode: state.opts.mode || 'remove',
ignoreFilenames,
types,
Expand Down Expand Up @@ -301,6 +287,11 @@ export default function(api) {
let skippedIdentifiers = 0
const removeNewlyUnusedIdentifiers = {
VariableDeclarator(path) {
// Only consider the top level scope.
if (path.scope.block.type !== 'Program') {
return
}

if (['ObjectPattern', 'ArrayPattern'].includes(path.node.id.type)) {
// Object or Array destructuring, so we will want to capture all
// the names created by the destructuring. This currently doesn't
Expand All @@ -322,7 +313,7 @@ export default function(api) {
// 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))
const found = referencePath.find(path2 => removedPaths.has(path2))
return !found
})

Expand Down
6 changes: 2 additions & 4 deletions src/isAnnotatedForRemoval.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// @flow weak

export default function(node) {
export default function isAnnotatedForRemoval(node) {
const comments = node.trailingComments || []

return !!comments.find(({ value }) => value.trim() === 'remove-proptypes')
return Boolean(comments.find(({ value }) => value.trim() === 'remove-proptypes'))
}
2 changes: 0 additions & 2 deletions src/isStatelessComponent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow weak

const traversed = Symbol('traversed')

function isJSXElementOrReactCreateElement(path) {
Expand Down
12 changes: 4 additions & 8 deletions src/remove.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow weak
/* eslint-disable no-param-reassign */

function isInside(scope, regex) {
Expand Down Expand Up @@ -114,13 +113,10 @@ export default function remove(path, globalOptions, options) {
)
} else {
path.replaceWith(
wrapTemplate(
{
LEFT: path.node.id,
RIGHT: path.node.init,
},
{ as: 'variableDeclarator' }
)
wrapTemplate({
LEFT: path.node.id,
RIGHT: path.node.init,
})
)
}
path.node[visitedKey] = true
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('fixtures', () => {
const fixtureDir = path.join(fixturesDir, caseName)

// Only run a specific test
// if (caseName !== 'recursive') {
// if (caseName !== 'const-in-anonymous-validator') {
// return
// }

Expand Down
20 changes: 18 additions & 2 deletions test/fixtures/const-in-anonymous-validator/actual.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,26 @@ const Component = () => (
);

Component.propTypes = {
variant: props => {
variant1: props => {
const variants = [null];
return variantſ[0];
}
},
variant2: chainPropTypes(
PropTypes.oneOf(['foo']),
props => {
const deprecatedVariants = [
'display4',
'display3',
'display2',
'display1',
'headline',
'title',
'subheading',
];

return null;
},
),
};

export default Component;
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ var Component = function Component() {
};

var _default = Component;
exports.default = _default;
exports.default = _default;
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import React, { PropTypes } from 'react';

const Component = () => <div />;

export default Component;
export default Component;
12 changes: 8 additions & 4 deletions test/fixtures/const-in-anonymous-validator/expected-wrap-es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ var Component = function Component() {
};

Component.propTypes = process.env.NODE_ENV !== "production" ? {
variant: function variant(props) {
var variants = process.env.NODE_ENV !== "production" ? [null] : {};
variant1: function variant1(props) {
var variants = [null];
return variantſ[0];
}
},
variant2: chainPropTypes(_react.PropTypes.oneOf(['foo']), function (props) {
var deprecatedVariants = ['display4', 'display3', 'display2', 'display1', 'headline', 'title', 'subheading'];
return null;
})
} : {};
var _default = Component;
exports.default = _default;
exports.default = _default;
12 changes: 8 additions & 4 deletions test/fixtures/const-in-anonymous-validator/expected-wrap-es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import React, { PropTypes } from 'react';
const Component = () => <div />;

Component.propTypes = process.env.NODE_ENV !== "production" ? {
variant: props => {
const variants = process.env.NODE_ENV !== "production" ? [null] : {};
variant1: props => {
const variants = [null];
return variantſ[0];
}
},
variant2: chainPropTypes(PropTypes.oneOf(['foo']), props => {
const deprecatedVariants = ['display4', 'display3', 'display2', 'display1', 'headline', 'title', 'subheading'];
return null;
})
} : {};
export default Component;
export default Component;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var propTypes = process.env.NODE_ENV !== "production" ? {
foo: PropTypes.string
} : {};
} : {};;

var Foo =
/*#__PURE__*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const propTypes = process.env.NODE_ENV !== "production" ? {
foo: PropTypes.string
} : {};
} : {};;

class Foo extends React.Component {
render() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var shapePropType = process.env.NODE_ENV !== "production" ? PropTypes.shape({
foo: PropTypes.string
}) : {};
}) : {};;

var ComponentA = function ComponentA() {
return React.createElement("div", null);
Expand All @@ -14,7 +14,7 @@ ComponentA.propTypes = process.env.NODE_ENV !== "production" ? {
var somePropTypes = process.env.NODE_ENV !== "production" ? {
foo: PropTypes.string,
bar: PropTypes.number
} : {};
} : {};;

var ComponentB = function ComponentB() {
return React.createElement("div", null);
Expand All @@ -26,7 +26,7 @@ ComponentB.propTypes = process.env.NODE_ENV !== "production" ? {
var somePropTypesC = process.env.NODE_ENV !== "production" ? {
foo: PropTypes.string,
bar: PropTypes.number
} : {};
} : {};;

var ComponentC = function ComponentC() {
return React.createElement("div", null);
Expand All @@ -38,7 +38,7 @@ ComponentC.propTypes = process.env.NODE_ENV !== "production" ? {
var somePropTypesD = process.env.NODE_ENV !== "production" ? {
foo: PropTypes.string,
bar: PropTypes.number
} : {};
} : {};;

var ComponentD = function ComponentD() {
return React.createElement("div", null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const shapePropType = process.env.NODE_ENV !== "production" ? PropTypes.shape({
foo: PropTypes.string
}) : {};
}) : {};;

const ComponentA = () => <div />;

Expand All @@ -10,7 +10,7 @@ ComponentA.propTypes = process.env.NODE_ENV !== "production" ? {
const somePropTypes = process.env.NODE_ENV !== "production" ? {
foo: PropTypes.string,
bar: PropTypes.number
} : {};
} : {};;

const ComponentB = () => <div />;

Expand All @@ -20,7 +20,7 @@ ComponentB.propTypes = process.env.NODE_ENV !== "production" ? {
const somePropTypesC = process.env.NODE_ENV !== "production" ? {
foo: PropTypes.string,
bar: PropTypes.number
} : {};
} : {};;

const ComponentC = () => <div />;

Expand All @@ -30,7 +30,7 @@ ComponentC.propTypes = process.env.NODE_ENV !== "production" ? {
const somePropTypesD = process.env.NODE_ENV !== "production" ? {
foo: PropTypes.string,
bar: PropTypes.number
} : {};
} : {};;

const ComponentD = () => <div />;

Expand Down
12 changes: 6 additions & 6 deletions test/fixtures/variable-assignment/expected-wrap-es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
exports.propTypesExported = void 0;
var propTypesBasic = process.env.NODE_ENV !== "production" ? {
foo: PropTypes.string
} : {};
} : {};;

var FooBasic = function FooBasic() {
return React.createElement("div", null);
Expand All @@ -15,10 +15,10 @@ var FooBasic = function FooBasic() {
FooBasic.propTypes = process.env.NODE_ENV !== "production" ? propTypesBasic : {};
var extraReference = process.env.NODE_ENV !== "production" ? {
bar: PropTypes.string
} : {};
} : {};;
var propTypesWithExtraReference = process.env.NODE_ENV !== "production" ? Object.assign({}, extraReference, {
foo: PropTypes.string
}) : {};
}) : {};;

var FooExtraReference = function FooExtraReference() {
return React.createElement("div", null);
Expand All @@ -29,7 +29,7 @@ var propTypesWithExtraReferenceSpread = process.env.NODE_ENV !== "production" ?
foo: PropTypes.string
}, {
bar: PropTypes.string
}) : {};
}) : {};;

var FooExtraReferenceSpread = function FooExtraReferenceSpread() {
return React.createElement("div", null);
Expand All @@ -38,7 +38,7 @@ var FooExtraReferenceSpread = function FooExtraReferenceSpread() {
FooExtraReferenceSpread.propTypes = process.env.NODE_ENV !== "production" ? propTypesWithExtraReferenceSpread : {};
var propTypesWrapped = process.env.NODE_ENV !== "production" ? {
foo: PropTypes.string
} : {};
} : {};;

var FooWrapped = function FooWrapped() {
return React.createElement("div", null);
Expand Down Expand Up @@ -68,7 +68,7 @@ var FooExported = function FooExported() {
FooExported.propTypes = process.env.NODE_ENV !== "production" ? propTypesExported : {};
var propTypesCreateClass = process.env.NODE_ENV !== "production" ? {
foo: PropTypes.string
} : {};
} : {};;
var FooCreateClass = createReactClass({
displayName: "FooCreateClass",
propTypes: propTypesCreateClass
Expand Down
12 changes: 6 additions & 6 deletions test/fixtures/variable-assignment/expected-wrap-es6.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const propTypesBasic = process.env.NODE_ENV !== "production" ? {
foo: PropTypes.string
} : {};
} : {};;

const FooBasic = () => <div />;

FooBasic.propTypes = process.env.NODE_ENV !== "production" ? propTypesBasic : {};
const extraReference = process.env.NODE_ENV !== "production" ? {
bar: PropTypes.string
} : {};
} : {};;
const propTypesWithExtraReference = process.env.NODE_ENV !== "production" ? Object.assign({}, extraReference, {
foo: PropTypes.string
}) : {};
}) : {};;

const FooExtraReference = () => <div />;

Expand All @@ -19,14 +19,14 @@ const propTypesWithExtraReferenceSpread = process.env.NODE_ENV !== "production"
foo: PropTypes.string
}, {
bar: PropTypes.string
}) : {};
}) : {};;

const FooExtraReferenceSpread = () => <div />;

FooExtraReferenceSpread.propTypes = process.env.NODE_ENV !== "production" ? propTypesWithExtraReferenceSpread : {};
const propTypesWrapped = process.env.NODE_ENV !== "production" ? {
foo: PropTypes.string
} : {};
} : {};;

const FooWrapped = () => <div />;

Expand All @@ -47,7 +47,7 @@ const FooExported = () => <div />;
FooExported.propTypes = process.env.NODE_ENV !== "production" ? propTypesExported : {};
const propTypesCreateClass = process.env.NODE_ENV !== "production" ? {
foo: PropTypes.string
} : {};
} : {};;
const FooCreateClass = createReactClass({
propTypes: propTypesCreateClass
});

0 comments on commit aadbac8

Please sign in to comment.