Skip to content

Commit 18376cf

Browse files
not-an-aardvarkkaicataldo
authored andcommitted
Update: add fixer for lines-around-directive (#7217)
1 parent f8e8fab commit 18376cf

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

docs/rules/lines-around-directive.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# require or disallow newlines around directives (lines-around-directive)
22

3+
(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule.
4+
35
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.
46

57
```js

lib/rules/lines-around-directive.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ module.exports = {
3737
minProperties: 2
3838
}
3939
]
40-
}]
40+
}],
41+
fixable: "whitespace"
4142
},
4243

4344
create(context) {
@@ -88,6 +89,12 @@ module.exports = {
8889
expected: expected ? "Expected" : "Unexpected",
8990
value: node.expression.value,
9091
location
92+
},
93+
fix(fixer) {
94+
if (expected) {
95+
return location === "before" ? fixer.insertTextBefore(node, "\n") : fixer.insertTextAfter(node, "\n");
96+
}
97+
return fixer.removeRange(location === "before" ? [node.range[0] - 1, node.range[0]] : [node.range[1], node.range[1] + 1]);
9198
}
9299
});
93100
}

0 commit comments

Comments
 (0)