Skip to content
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

Fix iterateProperties to support arrow functions #1064

Merged
merged 4 commits into from Mar 14, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/utils/index.js
Expand Up @@ -693,6 +693,8 @@ module.exports = {
yield * this.iterateObjectExpression(item.value, name)
} else if (item.value.type === 'FunctionExpression') {
yield * this.iterateFunctionExpression(item.value, name)
} else if (item.value.type === 'ArrowFunctionExpression') {
yield * this.iterateArrowFunctionExpression(item.value, name)
}
}
},
Expand Down Expand Up @@ -745,6 +747,24 @@ module.exports = {
}
},

/**
* Return generator with all elements inside ArrowFunctionExpression
* @param {ASTNode} node Node to check
* @param {string} groupName Name of parent group
*/
* iterateArrowFunctionExpression (node, groupName) {
assert(node.type === 'ArrowFunctionExpression')
if (node.body.type === 'BlockStatement') {
for (const item of node.body.body) {
if (item.type === 'ReturnStatement' && item.argument && item.argument.type === 'ObjectExpression') {
yield * this.iterateObjectExpression(item.argument, groupName)
}
}
} else if (node.body.type === 'ObjectExpression') {
yield * this.iterateObjectExpression(node.body, groupName)
}
},

/**
* Find all functions which do not always return values
* @param {boolean} treatUndefinedAsUnspecified
Expand Down
186 changes: 186 additions & 0 deletions tests/lib/rules/no-dupe-keys.js
Expand Up @@ -45,6 +45,58 @@ ruleTester.run('no-dupe-keys', rule, {
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
},

{
filename: 'test.vue',
code: `
export default {
props: ['foo'],
computed: {
bar () {
}
},
data: () => {
return {
dat: null
}
},
data: () => {
return
},
methods: {
_foo () {},
test () {
}
}
}
`,
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
},

{
filename: 'test.vue',
code: `
export default {
props: ['foo'],
computed: {
bar () {
}
},
data: () => ({
dat: null
}),
data: () => {
return
},
methods: {
_foo () {},
test () {
}
}
}
`,
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
},

{
filename: 'test.vue',
code: `
Expand Down Expand Up @@ -82,6 +134,78 @@ ruleTester.run('no-dupe-keys', rule, {
parserOptions: { ecmaVersion: 2018, sourceType: 'module' }
},

{
filename: 'test.vue',
code: `
export default {
...foo(),
props: {
...foo(),
foo: String
},
computed: {
...mapGetters({
test: 'getTest'
}),
bar: {
get () {
}
}
},
data: {
...foo(),
dat: null
},
methods: {
...foo(),
test () {
}
},
data: () => {
return {
...dat
}
},
}
`,
parserOptions: { ecmaVersion: 2018, sourceType: 'module' }
},

{
filename: 'test.vue',
code: `
export default {
...foo(),
props: {
...foo(),
foo: String
},
computed: {
...mapGetters({
test: 'getTest'
}),
bar: {
get () {
}
}
},
data: {
...foo(),
dat: null
},
methods: {
...foo(),
test () {
}
},
data: () => ({
...dat
}),
}
`,
parserOptions: { ecmaVersion: 2018, sourceType: 'module' }
},

{
filename: 'test.js',
code: `
Expand Down Expand Up @@ -136,6 +260,68 @@ ruleTester.run('no-dupe-keys', rule, {
line: 14
}]
},
{
filename: 'test.vue',
code: `
export default {
props: ['foo'],
computed: {
foo () {
}
},
data: () => {
return {
foo: null
}
},
methods: {
foo () {
}
}
}
`,
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
errors: [{
message: 'Duplicated key \'foo\'.',
line: 5
}, {
message: 'Duplicated key \'foo\'.',
line: 10
}, {
message: 'Duplicated key \'foo\'.',
line: 14
}]
},
{
filename: 'test.vue',
code: `
export default {
props: ['foo'],
computed: {
foo () {
}
},
data: () => ({
foo: null
}),
methods: {
foo () {
}
}
}
`,
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
errors: [{
message: 'Duplicated key \'foo\'.',
line: 5
}, {
message: 'Duplicated key \'foo\'.',
line: 9
}, {
message: 'Duplicated key \'foo\'.',
line: 12
}]
},
{
filename: 'test.vue',
code: `
Expand Down
76 changes: 76 additions & 0 deletions tests/lib/rules/no-reserved-keys.js
Expand Up @@ -45,6 +45,50 @@ ruleTester.run('no-reserved-keys', rule, {
}
`,
parserOptions
},
{
filename: 'test.vue',
code: `
export default {
props: ['foo'],
computed: {
bar () {
}
},
data: () => {
return {
dat: null
}
},
methods: {
_foo () {},
test () {
}
}
}
`,
parserOptions
},
{
filename: 'test.vue',
code: `
export default {
props: ['foo'],
computed: {
bar () {
}
},
data: () => ({
dat: null
}),
methods: {
_foo () {},
test () {
}
}
}
`,
parserOptions
}
],

Expand Down Expand Up @@ -79,6 +123,38 @@ ruleTester.run('no-reserved-keys', rule, {
line: 4
}]
},
{
filename: 'test.js',
code: `
new Vue({
data: () => {
return {
_foo: String
}
}
})
`,
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Keys starting with with '_' are reserved in '_foo' group.",
line: 5
}]
},
{
filename: 'test.js',
code: `
new Vue({
data: () => ({
_foo: String
})
})
`,
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Keys starting with with '_' are reserved in '_foo' group.",
line: 4
}]
},
{
filename: 'test.js',
code: `
Expand Down