Skip to content

feat(attributes-order): add slot attribute #1311

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

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions docs/rules/attributes-order.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ This rule aims to enforce ordering of component attributes. The default order is
e.g. '@click="functionCall"', 'v-on="event"'
- `CONTENT`
e.g. 'v-text', 'v-html'

Since 7.0.1:

- `SLOT`
e.g. 'v-slot', 'slot' (separately from `UNIQUE`).
It will inherit `UNIQUE` position by default.

### the default order

Expand Down
20 changes: 11 additions & 9 deletions lib/rules/attributes-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const ATTRS = {
OTHER_DIRECTIVES: 'OTHER_DIRECTIVES',
OTHER_ATTR: 'OTHER_ATTR',
EVENTS: 'EVENTS',
CONTENT: 'CONTENT'
CONTENT: 'CONTENT',
SLOT: 'SLOT'
}

/**
Expand Down Expand Up @@ -83,7 +84,7 @@ function getAttributeType(attribute, sourceCode) {
} else if (name === 'html' || name === 'text') {
return ATTRS.CONTENT
} else if (name === 'slot') {
return ATTRS.UNIQUE
return ATTRS.SLOT
} else if (name === 'is') {
return ATTRS.DEFINITION
} else {
Expand All @@ -100,13 +101,10 @@ function getAttributeType(attribute, sourceCode) {
return ATTRS.DEFINITION
} else if (propName === 'id') {
return ATTRS.GLOBAL
} else if (
propName === 'ref' ||
propName === 'key' ||
propName === 'slot' ||
propName === 'slot-scope'
) {
} else if (propName === 'ref' || propName === 'key') {
return ATTRS.UNIQUE
} else if (propName === 'slot' || propName === 'slot-scope') {
return ATTRS.SLOT
} else {
return ATTRS.OTHER_ATTR
}
Expand Down Expand Up @@ -186,6 +184,10 @@ function create(context) {
} else attributePosition[item] = i
})

if (!(ATTRS.SLOT in attributePosition)) {
attributePosition[ATTRS.SLOT] = attributePosition[ATTRS.GLOBAL]
}

/**
* @typedef {object} State
* @property {number} currentPosition
Expand Down Expand Up @@ -271,7 +273,7 @@ module.exports = {
items: {
type: 'string'
},
maxItems: 10,
maxItems: 11,
minItems: 10
}
}
Expand Down
62 changes: 62 additions & 0 deletions tests/lib/rules/attributes-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,68 @@ tester.run('attributes-order', rule, {
message: 'Attribute "v-is" should go before "v-cloak".'
}
]
},

{
filename: 'test.vue',
options: [
{
order: [
'UNIQUE',
'LIST_RENDERING',
'CONDITIONALS',
'RENDER_MODIFIERS',
'GLOBAL',
'TWO_WAY_BINDING',
'OTHER_DIRECTIVES',
'OTHER_ATTR',
'EVENTS',
'CONTENT',
'DEFINITION',
'SLOT'
]
}
],
code:
'<template><div ref="foo" v-slot="{ qux }" bar="baz"></div></template>',
output:
'<template><div ref="foo" bar="baz" v-slot="{ qux }"></div></template>',
errors: [
{
message: 'Attribute "bar" should go before "v-slot".'
}
]
},

{
filename: 'test.vue',
options: [
{
order: [
'UNIQUE',
'LIST_RENDERING',
'CONDITIONALS',
'RENDER_MODIFIERS',
'GLOBAL',
'TWO_WAY_BINDING',
'OTHER_DIRECTIVES',
'OTHER_ATTR',
'EVENTS',
'CONTENT',
'DEFINITION',
'SLOT'
]
}
],
code:
'<template><div bar="baz" ref="foo" v-slot="{ qux }"></div></template>',
output:
'<template><div ref="foo" bar="baz" v-slot="{ qux }"></div></template>',
errors: [
{
message: 'Attribute "ref" should go before "bar".'
}
]
}
]
})