Skip to content

Commit

Permalink
feat: Remove redundant expression containers. Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Jul 4, 2019
1 parent 3e7b08a commit ad027a8
Show file tree
Hide file tree
Showing 9 changed files with 653 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
/packages/*/node_modules
/packages/*/coverage
/packages/*/dist

# Test fixtures for jscodeshifts should be ignored, mainly because jscodeshift
# adds semicolons to its output, and also because we do not care if variables
# are defined or not in jscodeshift fixtures.
__testfixtures__
2 changes: 2 additions & 0 deletions packages/cozy-codeshifts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"build": "babel src -d dist --verbose"
},
"devDependencies": {
"jest": "^24.8.0",
"jscodeshift": "^0.6.4",
"jsdoc-to-markdown": "^5.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Old from './old'
import New from './old'
import React from 'react'
import flag from 'cozy-flags'

const Component = () => {
return flag('test-flag') ? <New /> : <Old />
}

const App = () => {
return (
<div>
{/* My comment */}
{flag('test-flag') ? <Component /> : null}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import New from './old'
import React from 'react'

const Component = () => {
return <New />;
}

const App = () => {
return (
<div>
{/* My comment */}
<Component />
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const defineTest = require('jscodeshift/dist/testUtils').defineTest
defineTest(__dirname, 'apply-flag')
4 changes: 3 additions & 1 deletion packages/cozy-codeshifts/src/transforms/apply-flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import simplifyConditions from '../utils/simplify-conditions'
import removeUnusedImports from '../utils/remove-unused-imports'
import replaceBooleanVars from '../utils/replace-boolean-vars'
import removeFalseJSXExpressionContainers from '../utils/remove-false-jsx-expression-containers'
import removeRedundantJSXExpressionContainers from '../utils/remove-redundant-jsx-expression-containers'

const flagCallFinder = name => ({
callee: { name: 'flag' },
Expand All @@ -14,7 +15,7 @@ export default function applyFlag(file, api) {

// Replace flags by true
const flagName =
typeof process !== 'undefined' ? process.env.FLAG : 'reimbursement-tag'
process.env.NODE_ENV === 'test' ? 'test-flag' : process.env.FLAG
const flags = root.find(j.CallExpression, flagCallFinder(flagName))
flags.forEach(flagCall => {
flagCall.replace(j.literal(true))
Expand All @@ -24,5 +25,6 @@ export default function applyFlag(file, api) {
simplifyConditions(root, j)
removeUnusedImports(root, j)
removeFalseJSXExpressionContainers(root, j)
removeRedundantJSXExpressionContainers(root, j)
return root.toSource()
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Remove redundant expression containers.
*
* @example
* <div>{<div></div>}</div> -> <div><div></div></div>
*
* @param {PathNode} root
* @param {Object} j
* @return
*/
const removeRedundantExpressionContainers = (root, j) => {
root.find(j.JSXExpressionContainer).forEach(path => {
if (
path.parentPath.node.type == 'JSXElement' &&
path.node.expression.type === 'JSXElement'
) {
path.replace(path.node.expression)
}
})
}

export default removeRedundantExpressionContainers
Loading

0 comments on commit ad027a8

Please sign in to comment.