Skip to content

Commit

Permalink
Fix crash when using objectsInObjects option in `vue/object-curly-s…
Browse files Browse the repository at this point in the history
…pacing` rule. (#1515)
  • Loading branch information
ota-meshi committed Jun 13, 2021
1 parent 68b7502 commit 9d54bd6
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
46 changes: 46 additions & 0 deletions lib/utils/index.js
Expand Up @@ -101,6 +101,7 @@ const SVG_ELEMENT_NAMES = new Set(require('./svg-elements.json'))
const VOID_ELEMENT_NAMES = new Set(require('./void-elements.json'))
const path = require('path')
const vueEslintParser = require('vue-eslint-parser')
const traverseNodes = vueEslintParser.AST.traverseNodes
const { findVariable } = require('eslint-utils')

/**
Expand Down Expand Up @@ -142,11 +143,56 @@ function wrapContextToOverrideTokenMethods(context, tokenStore) {
: []
return tokensAndComments
}

/** @param {number} index */
function getNodeByRangeIndex(index) {
const templateBody = eslintSourceCode.ast.templateBody
if (!templateBody) {
return eslintSourceCode.ast
}

/** @type {ASTNode} */
let result = eslintSourceCode.ast
/** @type {ASTNode[]} */
const skipNodes = []
let breakFlag = false

traverseNodes(templateBody, {
enterNode(node, parent) {
if (breakFlag) {
return
}
if (skipNodes[0] === parent) {
skipNodes.unshift(node)
return
}
if (node.range[0] <= index && index < node.range[1]) {
result = node
} else {
skipNodes.unshift(node)
}
},
leaveNode(node) {
if (breakFlag) {
return
}
if (result === node) {
breakFlag = true
} else if (skipNodes[0] === node) {
skipNodes.shift()
}
}
})
return result
}
const sourceCode = new Proxy(Object.assign({}, eslintSourceCode), {
get(_object, key) {
if (key === 'tokensAndComments') {
return getTokensAndComments()
}
if (key === 'getNodeByRangeIndex') {
return getNodeByRangeIndex
}
// @ts-expect-error
return key in tokenStore ? tokenStore[key] : eslintSourceCode[key]
}
Expand Down
60 changes: 60 additions & 0 deletions tests/lib/rules/object-curly-spacing.js
Expand Up @@ -26,6 +26,20 @@ tester.run('object-curly-spacing', rule, {
{
code: '<template><div :[{a:1}]="a" /></template>',
options: ['always']
},
{
code: `
<template>
<div v-bind="{foo: {bar: 'baz'} }">
Hello World
</div>
</template>`,
options: [
'never',
{
objectsInObjects: true
}
]
}
],
invalid: [
Expand Down Expand Up @@ -97,6 +111,52 @@ tester.run('object-curly-spacing', rule, {
"A space is required after '{'.",
"A space is required before '}'."
]
},
{
code: `
<template>
<div v-bind="{ foo: { bar: 'baz' }}">
Hello World
</div>
</template>`,
options: [
'never',
{
objectsInObjects: true
}
],
output: `
<template>
<div v-bind="{foo: {bar: 'baz'} }">
Hello World
</div>
</template>`,
errors: [
"There should be no space after '{'.",
"There should be no space after '{'.",
"There should be no space before '}'.",
"A space is required before '}'."
]
},
{
code: `
<template>
<div v-bind="{ foo: { bar: 'baz' }}">
Hello World
</div>
</template>`,
options: ['never'],
output: `
<template>
<div v-bind="{foo: {bar: 'baz'}}">
Hello World
</div>
</template>`,
errors: [
"There should be no space after '{'.",
"There should be no space after '{'.",
"There should be no space before '}'."
]
}
]
})

0 comments on commit 9d54bd6

Please sign in to comment.