Skip to content

Commit

Permalink
fix: Add babel transform plugin to remove prop-types
Browse files Browse the repository at this point in the history
  • Loading branch information
znck committed Jan 19, 2019
1 parent aa3f80c commit 49ae0d5
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
13 changes: 12 additions & 1 deletion .babelrc
@@ -1,3 +1,14 @@
{
"presets": ["@babel/preset-env"]
"presets": [
[
"@babel/preset-env",
{
"targets": {
"esmodules": true,
"node": "current",
"safari": "tp"
}
}
]
]
}
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -49,5 +49,6 @@
},
"dependencies": {
"vue": "2.*"
}
},
"sideEffects": false
}
4 changes: 1 addition & 3 deletions rollup.config.js
Expand Up @@ -4,9 +4,7 @@ import pkg from './package.json'
const banner = `
/**
* ${pkg.name} v${pkg.version}
* (c) ${new Date().getFullYear()} ${pkg.author.name} <${pkg.author.email}> (${
pkg.author.url
})
* (c) ${new Date().getFullYear()} ${pkg.author}
* @license ${pkg.license}
*/`.trim()

Expand Down
6 changes: 3 additions & 3 deletions src/helpers.js
Expand Up @@ -8,8 +8,8 @@ export const TYPES = {
array: Array,
}

export const typeNames = Object.keys(TYPES)
export const typeValues = Object.values(TYPES)
export const typeNames = () => Object.keys(TYPES)
export const typeValues = () => Object.values(TYPES)

export function runValidation(validator, value, strict = false) {
const types = ensureArray(validator.type)
Expand All @@ -29,7 +29,7 @@ export function isType(type, item, nullAllowed = false) {
if (Array === type && Array.isArray(item)) return true

return Object.entries(TYPES).some(
([key, TYPE]) => TYPE === type && typeof item === key // eslint-disable-line valid-typeof
({ 0: key, 1: TYPE }) => TYPE === type && typeof item === key // eslint-disable-line valid-typeof
)
}

Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Expand Up @@ -157,14 +157,14 @@ export default class PropTypes {
const prop = this.create(Object)
const shapeType = {}

Object.entries(shape).forEach(([key, value]) => {
Object.entries(shape).forEach(({0: key, 1: value}) => {
shapeType[key] = normalizeType(value)
})

prop.validator = value => {
if (!(value && typeof value === 'object')) return prop.required !== true

return Object.entries(shapeType).every(([key, type]) =>
return Object.entries(shapeType).every(({0: key, 1: type}) =>
runValidation(type, value[key])
)
}
Expand All @@ -188,7 +188,7 @@ function normalizeType(type) {

if (type in TYPES) return PropTypes.create(TYPES[type])

if (typeValues.includes(type)) return PropTypes.create(type)
if (typeValues().includes(type)) return PropTypes.create(type)

if (typeof type === 'function') return { validator: type, type: [] }

Expand Down
32 changes: 21 additions & 11 deletions src/remove.js
Expand Up @@ -24,15 +24,24 @@ export default function(babel) {

path.node._processed = true

const prop = path.findParent(path => path.isObjectProperty())
let prop = path.findParent(path => path.isObjectProperty())

if (prop) {
if (oldProps) {
prop.skip()
path.stop()
return
}
const props = prop.parentPath
if (props && props.isObjectExpression()) {
const api = props.parentPath
if (
api &&
api.isObjectProperty() &&
api.get('key').isIdentifier({ name: 'props' })
) {
// continue
prop = prop
} else prop = null
} else prop = null
}

if (prop) {
const info = {}

prop.get('value').traverse({
Expand Down Expand Up @@ -127,11 +136,12 @@ export default function(babel) {
newProps = t.arrayExpression(properties)
}

oldProps.replaceWith(
babel.template.ast`process.env.NODE_ENV !== 'production' ? ${
oldProps.node
} : ${newProps}`
)
const node = babel.template
.ast`process.env.NODE_ENV !== 'production' ? ${
oldProps.node
} : ${newProps}`

oldProps.replaceWith(node)
}
} else {
const statement = path.findParent(path =>
Expand Down

0 comments on commit 49ae0d5

Please sign in to comment.