diff --git a/lib/utils/index.js b/lib/utils/index.js index 011ec6571..418ab2f9c 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -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) } } }, @@ -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 diff --git a/tests/lib/rules/no-dupe-keys.js b/tests/lib/rules/no-dupe-keys.js index 1abb67642..f1a35568a 100644 --- a/tests/lib/rules/no-dupe-keys.js +++ b/tests/lib/rules/no-dupe-keys.js @@ -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: ` @@ -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: ` @@ -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: ` diff --git a/tests/lib/rules/no-reserved-keys.js b/tests/lib/rules/no-reserved-keys.js index 08c7f244c..05836bc47 100644 --- a/tests/lib/rules/no-reserved-keys.js +++ b/tests/lib/rules/no-reserved-keys.js @@ -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 } ], @@ -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: ` diff --git a/tests/lib/rules/no-template-shadow.js b/tests/lib/rules/no-template-shadow.js index 68fefce0e..08e0e03b6 100644 --- a/tests/lib/rules/no-template-shadow.js +++ b/tests/lib/rules/no-template-shadow.js @@ -70,6 +70,52 @@ ruleTester.run('no-template-shadow', rule, { } } ` + }, + { + filename: 'test.vue', + code: ` + ` + }, + { + filename: 'test.vue', + code: ` + ` } ], @@ -208,6 +254,76 @@ ruleTester.run('no-template-shadow', rule, { type: 'Identifier', line: 7 }] + }, + { + filename: 'test.vue', + code: ` + `, + errors: [{ + message: "Variable 'e' is already declared in the upper scope.", + type: 'Identifier', + line: 6 + }, { + message: "Variable 'f' is already declared in the upper scope.", + type: 'Identifier', + line: 7 + }] + }, + { + filename: 'test.vue', + code: ` + `, + errors: [{ + message: "Variable 'e' is already declared in the upper scope.", + type: 'Identifier', + line: 6 + }, { + message: "Variable 'f' is already declared in the upper scope.", + type: 'Identifier', + line: 7 + }] } ] })