UNPKG

7.63 kBJavaScriptView Raw
1/**
2 * @fileoverview Require or disallow newlines around directives.
3 * @author Kai Cataldo
4 * @deprecated
5 */
6
7"use strict";
8
9const astUtils = require("../ast-utils");
10
11//------------------------------------------------------------------------------
12// Rule Definition
13//------------------------------------------------------------------------------
14
15module.exports = {
16 meta: {
17 docs: {
18 description: "require or disallow newlines around directives",
19 category: "Stylistic Issues",
20 recommended: false,
21 replacedBy: ["padding-line-between-statements"],
22 url: "https://eslint.org/docs/rules/lines-around-directive"
23 },
24 schema: [{
25 oneOf: [
26 {
27 enum: ["always", "never"]
28 },
29 {
30 type: "object",
31 properties: {
32 before: {
33 enum: ["always", "never"]
34 },
35 after: {
36 enum: ["always", "never"]
37 }
38 },
39 additionalProperties: false,
40 minProperties: 2
41 }
42 ]
43 }],
44 fixable: "whitespace",
45 deprecated: true
46 },
47
48 create(context) {
49 const sourceCode = context.getSourceCode();
50 const config = context.options[0] || "always";
51 const expectLineBefore = typeof config === "string" ? config : config.before;
52 const expectLineAfter = typeof config === "string" ? config : config.after;
53
54 //--------------------------------------------------------------------------
55 // Helpers
56 //--------------------------------------------------------------------------
57
58 /**
59 * Check if node is preceded by a blank newline.
60 * @param {ASTNode} node Node to check.
61 * @returns {boolean} Whether or not the passed in node is preceded by a blank newline.
62 */
63 function hasNewlineBefore(node) {
64 const tokenBefore = sourceCode.getTokenBefore(node, { includeComments: true });
65 const tokenLineBefore = tokenBefore ? tokenBefore.loc.end.line : 0;
66
67 return node.loc.start.line - tokenLineBefore >= 2;
68 }
69
70 /**
71 * Gets the last token of a node that is on the same line as the rest of the node.
72 * This will usually be the last token of the node, but it will be the second-to-last token if the node has a trailing
73 * semicolon on a different line.
74 * @param {ASTNode} node A directive node
75 * @returns {Token} The last token of the node on the line
76 */
77 function getLastTokenOnLine(node) {
78 const lastToken = sourceCode.getLastToken(node);
79 const secondToLastToken = sourceCode.getTokenBefore(lastToken);
80
81 return astUtils.isSemicolonToken(lastToken) && lastToken.loc.start.line > secondToLastToken.loc.end.line
82 ? secondToLastToken
83 : lastToken;
84 }
85
86 /**
87 * Check if node is followed by a blank newline.
88 * @param {ASTNode} node Node to check.
89 * @returns {boolean} Whether or not the passed in node is followed by a blank newline.
90 */
91 function hasNewlineAfter(node) {
92 const lastToken = getLastTokenOnLine(node);
93 const tokenAfter = sourceCode.getTokenAfter(lastToken, { includeComments: true });
94
95 return tokenAfter.loc.start.line - lastToken.loc.end.line >= 2;
96 }
97
98 /**
99 * Report errors for newlines around directives.
100 * @param {ASTNode} node Node to check.
101 * @param {string} location Whether the error was found before or after the directive.
102 * @param {boolean} expected Whether or not a newline was expected or unexpected.
103 * @returns {void}
104 */
105 function reportError(node, location, expected) {
106 context.report({
107 node,
108 message: "{{expected}} newline {{location}} \"{{value}}\" directive.",
109 data: {
110 expected: expected ? "Expected" : "Unexpected",
111 value: node.expression.value,
112 location
113 },
114 fix(fixer) {
115 const lastToken = getLastTokenOnLine(node);
116
117 if (expected) {
118 return location === "before" ? fixer.insertTextBefore(node, "\n") : fixer.insertTextAfter(lastToken, "\n");
119 }
120 return fixer.removeRange(location === "before" ? [node.range[0] - 1, node.range[0]] : [lastToken.range[1], lastToken.range[1] + 1]);
121 }
122 });
123 }
124
125 /**
126 * Check lines around directives in node
127 * @param {ASTNode} node - node to check
128 * @returns {void}
129 */
130 function checkDirectives(node) {
131 const directives = astUtils.getDirectivePrologue(node);
132
133 if (!directives.length) {
134 return;
135 }
136
137 const firstDirective = directives[0];
138 const leadingComments = sourceCode.getCommentsBefore(firstDirective);
139
140 /*
141 * Only check before the first directive if it is preceded by a comment or if it is at the top of
142 * the file and expectLineBefore is set to "never". This is to not force a newline at the top of
143 * the file if there are no comments as well as for compatibility with padded-blocks.
144 */
145 if (leadingComments.length) {
146 if (expectLineBefore === "always" && !hasNewlineBefore(firstDirective)) {
147 reportError(firstDirective, "before", true);
148 }
149
150 if (expectLineBefore === "never" && hasNewlineBefore(firstDirective)) {
151 reportError(firstDirective, "before", false);
152 }
153 } else if (
154 node.type === "Program" &&
155 expectLineBefore === "never" &&
156 !leadingComments.length &&
157 hasNewlineBefore(firstDirective)
158 ) {
159 reportError(firstDirective, "before", false);
160 }
161
162 const lastDirective = directives[directives.length - 1];
163 const statements = node.type === "Program" ? node.body : node.body.body;
164
165 /*
166 * Do not check after the last directive if the body only
167 * contains a directive prologue and isn't followed by a comment to ensure
168 * this rule behaves well with padded-blocks.
169 */
170 if (lastDirective === statements[statements.length - 1] && !lastDirective.trailingComments) {
171 return;
172 }
173
174 if (expectLineAfter === "always" && !hasNewlineAfter(lastDirective)) {
175 reportError(lastDirective, "after", true);
176 }
177
178 if (expectLineAfter === "never" && hasNewlineAfter(lastDirective)) {
179 reportError(lastDirective, "after", false);
180 }
181 }
182
183 //--------------------------------------------------------------------------
184 // Public
185 //--------------------------------------------------------------------------
186
187 return {
188 Program: checkDirectives,
189 FunctionDeclaration: checkDirectives,
190 FunctionExpression: checkDirectives,
191 ArrowFunctionExpression: checkDirectives
192 };
193 }
194};