oliviertassinari / babel-plugin-transform-react-remove-prop-types

Remove unnecessary React propTypes from the production build. :balloon:
MIT License
897 stars 61 forks source link

Fix extraneous semicolon #168

Closed eps1lon closed 5 years ago

eps1lon commented 5 years ago

Followup on #167

This was caused because the transformed AST was invalid when VariableDeclarator was wrapped because it returned an AssignmentExpression that was used as the variableDeclarator for a VariableDeclaration.

To be more specific it previously created nodes similar to this:

{
  "type": "VariableDeclaration",
  "declarations": [
    {
      "type": "ExpressionStatement",
      "expression": {
        "type": "AssignmentExpression",
        "operator": "=",
        "left": {
          "type": "Identifier",
          "loc": {
            "identifierName": "referencedPropTypes"
          },
          "name": "referencedPropTypes"
        },
        "right": {
          "type": "ConditionalExpression",
          "test": {},
          "consequent": {},
          "alternate": {}
        }
      },
    }
  ],
  "kind": "const"
},
const referencedPropTypes = process.env.NODE_ENV !== "production" ? {} : {};;

which would never be created by any parser but @babel/generator is robust enough to handle it.

Now we get a valid AST:

{
  "type": "VariableDeclaration",
  "declarations": [
    {
      "type": "VariableDeclarator",
      "id": {
        "type": "Identifier",
        "loc": {
          "identifierName": "referencedPropTypes"
        },
        "name": "referencedPropTypes"
      },
      "init": {
        "type": "ConditionalExpression",
        "test": {},
        "consequent": {},
        "alternate": {},
        "extra": {
          "parenthesized": true,
          "parenStart": 0
        }
      }
    }
  ],
  "kind": "const"
},
const referencedPropTypes = process.env.NODE_ENV !== "production" ? {} : {};