Skip to content

Commit

Permalink
Refactored to resemble eslint core util function naming. e.g. unwrap …
Browse files Browse the repository at this point in the history
…-> skip
  • Loading branch information
ota-meshi committed Jul 2, 2020
1 parent d365068 commit e3c0875
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 60 deletions.
4 changes: 2 additions & 2 deletions lib/rules/custom-event-name-casing.js
Expand Up @@ -48,7 +48,7 @@ function getNameParamNode(node) {
* @param {CallExpression} node CallExpression
*/
function getCalleeMemberNode(node) {
const callee = utils.unwrapChainExpression(node.callee)
const callee = utils.skipChainExpression(node.callee)

if (callee.type === 'MemberExpression') {
const name = utils.getStaticPropertyName(callee)
Expand Down Expand Up @@ -116,7 +116,7 @@ module.exports = {
utils.compositingVisitors(
utils.defineVueVisitor(context, {
onSetupFunctionEnter(node, { node: vueNode }) {
const contextParam = utils.unwrapAssignmentPattern(node.params[1])
const contextParam = utils.skipDefaultParamValue(node.params[1])
if (!contextParam) {
// no arguments
return
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-async-in-computed-properties.js
Expand Up @@ -26,7 +26,7 @@ const TIMED_FUNCTIONS = [
* @param {CallExpression} node
*/
function isTimedFunction(node) {
const callee = utils.unwrapChainExpression(node.callee)
const callee = utils.skipChainExpression(node.callee)
return (
((node.type === 'CallExpression' &&
callee.type === 'Identifier' &&
Expand All @@ -45,7 +45,7 @@ function isTimedFunction(node) {
* @param {CallExpression} node
*/
function isPromise(node) {
const callee = utils.unwrapChainExpression(node.callee)
const callee = utils.skipChainExpression(node.callee)
if (node.type === 'CallExpression' && callee.type === 'MemberExpression') {
return (
// hello.PROMISE_FUNCTION()
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-deprecated-events-api.js
Expand Up @@ -48,7 +48,7 @@ module.exports = {
}

if (
utils.unwrapChainExpression(call.callee) !== node ||
utils.skipChainExpression(call.callee) !== node ||
!['$on', '$off', '$once'].includes(
utils.getStaticPropertyName(node) || ''
)
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-deprecated-vue-config-keycodes.js
Expand Up @@ -33,7 +33,7 @@ module.exports = {
"MemberExpression[property.type='Identifier'][property.name='keyCodes']"(
node
) {
const config = utils.unwrapChainExpression(node.object)
const config = utils.skipChainExpression(node.object)
if (
config.type !== 'MemberExpression' ||
config.property.type !== 'Identifier' ||
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-multiple-slot-args.js
Expand Up @@ -100,7 +100,7 @@ module.exports = {
return utils.defineVueVisitor(context, {
/** @param {MemberExpression} node */
MemberExpression(node) {
const object = utils.unwrapChainExpression(node.object)
const object = utils.skipChainExpression(node.object)
if (object.type !== 'MemberExpression') {
return
}
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/no-setup-props-destructure.js
Expand Up @@ -49,7 +49,7 @@ module.exports = {
return
}

const rightNode = utils.unwrapChainExpression(right)
const rightNode = utils.skipChainExpression(right)
if (
left.type !== 'ArrayPattern' &&
left.type !== 'ObjectPattern' &&
Expand All @@ -60,7 +60,7 @@ module.exports = {
/** @type {Expression | Super} */
let rightId = rightNode
while (rightId.type === 'MemberExpression') {
rightId = utils.unwrapChainExpression(rightId.object)
rightId = utils.skipChainExpression(rightId.object)
}
if (rightId.type === 'Identifier' && propsReferenceIds.has(rightId)) {
report(left, 'getProperty')
Expand All @@ -84,7 +84,7 @@ module.exports = {
}
},
onSetupFunctionEnter(node) {
const propsParam = utils.unwrapAssignmentPattern(node.params[0])
const propsParam = utils.skipDefaultParamValue(node.params[0])
if (!propsParam) {
// no arguments
return
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/require-default-prop.js
Expand Up @@ -123,7 +123,7 @@ module.exports = {
* @return {Boolean}
*/
function isBooleanProp(prop) {
const value = utils.unwrapTypes(prop.value)
const value = utils.skipTSAsExpression(prop.value)

return (
isValueNodeOfBooleanType(value) ||
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/require-explicit-emits.js
Expand Up @@ -261,7 +261,7 @@ module.exports = {
* @param {VueObjectData} data
*/
'CallExpression[arguments.0.type=Literal]'(node, { node: vueNode }) {
const callee = utils.unwrapChainExpression(node.callee)
const callee = utils.skipChainExpression(node.callee)
const nameLiteralNode = node.arguments[0]
if (!nameLiteralNode || typeof nameLiteralNode.value !== 'string') {
// cannot check
Expand All @@ -288,7 +288,7 @@ module.exports = {
// verify setup(props,{emit}) {emit()}
verify(emitsDeclarations, nameLiteralNode, vueNode)
} else if (emit && emit.name === 'emit') {
const memObject = utils.unwrapChainExpression(emit.member.object)
const memObject = utils.skipChainExpression(emit.member.object)
if (
memObject.type === 'Identifier' &&
contextReferenceIds.has(memObject)
Expand All @@ -301,7 +301,7 @@ module.exports = {

// verify $emit
if (emit && emit.name === '$emit') {
const memObject = utils.unwrapChainExpression(emit.member.object)
const memObject = utils.skipChainExpression(emit.member.object)
if (utils.isThis(memObject, context)) {
// verify this.$emit()
verify(emitsDeclarations, nameLiteralNode, vueNode)
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/require-slots-as-functions.js
Expand Up @@ -103,7 +103,7 @@ module.exports = {
return utils.defineVueVisitor(context, {
/** @param {MemberExpression} node */
MemberExpression(node) {
const object = utils.unwrapChainExpression(node.object)
const object = utils.skipChainExpression(node.object)
if (object.type !== 'MemberExpression') {
return
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/require-valid-default-prop.js
Expand Up @@ -126,7 +126,7 @@ module.exports = {
* @returns { StandardValueType | FunctionExprValueType | FunctionValueType | null }
*/
function getValueType(targetNode) {
const node = utils.unwrapChainExpression(targetNode)
const node = utils.skipChainExpression(targetNode)
if (node.type === 'CallExpression') {
// Symbol(), Number() ...
if (
Expand Down
90 changes: 46 additions & 44 deletions lib/utils/index.js
Expand Up @@ -681,15 +681,15 @@ module.exports = {
type: 'object',
key: prop.key,
propName,
value: unwrapTypes(prop.value),
value: skipTSAsExpression(prop.value),
node: prop
}
}
return {
type: 'object',
key: null,
propName: null,
value: unwrapTypes(prop.value),
value: skipTSAsExpression(prop.value),
node: prop
}
})
Expand Down Expand Up @@ -754,15 +754,15 @@ module.exports = {
type: 'object',
key: prop.key,
emitName,
value: unwrapTypes(prop.value),
value: skipTSAsExpression(prop.value),
node: prop
}
}
return {
type: 'object',
key: null,
emitName: null,
value: unwrapTypes(prop.value),
value: skipTSAsExpression(prop.value),
node: prop
}
})
Expand Down Expand Up @@ -823,7 +823,7 @@ module.exports = {
.map((cp) => {
const key = getStaticPropertyName(cp)
/** @type {Expression} */
const propValue = unwrapTypes(cp.value)
const propValue = skipTSAsExpression(cp.value)
/** @type {BlockStatement | null} */
let value = null

Expand Down Expand Up @@ -1017,7 +1017,7 @@ module.exports = {
const callee = callExpr.callee

if (callee.type === 'MemberExpression') {
const calleeObject = unwrapTypes(callee.object)
const calleeObject = skipTSAsExpression(callee.object)

if (
calleeObject.type === 'Identifier' &&
Expand Down Expand Up @@ -1271,11 +1271,11 @@ module.exports = {
getMemberChaining(node) {
/** @type {MemberExpression[]} */
const nodes = []
let n = unwrapChainExpression(node)
let n = skipChainExpression(node)

while (n.type === 'MemberExpression') {
nodes.push(n)
n = unwrapChainExpression(n.object)
n = skipChainExpression(n.object)
}

return [n, ...nodes.reverse()]
Expand Down Expand Up @@ -1339,24 +1339,17 @@ module.exports = {
*/
isPropertyChain,
/**
* Unwrap typescript types like "X as F"
* @template T
* @param {T} node
* @return {T}
* Retrieve `TSAsExpression#expression` value if the given node a `TSAsExpression` node. Otherwise, pass through it.
*/
unwrapTypes,
skipTSAsExpression,
/**
* Unwrap AssignmentPattern like "(a = 1) => ret"
* @param { AssignmentPattern | RestElement | ArrayPattern | ObjectPattern | Identifier } node
* @return { RestElement | ArrayPattern | ObjectPattern | Identifier}
* Retrieve `AssignmentPattern#left` value if the given node a `AssignmentPattern` node. Otherwise, pass through it.
*/
unwrapAssignmentPattern,
skipDefaultParamValue,
/**
* Unwrap ChainExpression like "(a?.b)"
* @param { Expression | Super } node
* @return { Expression | Super }
* Retrieve `ChainExpression#expression` value if the given node a `ChainExpression` node. Otherwise, pass through it.
*/
unwrapChainExpression,
skipChainExpression,

/**
* Check whether the given node is `this` or variable that stores `this`.
Expand Down Expand Up @@ -1617,20 +1610,21 @@ function isVElement(node) {
}

/**
* Unwrap typescript types like "X as F"
* @template T
* @param {T} node
* @return {T}
* Retrieve `TSAsExpression#expression` value if the given node a `TSAsExpression` node. Otherwise, pass through it.
* @template T Node type
* @param {T | TSAsExpression} node The node to address.
* @returns {T} The `TSAsExpression#expression` value if the node is a `TSAsExpression` node. Otherwise, the node.
*/
function unwrapTypes(node) {
function skipTSAsExpression(node) {
if (!node) {
return node
}
// @ts-expect-error
if (node.type === 'TSAsExpression') {
// @ts-expect-error
return unwrapTypes(node.expression)
return skipTSAsExpression(node.expression)
}
// @ts-expect-error
return node
}

Expand Down Expand Up @@ -1661,36 +1655,40 @@ function isPropertyChain(prop, node) {
}

/**
* Unwrap AssignmentPattern like "(a = 1) => ret"
* @param { AssignmentPattern | RestElement | ArrayPattern | ObjectPattern | Identifier } node
* @return { RestElement | ArrayPattern | ObjectPattern | Identifier }
* Retrieve `AssignmentPattern#left` value if the given node a `AssignmentPattern` node. Otherwise, pass through it.
* @template T Node type
* @param {T | AssignmentPattern} node The node to address.
* @return {T} The `AssignmentPattern#left` value if the node is a `AssignmentPattern` node. Otherwise, the node.
*/
function unwrapAssignmentPattern(node) {
function skipDefaultParamValue(node) {
if (!node) {
return node
}
// @ts-expect-error
if (node.type === 'AssignmentPattern') {
// @ts-expect-error
return unwrapAssignmentPattern(node.left)
return skipDefaultParamValue(node.left)
}
// @ts-expect-error
return node
}

/**
* Unwrap ChainExpression like "(a?.b)"
* @template T
* @param {T} node
* @return {T}
* Retrieve `ChainExpression#expression` value if the given node a `ChainExpression` node. Otherwise, pass through it.
* @template T Node type
* @param {T | ChainExpression} node The node to address.
* @returns {T} The `ChainExpression#expression` value if the node is a `ChainExpression` node. Otherwise, the node.
*/
function unwrapChainExpression(node) {
function skipChainExpression(node) {
if (!node) {
return node
}
// @ts-expect-error
if (node.type === 'ChainExpression') {
// @ts-expect-error
return unwrapChainExpression(node.expression)
return skipChainExpression(node.expression)
}
// @ts-expect-error
return node
}

Expand Down Expand Up @@ -1792,7 +1790,7 @@ function isVueComponent(node) {
const callee = node.callee

if (callee.type === 'MemberExpression') {
const calleeObject = unwrapTypes(callee.object)
const calleeObject = skipTSAsExpression(callee.object)

if (calleeObject.type === 'Identifier') {
const propName = getStaticPropertyName(callee)
Expand Down Expand Up @@ -1846,7 +1844,8 @@ function isVueComponent(node) {
function isObjectArgument(node) {
return (
node.arguments.length > 0 &&
unwrapTypes(node.arguments.slice(-1)[0]).type === 'ObjectExpression'
skipTSAsExpression(node.arguments.slice(-1)[0]).type ===
'ObjectExpression'
)
}
}
Expand All @@ -1864,7 +1863,7 @@ function isVueInstance(node) {
callee.type === 'Identifier' &&
callee.name === 'Vue' &&
node.arguments.length &&
unwrapTypes(node.arguments[0]).type === 'ObjectExpression'
skipTSAsExpression(node.arguments[0]).type === 'ObjectExpression'
)
}

Expand All @@ -1884,21 +1883,24 @@ function getVueObjectType(context, node) {
const filePath = context.getFilename()
if (
isVueComponentFile(parent, filePath) &&
unwrapTypes(parent.declaration) === node
skipTSAsExpression(parent.declaration) === node
) {
return 'export'
}
} else if (parent.type === 'CallExpression') {
// Vue.component('xxx', {}) || component('xxx', {})
if (
isVueComponent(parent) &&
unwrapTypes(parent.arguments.slice(-1)[0]) === node
skipTSAsExpression(parent.arguments.slice(-1)[0]) === node
) {
return 'definition'
}
} else if (parent.type === 'NewExpression') {
// new Vue({})
if (isVueInstance(parent) && unwrapTypes(parent.arguments[0]) === node) {
if (
isVueInstance(parent) &&
skipTSAsExpression(parent.arguments[0]) === node
) {
return 'instance'
}
}
Expand Down

0 comments on commit e3c0875

Please sign in to comment.