Skip to content

Commit 09bc534

Browse files
authored
Add vue/object-shorthand rule (#1762)
1 parent 5ed7397 commit 09bc534

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

Diff for: docs/rules/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ The following rules extend the rules provided by ESLint itself and apply them to
396396
| [vue/object-curly-newline](./object-curly-newline.md) | enforce consistent line breaks after opening and before closing braces | :wrench: |
397397
| [vue/object-curly-spacing](./object-curly-spacing.md) | enforce consistent spacing inside braces | :wrench: |
398398
| [vue/object-property-newline](./object-property-newline.md) | enforce placing object properties on separate lines | :wrench: |
399+
| [vue/object-shorthand](./object-shorthand.md) | require or disallow method and property shorthand syntax for object literals | :wrench: |
399400
| [vue/operator-linebreak](./operator-linebreak.md) | enforce consistent linebreak style for operators | :wrench: |
400401
| [vue/prefer-template](./prefer-template.md) | require template literals instead of string concatenation | :wrench: |
401402
| [vue/space-in-parens](./space-in-parens.md) | enforce consistent spacing inside parentheses | :wrench: |

Diff for: docs/rules/object-shorthand.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
pageClass: rule-details
3+
sidebarDepth: 0
4+
title: vue/object-shorthand
5+
description: require or disallow method and property shorthand syntax for object literals
6+
---
7+
# vue/object-shorthand
8+
9+
> require or disallow method and property shorthand syntax for object literals
10+
11+
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
12+
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.
13+
14+
This rule is the same rule as core [object-shorthand] rule but it applies to the expressions in `<template>`.
15+
16+
## :books: Further Reading
17+
18+
- [object-shorthand]
19+
20+
[object-shorthand]: https://eslint.org/docs/rules/object-shorthand
21+
22+
## :mag: Implementation
23+
24+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/object-shorthand.js)
25+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/object-shorthand.js)
26+
27+
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/object-shorthand)</sup>

Diff for: lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ module.exports = {
151151
'object-curly-newline': require('./rules/object-curly-newline'),
152152
'object-curly-spacing': require('./rules/object-curly-spacing'),
153153
'object-property-newline': require('./rules/object-property-newline'),
154+
'object-shorthand': require('./rules/object-shorthand'),
154155
'one-component-per-file': require('./rules/one-component-per-file'),
155156
'operator-linebreak': require('./rules/operator-linebreak'),
156157
'order-in-components': require('./rules/order-in-components'),

Diff for: lib/rules/object-shorthand.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @author Yosuke Ota
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
'use strict'
6+
7+
const { wrapCoreRule } = require('../utils')
8+
9+
// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
10+
module.exports = wrapCoreRule('object-shorthand', {
11+
skipDynamicArguments: true
12+
})

Diff for: tests/lib/rules/object-shorthand.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* @author Yosuke Ota
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
'use strict'
6+
7+
const RuleTester = require('eslint').RuleTester
8+
const rule = require('../../../lib/rules/object-shorthand')
9+
10+
const tester = new RuleTester({
11+
parser: require.resolve('vue-eslint-parser'),
12+
parserOptions: {
13+
ecmaVersion: 2020,
14+
sourceType: 'module'
15+
}
16+
})
17+
18+
tester.run('object-shorthand', rule, {
19+
valid: [
20+
{
21+
filename: 'test.vue',
22+
code: `
23+
<template>
24+
<div :style="{height}"></div>
25+
</template>
26+
`
27+
},
28+
{
29+
filename: 'test.vue',
30+
code: `
31+
<template>
32+
<div :style="{height: height}"></div>
33+
</template>
34+
`,
35+
options: ['never']
36+
}
37+
],
38+
invalid: [
39+
{
40+
filename: 'test.vue',
41+
code: `
42+
<template>
43+
<div :style="{height: height}"></div>
44+
</template>
45+
`,
46+
output: `
47+
<template>
48+
<div :style="{height}"></div>
49+
</template>
50+
`,
51+
errors: [
52+
{
53+
message: 'Expected property shorthand.',
54+
line: 3,
55+
column: 23
56+
}
57+
]
58+
},
59+
{
60+
filename: 'test.vue',
61+
code: `
62+
<template>
63+
<div :style="{height}"></div>
64+
</template>
65+
`,
66+
output: `
67+
<template>
68+
<div :style="{height: height}"></div>
69+
</template>
70+
`,
71+
options: ['never'],
72+
errors: [
73+
{
74+
message: 'Expected longform property syntax.',
75+
line: 3,
76+
column: 23
77+
}
78+
]
79+
}
80+
]
81+
})

0 commit comments

Comments
 (0)