Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add runOutsideVue option to vue/sort-keys #1866

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
lukethompson wants to merge 3 commits into vuejs:master from lukethompson:sort-keys-run-outside-vue
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
4 changes: 3 additions & 1 deletion docs/rules/sort-keys.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ export default {
"ignoreChildrenOf": ["model"],
"ignoreGrandchildrenOf": ["computed", "directives", "inject", "props", "watch"],
"minKeys": 2,
"natural": false
"natural": false,
"runOutsideVue": true,
}]
}
```
Expand All @@ -96,6 +97,7 @@ The 2nd option is an object which has 5 properties.
- `ignoreGrandchildrenOf` - an array of properties to ignore the grandchildren sort order. Default is `["computed", "directives", "inject", "props", "watch"]`
- `minKeys` - Specifies the minimum number of keys that an object should have in order for the object's unsorted keys to produce an error. Default is `2`, which means by default all objects with unsorted keys will result in lint errors.
- `natural` - if `true`, enforce properties to be in natural order. Default is `false`. Natural Order compares strings containing combination of letters and numbers in the way a human being would sort. It basically sorts numerically, instead of sorting alphabetically. So the number 10 comes after the number 3 in Natural Sorting.
- `runOutsideVue` - if `false`, this rule will ignore all keys outside of Vue. Default is `true`. Any child keys of a Vue instance are considered to be inside Vue.

While using this rule, you may disable the normal `sort-keys` rule. This rule will apply to plain js files as well as Vue component scripts.

Expand Down
28 changes: 26 additions & 2 deletions lib/rules/sort-keys.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ function getPropertyName(node) {
return node.key.type === 'Identifier' ? node.key.name : null
}

/**
* Checks if the given property is a method named data.
*
* @param {Property} node The `Property` node to get.
* @returns {Boolean}
* @private
*/
function isDataMethod(node) {
return node.method && getPropertyName(node) === 'data'
}

/**
* Functions which check that the given 2 names are in specific order.
*
Expand Down Expand Up @@ -152,6 +163,7 @@ module.exports = {
const insensitive = options && options.caseSensitive === false
const minKeys = options && options.minKeys
const natural = options && options.natural
const onlyInsideVue = options && options.runOutsideVue === false
const isValidOrder =
isValidOrders[order + (insensitive ? 'I' : '') + (natural ? 'N' : '')]

Expand All @@ -161,6 +173,7 @@ module.exports = {
* @property {string | null} ObjectStack.prevName
* @property {number} ObjectStack.numKeys
* @property {VueState} ObjectStack.vueState
* @property {boolean} ObjectStack.withinVueData
*
* @typedef {object} VueState
* @property {Property} [VueState.currentProperty]
Expand All @@ -186,7 +199,8 @@ module.exports = {
upper: objectStack,
prevName: null,
numKeys: node.properties.length,
vueState
vueState,
withinVueData: !!objectStack && objectStack.withinVueData
}

vueState.isVueObject = utils.getVueObjectType(context, node) != null
Expand Down Expand Up @@ -246,8 +260,18 @@ module.exports = {
if (!objectStack) {
return
}

if (objectStack.vueState.isVueObject && isDataMethod(node)) {
objectStack.withinVueData = true
}

objectStack.vueState.currentProperty = node
if (objectStack.vueState.ignore) {
if (
objectStack.vueState.ignore ||
(onlyInsideVue &&
!objectStack.vueState.within &&
!objectStack.withinVueData)
) {
return
}
const prevName = objectStack.prevName
Expand Down
150 changes: 150 additions & 0 deletions tests/lib/rules/sort-keys.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,53 @@ ruleTester.run('sort-keys', rule, {
{
code: 'var obj = {a:1, _:2, b:3}',
options: ['desc', { natural: true, caseSensitive: false, minKeys: 4 }]
},

// runOutsideVue (false) should ignore unsorted keys outside of vue
{
code: 'var obj = {c:3, a:1, b:2}',
options: ['asc', { runOutsideVue: false }]
},
{
filename: 'test.js',
code: `
const { component } = Vue;
component('test', {
name: 'app',
computed: {
test () {
return {
c: 3,
a: 1,
b: 2
}
}
}
})
`,
parserOptions: { ecmaVersion: 6 },
options: ['asc', { runOutsideVue: false }]
},
// runOutsideVue (false) should ignore unsorted keys of data methods that are not the vue data method.
{
filename: 'test.js',
code: `
const { component } = Vue;
component('test', {
name: 'app',
computed: {
data () {
return {
c: 3,
a: 1,
b: 2
}
}
}
})
`,
parserOptions: { ecmaVersion: 6 },
options: ['asc', { runOutsideVue: false }]
}
],

Expand Down Expand Up @@ -1495,6 +1542,109 @@ ruleTester.run('sort-keys', rule, {
line: 7
}
]
},

// runOutsideVue (false)
{
filename: 'test.js',
code: `
const { component } = Vue;
component('test', {
name: 'app',
computed: {
b () {
return true
},
a () {
return true
}
}
})
`,
parserOptions,
options: ['asc', { runOutsideVue: false }],
errors: [
{
message:
"Expected object keys to be in ascending order. 'a' should be before 'b'.",
line: 9
}
]
},
{
filename: 'test.js',
code: `
const { component } = Vue;
component('test', {
name: 'app',
props: {
a: {
type: Boolean,
default: true
},
}
})
`,
parserOptions,
options: ['asc', { ignoreGrandchildrenOf: [], runOutsideVue: false }],
errors: [
{
message:
"Expected object keys to be in ascending order. 'default' should be before 'type'.",
line: 8
}
]
},
{
filename: 'test.js',
code: `
const { component } = Vue;
component('test', {
name: 'app',
data () {
return {
c: null,
a: null,
b: null,
}
}
})
`,
parserOptions,
options: ['asc', { runOutsideVue: false }],
errors: [
{
message:
"Expected object keys to be in ascending order. 'a' should be before 'c'.",
line: 8
}
]
},
{
filename: 'test.js',
code: `
const { component } = Vue;
component('test', {
name: 'app',
data () {
return {
a: {
c: null,
b: null,
},
}
}
})
`,
parserOptions,
options: ['asc', { runOutsideVue: false }],
errors: [
{
message:
"Expected object keys to be in ascending order. 'b' should be before 'c'.",
line: 9
}
]
}
]
})

AltStyle によって変換されたページ (->オリジナル) /