Skip to content

New: meta-property-ordering (fixes #62) #80

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 3 commits into from
May 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extends:
root: true
rules:
require-jsdoc: error
self/meta-property-ordering: off
self/require-meta-docs-url: off
self/report-message-format:
- error
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Name | ✔️ | 🛠 | Description
----- | ----- | ----- | -----
[consistent-output](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/consistent-output.md) | | | Enforce consistent use of output assertions in rule tests
[fixer-return](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/fixer-return.md) | ✔️ | | Expected fixer function to always return a value.
[meta-property-ordering](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/meta-property-ordering.md) | | 🛠 | Enforces the order of meta properties
[no-deprecated-context-methods](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-deprecated-context-methods.md) | | 🛠 | Disallows usage of deprecated methods on rule context objects
[no-deprecated-report-api](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-deprecated-report-api.md) | ✔️ | 🛠 | disallow use of the deprecated context.report() API
[no-identical-tests](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-identical-tests.md) | ✔️ | 🛠 | disallow identical tests
Expand Down
51 changes: 51 additions & 0 deletions docs/rules/meta-property-ordering.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# enforce ordering of meta properties in rule source (meta-property-ordering)

(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule.

This rule enforces that the properties of rule meta are arranged in a consistent order.

## Rule Details

### Options

This rule has an array option:

* `['type', 'docs', 'fixable', 'schema', 'messages', 'deprecated', 'replacedBy']` (default): The properties of meta should be placed in a consistent order.

Examples of **incorrect** code for this rule:

```js

/* eslint eslint-plugin/meta-property-ordering: ["error",
["type", "docs", "fixable", "schema", "messages"]
] */

// invalid; wrong order
{
fixable: false,
type: "problem",
docs: "",
}

```

Examples of **correct** code for this rule:

```js
/* eslint eslint-plugin/test-case-property-ordering: ["error",
["type", "docs", "fixable", "schema", "messages"]
] */

// valid;
{
type: "bar",
docs: "foo",
fooooooooo: "foo",
fixable: false,
}

```

## When Not To Use It

If don't want to enforce ordering of properies in meta, you can turn off this rule.
78 changes: 78 additions & 0 deletions lib/rules/meta-property-ordering.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @fileoverview Enforces the order of meta properties
*/

'use strict';

const { getKeyName, getRuleInfo } = require('../utils');

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description: 'Enforces the order of meta properties',
category: 'Rules',
recommended: false,
},
type: 'suggestion',
fixable: 'code',
schema: [{
type: 'array',
elements: { type: 'string' },
}],
},

create (context) {
const sourceCode = context.getSourceCode();
const info = getRuleInfo(sourceCode.ast);

const message = 'The meta properties should be placed in a consistent order: [{{order}}].';
const order = context.options[0] || ['type', 'docs', 'fixable', 'schema', 'messages'];

const orderMap = new Map(order.map((name, i) => [name, i]));

return {
Program () {
if (
!info ||
!info.meta ||
info.meta.properties.length < 2
) {
return;
}

const propsActual = info.meta.properties
.filter(prop => orderMap.has(getKeyName(prop)));

for (let i = 1, j = propsActual.length; i < j; i += 1) {
const last = propsActual[i - 1];
const curr = propsActual[i];
if (order.indexOf(getKeyName(last)) > order.indexOf(getKeyName(curr))) {
const propsExpected = propsActual
.slice()
.sort((a, b) => orderMap.get(getKeyName(a)) - orderMap.get(getKeyName(b)));

context.report({
node: curr,
message,
data: {
order: propsExpected.map(getKeyName).join(', '),
},
fix (fixer) {
return propsActual.map((prop, k) => {
return fixer.replaceText(
prop,
sourceCode.getText(propsExpected[k])
);
});
},
});
}
}
},
};
},
};
133 changes: 133 additions & 0 deletions tests/lib/rules/meta-property-ordering.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/**
* @fileoverview Enforces the order of meta properties
*/

'use strict';

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const rule = require('../../../lib/rules/meta-property-ordering');
const RuleTester = require('eslint').RuleTester;

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

/**
* @param {string[]} order
* @returns {string}
*/
function getMessage (order) {
return `The meta properties should be placed in a consistent order: [${order.join(', ')}].`;
}

const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
ruleTester.run('test-case-property-ordering', rule, {
valid: [
`
module.exports = {
meta: {type, docs, fixable, schema, messages},
create() {},
};`,

`
module.exports = {
meta: {docs, schema, messages},
create() {},
};`,

`
module.exports = {
meta: {docs, foo, bar, messages},
create() {},
};`,

`
module.exports = {
meta: {
type: 'problem',
docs: {},
fixable: 'code',
schema: [],
messages: {}
},
create() {},
};`,
{
code: `
module.exports = {
meta: {messages, schema, docs},
create() {},
};`,
options: [['schema', 'docs']],
},
`
module.exports = {
meta: {},
create() {},
};`,
],

invalid: [
{
code: `
module.exports = {
meta: {
docs,
fixable,
type: 'problem',
},
create() {},
};`,

output: `
module.exports = {
meta: {
type: 'problem',
docs,
fixable,
},
create() {},
};`,
errors: [{ message: getMessage(['type', 'docs', 'fixable']) }],
},
{
code: `
module.exports = {
meta: {schema, fixable, type, docs},
create() {},
};`,

output: `
module.exports = {
meta: {type, docs, fixable, schema},
create() {},
};`,
errors: [
{ message: getMessage(['type', 'docs', 'fixable', 'schema']) },
{ message: getMessage(['type', 'docs', 'fixable', 'schema']) },
],
},

{
code: `
module.exports = {
meta: {fixable, fooooooooo, doc, type},
create() {},
};`,

output: `
module.exports = {
meta: {type, fooooooooo, doc, fixable},
create() {},
};`,
options: [['type', 'doc', 'fixable']],
errors: [
{ message: getMessage(['type', 'doc', 'fixable']) },
{ message: getMessage(['type', 'doc', 'fixable']) },
],
},
],
});