UNPKG

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