Skip to content

Add vue/object-shorthand rule #1762

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

Merged
merged 1 commit into from
Jan 19, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ The following rules extend the rules provided by ESLint itself and apply them to
| [vue/object-curly-newline](./object-curly-newline.md) | enforce consistent line breaks after opening and before closing braces | :wrench: |
| [vue/object-curly-spacing](./object-curly-spacing.md) | enforce consistent spacing inside braces | :wrench: |
| [vue/object-property-newline](./object-property-newline.md) | enforce placing object properties on separate lines | :wrench: |
| [vue/object-shorthand](./object-shorthand.md) | require or disallow method and property shorthand syntax for object literals | :wrench: |
| [vue/operator-linebreak](./operator-linebreak.md) | enforce consistent linebreak style for operators | :wrench: |
| [vue/prefer-template](./prefer-template.md) | require template literals instead of string concatenation | :wrench: |
| [vue/space-in-parens](./space-in-parens.md) | enforce consistent spacing inside parentheses | :wrench: |
Expand Down
27 changes: 27 additions & 0 deletions docs/rules/object-shorthand.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/object-shorthand
description: require or disallow method and property shorthand syntax for object literals
---
# vue/object-shorthand

> require or disallow method and property shorthand syntax for object literals

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
- :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.

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

## :books: Further Reading

- [object-shorthand]

[object-shorthand]: https://eslint.org/docs/rules/object-shorthand

## :mag: Implementation

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

<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/object-shorthand)</sup>
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ module.exports = {
'object-curly-newline': require('./rules/object-curly-newline'),
'object-curly-spacing': require('./rules/object-curly-spacing'),
'object-property-newline': require('./rules/object-property-newline'),
'object-shorthand': require('./rules/object-shorthand'),
'one-component-per-file': require('./rules/one-component-per-file'),
'operator-linebreak': require('./rules/operator-linebreak'),
'order-in-components': require('./rules/order-in-components'),
Expand Down
12 changes: 12 additions & 0 deletions lib/rules/object-shorthand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'

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

// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
module.exports = wrapCoreRule('object-shorthand', {
skipDynamicArguments: true
})
81 changes: 81 additions & 0 deletions tests/lib/rules/object-shorthand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'

const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/object-shorthand')

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

tester.run('object-shorthand', rule, {
valid: [
{
filename: 'test.vue',
code: `
<template>
<div :style="{height}"></div>
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<div :style="{height: height}"></div>
</template>
`,
options: ['never']
}
],
invalid: [
{
filename: 'test.vue',
code: `
<template>
<div :style="{height: height}"></div>
</template>
`,
output: `
<template>
<div :style="{height}"></div>
</template>
`,
errors: [
{
message: 'Expected property shorthand.',
line: 3,
column: 23
}
]
},
{
filename: 'test.vue',
code: `
<template>
<div :style="{height}"></div>
</template>
`,
output: `
<template>
<div :style="{height: height}"></div>
</template>
`,
options: ['never'],
errors: [
{
message: 'Expected longform property syntax.',
line: 3,
column: 23
}
]
}
]
})