Skip to content

Commit

Permalink
New: lines-around-directive rule (fixes #6069)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Aug 29, 2016
1 parent a989a7c commit 4ab5b1a
Show file tree
Hide file tree
Showing 6 changed files with 2,110 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/eslint.json
Expand Up @@ -172,6 +172,7 @@
"keyword-spacing": "off",
"linebreak-style": "off",
"lines-around-comment": "off",
"lines-around-directive": "off",
"max-depth": "off",
"max-len": "off",
"max-lines": "off",
Expand Down
320 changes: 320 additions & 0 deletions docs/rules/lines-around-directive.md
@@ -0,0 +1,320 @@
# Require or disallow newlines around directives (lines-around-directive)

Directives are used in JavaScript to indicate to the execution environment that a script would like to opt into a feature such as `"strict mode"`. Directives are grouped together in a [directive prologue](http://www.ecma-international.org/ecma-262/7.0/#directive-prologue) at the top of either a file or function block and are applied to the scope in which they occur.

```js
// Strict mode is invoked for the entire script
"use strict";

var foo;

function bar() {
var baz;
}
```

```js
var foo;

// Strict mode is only invoked within the following function
function bar() {
"use strict";

var baz;
}
```

## Rule Details

This rule requires or disallows blank newlines around directive prologues. This rule does not enforce any conventions about blank newlines between the individual directives. In addition, it does not require blank newlines before directive prologues unless they are preceded by a comment. Please use the [padded-blocks](padded-blocks.md) rule if this is a style you would like to enforce.

## Options

This rule has one option. It can either be a string or an object:

* `"always"` (default) enforces blank newlines around directives.
* `"never"` disallows blank newlines around directives.

or

```js
{
"before": "always" or "never"
"after": "always" or "never",
}
```

### always

This is the default option.

Examples of **incorrect** code for this rule with the `"always"` option:

```js
/* eslint lines-around-directive: ["error", "always"] */

/* Top of file */
"use strict";
var foo;

/* Top of file */
// comment
"use strict";
"use asm";
var foo;

function foo() {
"use strict";
"use asm";
var bar;
}

function foo() {
// comment
"use strict";
var bar;
}
```

Examples of **correct** code for this rule with the `"always"` option:

```js
/* eslint lines-around-directive: ["error", "always"] */

/* Top of file */
"use strict";

var foo;

/* Top of file */
// comment

"use strict";
"use asm";

var foo;

function foo() {
"use strict";
"use asm";

var bar;
}

function foo() {
// comment

"use strict";

var bar;
}
```

### never

Examples of **incorrect** code for this rule with the `"never"` option:

```js
/* eslint lines-around-directive: ["error", "never"] */

/* Top of file */

"use strict";

var foo;


/* Top of file */
// comment

"use strict";
"use asm";

var foo;


function foo() {
"use strict";
"use asm";

var bar;
}


function foo() {
// comment

"use strict";

var bar;
}
```

Examples of **correct** code for this rule with the `"never"` option:

```js
/* eslint lines-around-directive: ["error", "never"] */

/* Top of file */
"use strict";
var foo;

/* Top of file */
// comment
"use strict";
"use asm";
var foo;

function foo() {
"use strict";
"use asm";
var bar;
}

function foo() {
// comment
"use strict";
var bar;
}
```

### before & after

Examples of **incorrect** code for this rule with the `{ "before": "never", "after": "always" }` option:

```js
/* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */

/* Top of file */

"use strict";
var foo;

/* Top of file */
// comment

"use strict";
"use asm";
var foo;

function foo() {
"use strict";
"use asm";
var bar;
}

function foo() {
// comment

"use strict";
var bar;
}
```

Examples of **correct** code for this rule with the `{ "before": "never", "after": "always" }` option:

```js
/* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */

/* Top of file */
"use strict";

var foo;

/* Top of file */
// comment
"use strict";
"use asm";

var foo;

function foo() {
"use strict";
"use asm";

var bar;
}

function foo() {
// comment
"use strict";

var bar;
}
```

Examples of **incorrect** code for this rule with the `{ "before": "always", "after": "never" }` option:

```js
/* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */

/* Top of file */
"use strict";

var foo;

/* Top of file */
// comment
"use strict";
"use asm";

var foo;

function foo() {
"use strict";
"use asm";

var bar;
}

function foo() {
// comment
"use strict";

var bar;
}
```

Examples of **correct** code for this rule with the `{ "before": "always", "after": "never" }` option:

```js
/* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */

/* Top of file */
"use strict";
var foo;

/* Top of file */
// comment

"use strict";
"use asm";
var foo;

function foo() {
"use strict";
"use asm";
var bar;
}

function foo() {
// comment

"use strict";
var bar;
}
```

## When Not To Use It

You can safely disable this rule if you do not have any strict conventions about whether or not directive prologues should have blank newlines before or after them.

## Related Rules

* [lines-around-comment](lines-around-comment.md)
* [padded-blocks](padded-blocks.md)

## Compatibility

* **JSCS**: [requirePaddingNewLinesAfterUseStrict](http://jscs.info/rule/requirePaddingNewLinesAfterUseStrict)
* **JSCS**: [disallowPaddingNewLinesAfterUseStrict](http://jscs.info/rule/disallowPaddingNewLinesAfterUseStrict)
41 changes: 41 additions & 0 deletions lib/ast-utils.js
Expand Up @@ -680,5 +680,46 @@ module.exports = {
}

return null;
},

/**
* Get directives from directive prologue of a Program or Function node.
* @param {ASTNode} node - The node to check.
* @returns {ASTNode[]|null} The directives found in the directive prologue or
* null if none are found.
*/
getDirectivePrologue(node) {
if (
node.type !== "Program" &&
node.type !== "FunctionDeclaration" &&
node.type !== "FunctionExpression" &&
node.type !== "ArrowFunctionExpression" ||

// Do not check arrow functions with implicit return.
// `() => "use strict";` returns the string `"use strict"`.
node.type === "ArrowFunctionExpression" && node.body.type !== "BlockStatement"
) {
return null;
}

const statements = node.type === "Program" ? node.body : node.body.body;
const directives = [];

for (const statement of statements) {
if (
statement.type === "ExpressionStatement" &&
statement.expression.type === "Literal"
) {
directives.push(statement);
} else {
if (!directives.length) {
return null;
}

break;
}
}

return directives;
}
};

0 comments on commit 4ab5b1a

Please sign in to comment.