Last active
December 6, 2017 08:07
-
-
Save chrisvfritz/63055b655014d8a4f76f84ae4ce64e77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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