Skip to content

Instantly share code, notes, and snippets.

@chrisvfritz
Last active December 6, 2017 08:07
Show Gist options
  • Save chrisvfritz/63055b655014d8a4f76f84ae4ce64e77 to your computer and use it in GitHub Desktop.
Save chrisvfritz/63055b655014d8a4f76f84ae4ce64e77 to your computer and use it in GitHub Desktop.
const topLevelPropertiesToRemove = [
'description',
'reusable',
'events',
'slots'
]
const propPropertiesToRemove = [
'description'
]
module.exports = ({ types: t }) => {
const visitor = {
ObjectProperty(path) {
if (
// Skip if the object property is computed
// e.g. `[computedKey]: value`
path.node.computed ||
// Skip if the object property is not in a
// .vue file
!isInVueFile(path)
) return
// Get the name of the property
const propertyName = getPropertyName(path)
// Remove any blacklisted top-level properties
if (
isTopLevelProperty(path) &&
topLevelPropertiesToRemove.includes(propertyName)
) return path.remove()
// Remove any blacklisted prop properties
if (
isOnAProp(path) &&
propPropertiesToRemove.includes(propertyName)
) return path.remove()
}
}
return { visitor }
function isInVueFile (path) {
return /\.vue$/.test(path.scope.hub.file.opts.filename)
}
function isTopLevelProperty (path) {
return path.parentPath.parentPath.type === 'ExportDefaultDeclaration'
}
function isOnAProp (path) {
return !!path.findParent(ancestorPath =>
ancestorPath.type === 'ObjectProperty' &&
getPropertyName(ancestorPath) === 'props'
)
}
function getPropertyName(path) {
const { node: { key } } = path
return t.isIdentifier(key)
? key.name
: t.isStringLiteral(key)
? key.value
: null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment