Skip to content

[Update] Make vue/no-shared-component-data fixable #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions lib/rules/no-shared-component-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,33 @@

const utils = require('../utils')

function isOpenParen (token) {
return token.type === 'Punctuator' && token.value === '('
}

function isCloseParen (token) {
return token.type === 'Punctuator' && token.value === ')'
}

function getFirstAndLastTokens (node, sourceCode) {
let first = sourceCode.getFirstToken(node)
let last = sourceCode.getLastToken(node)

// If the value enclosed by parentheses, update the 'first' and 'last' by the parentheses.
while (true) {
const prev = sourceCode.getTokenBefore(first)
const next = sourceCode.getTokenAfter(last)
if (isOpenParen(prev) && isCloseParen(next)) {
first = prev
last = next
} else {
return { first, last }
}
}
}

function create (context) {
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
const sourceCode = context.getSourceCode()

return utils.executeOnVueComponent(context, (obj) => {
obj.properties
Expand All @@ -21,10 +44,24 @@ function create (context) {
p.value.type !== 'ArrowFunctionExpression' &&
p.value.type !== 'Identifier'
)
.forEach(cp => {
.forEach(p => {
context.report({
node: cp.value,
message: '`data` property in component must be a function'
node: p,
message: '`data` property in component must be a function',
fix (fixer) {
const tokens = getFirstAndLastTokens(p.value, sourceCode)

// If we can upgrade requirements to `eslint@>4.1.0`, this code can be replaced by:
// return [
// fixer.insertTextBefore(tokens.first, 'function() {\nreturn '),
// fixer.insertTextAfter(tokens.last, ';\n}')
// ]
// See: https://eslint.org/blog/2017/06/eslint-v4.1.0-released#applying-multiple-autofixes-simultaneously
const range = [tokens.first.range[0], tokens.last.range[1]]
const valueText = sourceCode.text.slice(range[0], range[1])
const replacement = `function() {\nreturn ${valueText};\n}`
return fixer.replaceTextRange(range, replacement)
}
})
})
})
Expand All @@ -40,7 +77,7 @@ module.exports = {
description: "enforce component's data property to be a function",
category: 'essential'
},
fixable: null, // or "code" or "whitespace"
fixable: 'code',
schema: []
},

Expand Down
42 changes: 42 additions & 0 deletions tests/lib/rules/no-shared-component-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ ruleTester.run('no-shared-component-data', rule, {
}
})
`,
output: `
Vue.component('some-comp', {
data: function() {
return {
foo: 'bar'
};
}
})
`,
parserOptions,
errors: [{
message: '`data` property in component must be a function',
Expand All @@ -145,6 +154,39 @@ ruleTester.run('no-shared-component-data', rule, {
}
}
`,
output: `
export default {
data: function() {
return {
foo: 'bar'
};
}
}
`,
parserOptions,
errors: [{
message: '`data` property in component must be a function',
line: 3
}]
},
{
filename: 'test.vue',
code: `
export default {
data: /*a*/ (/*b*/{
foo: 'bar'
})
}
`,
output: `
export default {
data: /*a*/ function() {
return (/*b*/{
foo: 'bar'
});
}
}
`,
parserOptions,
errors: [{
message: '`data` property in component must be a function',
Expand Down