Skip to content

Commit

Permalink
Fix iterateProperties to support arrow functions (#1064)
Browse files Browse the repository at this point in the history
* Fix iterateProperties to support arrow functions

* indent mistake

* add no-dupe-keys test cases using arrow function

* add no-reserved-keys no-template-shadow test cases using arrow function

Co-authored-by: maddocnc <maddocnc@gmail.com>
  • Loading branch information
maddocnc and maddocnc committed Mar 14, 2020
1 parent 3cc5ac0 commit 7a790bc
Show file tree
Hide file tree
Showing 4 changed files with 398 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/utils/index.js
Expand Up @@ -682,6 +682,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 @@ -734,6 +736,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

0 comments on commit 7a790bc

Please sign in to comment.