Skip to content

Commit 455e852

Browse files
authored
Add vue/no-sparse-arrays rule (#1243)
1 parent a7f95ce commit 455e852

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ The following rules extend the rules provided by ESLint itself and apply them to
338338
| [vue/no-extra-parens](./no-extra-parens.md) | disallow unnecessary parentheses | :wrench: |
339339
| [vue/no-irregular-whitespace](./no-irregular-whitespace.md) | disallow irregular whitespace | |
340340
| [vue/no-restricted-syntax](./no-restricted-syntax.md) | disallow specified syntax | |
341+
| [vue/no-sparse-arrays](./no-sparse-arrays.md) | disallow sparse arrays | |
341342
| [vue/no-useless-concat](./no-useless-concat.md) | disallow unnecessary concatenation of literals or template literals | |
342343
| [vue/object-curly-newline](./object-curly-newline.md) | enforce consistent line breaks inside braces | :wrench: |
343344
| [vue/object-curly-spacing](./object-curly-spacing.md) | enforce consistent spacing inside braces | :wrench: |

docs/rules/no-sparse-arrays.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
pageClass: rule-details
3+
sidebarDepth: 0
4+
title: vue/no-sparse-arrays
5+
description: disallow sparse arrays
6+
---
7+
# vue/no-sparse-arrays
8+
> disallow sparse arrays
9+
10+
This rule is the same rule as core [no-sparse-arrays] rule but it applies to the expressions in `<template>`.
11+
12+
## :books: Further reading
13+
14+
- [no-sparse-arrays]
15+
16+
[no-sparse-arrays]: https://eslint.org/docs/rules/no-sparse-arrays
17+
18+
## :mag: Implementation
19+
20+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-sparse-arrays.js)
21+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-sparse-arrays.js)
22+
23+
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/no-sparse-arrays)</sup>

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ module.exports = {
9494
'no-shared-component-data': require('./rules/no-shared-component-data'),
9595
'no-side-effects-in-computed-properties': require('./rules/no-side-effects-in-computed-properties'),
9696
'no-spaces-around-equal-signs-in-attribute': require('./rules/no-spaces-around-equal-signs-in-attribute'),
97+
'no-sparse-arrays': require('./rules/no-sparse-arrays'),
9798
'no-static-inline-styles': require('./rules/no-static-inline-styles'),
9899
'no-template-key': require('./rules/no-template-key'),
99100
'no-template-shadow': require('./rules/no-template-shadow'),

lib/rules/no-sparse-arrays.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @author Yosuke Ota
3+
*/
4+
'use strict'
5+
6+
const { wrapCoreRule } = require('../utils')
7+
8+
// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
9+
module.exports = wrapCoreRule(require('eslint/lib/rules/no-sparse-arrays'))

tests/lib/rules/no-sparse-arrays.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @author Yosuke Ota
3+
*/
4+
'use strict'
5+
6+
const RuleTester = require('eslint').RuleTester
7+
const rule = require('../../../lib/rules/no-sparse-arrays')
8+
9+
const tester = new RuleTester({
10+
parser: require.resolve('vue-eslint-parser'),
11+
parserOptions: { ecmaVersion: 2015 }
12+
})
13+
14+
tester.run('no-sparse-arrays', rule, {
15+
valid: [
16+
`<template>
17+
<div :class="['foo', 'bar']" />
18+
</template>`,
19+
`<template>
20+
<div v-bind:[['foo'][0]]="bar" />
21+
</template>`
22+
],
23+
invalid: [
24+
{
25+
code: `
26+
<template>
27+
<div :class="[, 'foo', 'bar']" />
28+
</template>`,
29+
errors: [
30+
{
31+
message: 'Unexpected comma in middle of array.',
32+
line: 3,
33+
column: 22,
34+
endLine: 3,
35+
endColumn: 38
36+
}
37+
]
38+
},
39+
{
40+
code: `
41+
<template>
42+
<div v-bind:[[,'foo'][1]]="bar" />
43+
</template>`,
44+
errors: [
45+
{
46+
message: 'Unexpected comma in middle of array.',
47+
line: 3,
48+
column: 22,
49+
endLine: 3,
50+
endColumn: 30
51+
}
52+
]
53+
}
54+
]
55+
})

0 commit comments

Comments
 (0)