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

Add vue/no-sparse-arrays rule #1243

Merged
merged 1 commit into from Jul 15, 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
1 change: 1 addition & 0 deletions docs/rules/README.md
Expand Up @@ -338,6 +338,7 @@ The following rules extend the rules provided by ESLint itself and apply them to
| [vue/no-extra-parens](./no-extra-parens.md) | disallow unnecessary parentheses | :wrench: |
| [vue/no-irregular-whitespace](./no-irregular-whitespace.md) | disallow irregular whitespace | |
| [vue/no-restricted-syntax](./no-restricted-syntax.md) | disallow specified syntax | |
| [vue/no-sparse-arrays](./no-sparse-arrays.md) | disallow sparse arrays | |
| [vue/no-useless-concat](./no-useless-concat.md) | disallow unnecessary concatenation of literals or template literals | |
| [vue/object-curly-newline](./object-curly-newline.md) | enforce consistent line breaks inside braces | :wrench: |
| [vue/object-curly-spacing](./object-curly-spacing.md) | enforce consistent spacing inside braces | :wrench: |
Expand Down
23 changes: 23 additions & 0 deletions docs/rules/no-sparse-arrays.md
@@ -0,0 +1,23 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/no-sparse-arrays
description: disallow sparse arrays
---
# vue/no-sparse-arrays
> disallow sparse arrays

This rule is the same rule as core [no-sparse-arrays] rule but it applies to the expressions in `<template>`.

## :books: Further reading

- [no-sparse-arrays]

[no-sparse-arrays]: https://eslint.org/docs/rules/no-sparse-arrays

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-sparse-arrays.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-sparse-arrays.js)

<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/no-sparse-arrays)</sup>
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -94,6 +94,7 @@ module.exports = {
'no-shared-component-data': require('./rules/no-shared-component-data'),
'no-side-effects-in-computed-properties': require('./rules/no-side-effects-in-computed-properties'),
'no-spaces-around-equal-signs-in-attribute': require('./rules/no-spaces-around-equal-signs-in-attribute'),
'no-sparse-arrays': require('./rules/no-sparse-arrays'),
'no-static-inline-styles': require('./rules/no-static-inline-styles'),
'no-template-key': require('./rules/no-template-key'),
'no-template-shadow': require('./rules/no-template-shadow'),
Expand Down
9 changes: 9 additions & 0 deletions lib/rules/no-sparse-arrays.js
@@ -0,0 +1,9 @@
/**
* @author Yosuke Ota
*/
'use strict'

const { wrapCoreRule } = require('../utils')

// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
module.exports = wrapCoreRule(require('eslint/lib/rules/no-sparse-arrays'))
55 changes: 55 additions & 0 deletions tests/lib/rules/no-sparse-arrays.js
@@ -0,0 +1,55 @@
/**
* @author Yosuke Ota
*/
'use strict'

const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/no-sparse-arrays')

const tester = new RuleTester({
parser: require.resolve('vue-eslint-parser'),
parserOptions: { ecmaVersion: 2015 }
})

tester.run('no-sparse-arrays', rule, {
valid: [
`<template>
<div :class="['foo', 'bar']" />
</template>`,
`<template>
<div v-bind:[['foo'][0]]="bar" />
</template>`
],
invalid: [
{
code: `
<template>
<div :class="[, 'foo', 'bar']" />
</template>`,
errors: [
{
message: 'Unexpected comma in middle of array.',
line: 3,
column: 22,
endLine: 3,
endColumn: 38
}
]
},
{
code: `
<template>
<div v-bind:[[,'foo'][1]]="bar" />
</template>`,
errors: [
{
message: 'Unexpected comma in middle of array.',
line: 3,
column: 22,
endLine: 3,
endColumn: 30
}
]
}
]
})