Skip to content

Fix 'no-shared-component-data' false positive when using arrow functions #156

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
Aug 16, 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
5 changes: 2 additions & 3 deletions lib/rules/no-shared-component-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function create (context) {
p.key.type === 'Identifier' &&
p.key.name === 'data' &&
p.value.type !== 'FunctionExpression' &&
p.value.type !== 'ArrowFunctionExpression' &&
p.value.type !== 'Identifier'
)
.forEach(cp => {
Expand All @@ -41,9 +42,7 @@ module.exports = {
recommended: false
},
fixable: null, // or "code" or "whitespace"
schema: [
// fill in your schema
]
schema: []
},

create
Expand Down
38 changes: 26 additions & 12 deletions tests/lib/rules/no-shared-component-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const rule = require('../../../lib/rules/no-shared-component-data')

const RuleTester = require('eslint').RuleTester

const parserOptions = {
ecmaVersion: 7,
sourceType: 'module',
ecmaFeatures: { experimentalObjectRestSpread: true }
}

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
Expand All @@ -30,7 +36,8 @@ ruleTester.run('no-shared-component-data', rule, {
}
}
})
`
`,
parserOptions
},
{
filename: 'test.js',
Expand All @@ -44,11 +51,7 @@ ruleTester.run('no-shared-component-data', rule, {
}
})
`,
parserOptions: {
ecmaVersion: 7,
sourceType: 'module',
ecmaFeatures: { experimentalObjectRestSpread: true }
}
parserOptions
},
{
filename: 'test.js',
Expand All @@ -71,7 +74,7 @@ ruleTester.run('no-shared-component-data', rule, {
}
})
`,
parserOptions: { ecmaVersion: 6 }
parserOptions
},
{
filename: 'test.vue',
Expand All @@ -84,7 +87,7 @@ ruleTester.run('no-shared-component-data', rule, {
}
}
`,
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
parserOptions
},
{
filename: 'test.vue',
Expand All @@ -93,7 +96,7 @@ ruleTester.run('no-shared-component-data', rule, {
...foo
}
`,
parserOptions: { ecmaVersion: 6, sourceType: 'module', ecmaFeatures: { experimentalObjectRestSpread: true }}
parserOptions
},
{
filename: 'test.vue',
Expand All @@ -102,7 +105,18 @@ ruleTester.run('no-shared-component-data', rule, {
data
}
`,
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
parserOptions
},
{
filename: 'test.vue',
code: `
export default {
data: () => {

}
}
`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that this test case is not correct.
The filename is .vue and the code is in outside of <script> element.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is sort of correct @mysticatea I think. This plugin doesn't transform <script></script> from a vue file. It's part of eslint-vue-parser responsibilities so here we expect that any code inside script tag will be accessible as usual. That's why we write this code directly without extra script tag. All we need to know in our rules is the extension of the file to detect if simple export default should be treated as component.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I was under the impression that parserOptions has parser: "vue-eslint-parser". I'm sorry.

parserOptions
}
],

Expand All @@ -116,7 +130,7 @@ ruleTester.run('no-shared-component-data', rule, {
}
})
`,
parserOptions: { ecmaVersion: 6 },
parserOptions,
errors: [{
message: '`data` property in component must be a function',
line: 3
Expand All @@ -131,7 +145,7 @@ ruleTester.run('no-shared-component-data', rule, {
}
}
`,
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
parserOptions,
errors: [{
message: '`data` property in component must be a function',
line: 3
Expand Down