Skip to content

Commit

Permalink
New: Add rule schemas (refs #2179)
Browse files Browse the repository at this point in the history
  • Loading branch information
btmills committed May 20, 2015
1 parent 62954e9 commit 7fc6aa8
Show file tree
Hide file tree
Showing 172 changed files with 1,149 additions and 195 deletions.
32 changes: 32 additions & 0 deletions docs/developer-guide/working-with-rules.md
Expand Up @@ -21,6 +21,10 @@ module.exports = function(context) {
};

};

module.exports.schema = [
// JSON Schema for rule options goes here
];
```

**Important:** Rule submissions will not be accepted unless they are in this format.
Expand Down Expand Up @@ -132,6 +136,34 @@ Since `context.options` is just an array, you can use it to determine how many o

When using options, make sure that your rule has some logic defaults in case the options are not provided.

### Options Schemas

Rules may export a `schema` property, which is a [JSON schema](http://json-schema.org/) format description of a rule's options which will be used by ESLint to validate configuration options and prevent invalid or unexpected inputs before they are passed to the rule in `context.options`.

There are two formats for a rule's exported `schema`. The first is a full JSON Schema object describing all possible options the rule accepts, including the rule's error level as the first argument and any optional arguments thereafter.

However, to simplify schema creation, rules may also export an array of schemas for each optional positional argument, and ESLint will automatically validate the required error level first. For example, the `yoda` rule accepts a primary mode argument, as well as an extra options object with named properties.

```js
// "yoda": [2, "never", { "exceptRange": true }]
module.exports.schema = [
{
"enum": ["always", "never"]
},
{
"type": "object",
"properties": {
"exceptRange": {
"type": "boolean"
}
},
"additionalProperties": false
}
];
```

In the preceding example, the error level is assumed to be the first argument. It is followed by the first optional argument, a string which may be either `"always"` or `"never"`. The final optional argument is an object, which may have a Boolean property named `exceptRange`.

### Getting the Source

If your rule needs to get the actual JavaScript source to work with, then use the `context.getSource()` method. This method works as follows:
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/block-scoped-var.js
Expand Up @@ -316,3 +316,5 @@ module.exports = function(context) {
};

};

module.exports.schema = [];
15 changes: 15 additions & 0 deletions lib/rules/brace-style.js
Expand Up @@ -211,3 +211,18 @@ module.exports = function(context) {
};

};

module.exports.schema = [
{
"enum": ["1tbs", "stroustrup"]
},
{
"type": "object",
"properties": {
"allowSingleLine": {
"type": "boolean"
}
},
"additionalProperties": false
}
];
12 changes: 12 additions & 0 deletions lib/rules/camelcase.js
Expand Up @@ -97,3 +97,15 @@ module.exports = function(context) {
};

};

module.exports.schema = [
{
"type": "object",
"properties": {
"properties": {
"enum": ["always", "never"]
}
},
"additionalProperties": false
}
];
6 changes: 6 additions & 0 deletions lib/rules/comma-dangle.js
Expand Up @@ -56,3 +56,9 @@ module.exports = function (context) {
"ArrayExpression": checkForTrailingComma
};
};

module.exports.schema = [
{
"enum": ["always", "always-multiline", "never"]
}
];
15 changes: 15 additions & 0 deletions lib/rules/comma-spacing.js
Expand Up @@ -174,3 +174,18 @@ module.exports = function(context) {
};

};

module.exports.schema = [
{
"type": "object",
"properties": {
"before": {
"type": "boolean"
},
"after": {
"type": "boolean"
}
},
"additionalProperties": false
}
];
18 changes: 18 additions & 0 deletions lib/rules/comma-style.js
Expand Up @@ -175,3 +175,21 @@ module.exports = function(context) {

return nodes;
};

module.exports.schema = [
{
"enum": ["first", "last"]
},
{
"type": "object",
"properties": {
"exceptions": {
"type": "object",
"additionalProperties": {
"type": "boolean"
}
}
},
"additionalProperties": false
}
];
6 changes: 6 additions & 0 deletions lib/rules/complexity.js
Expand Up @@ -86,3 +86,9 @@ module.exports = function(context) {
};

};

module.exports.schema = [
{
"type": "integer"
}
];
2 changes: 2 additions & 0 deletions lib/rules/consistent-return.js
Expand Up @@ -71,3 +71,5 @@ module.exports = function(context) {
};

};

module.exports.schema = [];
6 changes: 6 additions & 0 deletions lib/rules/consistent-this.js
Expand Up @@ -106,3 +106,9 @@ module.exports = function(context) {
};

};

module.exports.schema = [
{
"type": "string"
}
];
6 changes: 6 additions & 0 deletions lib/rules/curly.js
Expand Up @@ -101,3 +101,9 @@ module.exports = function(context) {
};

};

module.exports.schema = [
{
"enum": ["all", "multi", "multi-line"]
}
];
2 changes: 2 additions & 0 deletions lib/rules/default-case.js
Expand Up @@ -62,3 +62,5 @@ module.exports = function(context) {
}
};
};

module.exports.schema = [];
15 changes: 15 additions & 0 deletions lib/rules/dot-notation.js
Expand Up @@ -102,3 +102,18 @@ module.exports = function(context) {
}
};
};

module.exports.schema = [
{
"type": "object",
"properties": {
"allowKeywords": {
"type": "boolean"
},
"allowPattern": {
"type": "string"
}
},
"additionalProperties": false
}
];
2 changes: 2 additions & 0 deletions lib/rules/eol-last.js
Expand Up @@ -34,3 +34,5 @@ module.exports = function(context) {
};

};

module.exports.schema = [];
6 changes: 6 additions & 0 deletions lib/rules/eqeqeq.js
Expand Up @@ -88,3 +88,9 @@ module.exports = function(context) {
};

};

module.exports.schema = [
{
"enum": ["smart", "allow-null"]
}
];
2 changes: 2 additions & 0 deletions lib/rules/func-names.js
Expand Up @@ -41,3 +41,5 @@ module.exports = function(context) {
}
};
};

module.exports.schema = [];
6 changes: 6 additions & 0 deletions lib/rules/func-style.js
Expand Up @@ -41,3 +41,9 @@ module.exports = function(context) {
};

};

module.exports.schema = [
{
"enum": ["declaration", "expression"]
}
];
6 changes: 6 additions & 0 deletions lib/rules/generator-star-spacing.js
Expand Up @@ -79,3 +79,9 @@ module.exports = function(context) {
};

};

module.exports.schema = [
{
"enum": ["before", "after", "both", "neither"]
}
];
6 changes: 6 additions & 0 deletions lib/rules/generator-star.js
Expand Up @@ -68,3 +68,9 @@ module.exports = function(context) {
};

};

module.exports.schema = [
{
"enum": ["start", "middle", "end"]
}
];
6 changes: 6 additions & 0 deletions lib/rules/global-strict.js
Expand Up @@ -41,3 +41,9 @@ module.exports = function(context) {
}

};

module.exports.schema = [
{
"enum": ["always", "never"]
}
];
2 changes: 2 additions & 0 deletions lib/rules/guard-for-in.js
Expand Up @@ -28,3 +28,5 @@ module.exports = function(context) {
};

};

module.exports.schema = [];
6 changes: 6 additions & 0 deletions lib/rules/handle-callback-err.js
Expand Up @@ -116,3 +116,9 @@ module.exports = function(context) {
};

};

module.exports.schema = [
{
"type": "string"
}
];
22 changes: 22 additions & 0 deletions lib/rules/indent.js
Expand Up @@ -462,3 +462,25 @@ module.exports = function (context) {
};

};

module.exports.schema = [
{
"oneOf": [
{
"enum": ["tab"]
},
{
"type": "integer"
}
]
},
{
"type": "object",
"properties": {
"indentSwitchCase": {
"type": "boolean"
}
},
"additionalProperties": false
}
];
18 changes: 18 additions & 0 deletions lib/rules/key-spacing.js
Expand Up @@ -305,3 +305,21 @@ module.exports = function(context) {
}

};

module.exports.schema = [
{
"type": "object",
"properties": {
"align": {
"enum": ["colon", "value"]
},
"beforeColon": {
"type": "boolean"
},
"afterColon": {
"type": "boolean"
}
},
"additionalProperties": false
}
];
6 changes: 6 additions & 0 deletions lib/rules/linebreak-style.js
Expand Up @@ -36,3 +36,9 @@ module.exports = function (context) {
}
};
};

module.exports.schema = [
{
"enum": ["unix", "windows"]
}
];
6 changes: 6 additions & 0 deletions lib/rules/max-depth.js
Expand Up @@ -81,3 +81,9 @@ module.exports = function(context) {
};

};

module.exports.schema = [
{
"type": "integer"
}
];
15 changes: 13 additions & 2 deletions lib/rules/max-len.js
Expand Up @@ -32,9 +32,9 @@ module.exports = function(context) {
return result;
}

var tabWidth = context.options[1];
var tabWidth = context.options[1] || 4;

var maxLength = context.options[0],
var maxLength = context.options[0] || 80,
tabString = stringRepeat(" ", tabWidth);

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -63,3 +63,14 @@ module.exports = function(context) {
};

};

module.exports.schema = [
{
"type": "integer",
"minimum": 0
},
{
"type": "integer",
"minimum": 0
}
];
6 changes: 6 additions & 0 deletions lib/rules/max-nested-callbacks.js
Expand Up @@ -65,3 +65,9 @@ module.exports = function(context) {
};

};

module.exports.schema = [
{
"type": "integer"
}
];
6 changes: 6 additions & 0 deletions lib/rules/max-params.js
Expand Up @@ -37,3 +37,9 @@ module.exports = function(context) {
};

};

module.exports.schema = [
{
"type": "integer"
}
];

0 comments on commit 7fc6aa8

Please sign in to comment.