@@ -108,40 +108,6 @@ module.exports = {
108108 return first . loc . start . line === lastExcludingSemicolon . loc . end . line ;
109109 }
110110
111- /**
112- * Determines if the given node is a lexical declaration (let, const, function, or class)
113- * @param {ASTNode } node The node to check
114- * @returns {boolean } True if the node is a lexical declaration
115- * @private
116- */
117- function isLexicalDeclaration ( node ) {
118- if ( node . type === "VariableDeclaration" ) {
119- return node . kind === "const" || node . kind === "let" ;
120- }
121-
122- return node . type === "FunctionDeclaration" || node . type === "ClassDeclaration" ;
123- }
124-
125- /**
126- * Checks if the given token is an `else` token or not.
127- * @param {Token } token The token to check.
128- * @returns {boolean } `true` if the token is an `else` token.
129- */
130- function isElseKeywordToken ( token ) {
131- return token . value === "else" && token . type === "Keyword" ;
132- }
133-
134- /**
135- * Determines whether the given node has an `else` keyword token as the first token after.
136- * @param {ASTNode } node The node to check.
137- * @returns {boolean } `true` if the node is followed by an `else` keyword token.
138- */
139- function isFollowedByElseKeyword ( node ) {
140- const nextToken = sourceCode . getTokenAfter ( node ) ;
141-
142- return Boolean ( nextToken ) && isElseKeywordToken ( nextToken ) ;
143- }
144-
145111 /**
146112 * Determines if a semicolon needs to be inserted after removing a set of curly brackets, in order to avoid a SyntaxError.
147113 * @param {Token } closingBracket The } token
@@ -196,110 +162,6 @@ module.exports = {
196162 return false ;
197163 }
198164
199- /**
200- * Determines whether the code represented by the given node contains an `if` statement
201- * that would become associated with an `else` keyword directly appended to that code.
202- *
203- * Examples where it returns `true`:
204- *
205- * if (a)
206- * foo();
207- *
208- * if (a) {
209- * foo();
210- * }
211- *
212- * if (a)
213- * foo();
214- * else if (b)
215- * bar();
216- *
217- * while (a)
218- * if (b)
219- * if(c)
220- * foo();
221- * else
222- * bar();
223- *
224- * Examples where it returns `false`:
225- *
226- * if (a)
227- * foo();
228- * else
229- * bar();
230- *
231- * while (a) {
232- * if (b)
233- * if(c)
234- * foo();
235- * else
236- * bar();
237- * }
238- *
239- * while (a)
240- * if (b) {
241- * if(c)
242- * foo();
243- * }
244- * else
245- * bar();
246- * @param {ASTNode } node Node representing the code to check.
247- * @returns {boolean } `true` if an `if` statement within the code would become associated with an `else` appended to that code.
248- */
249- function hasUnsafeIf ( node ) {
250- switch ( node . type ) {
251- case "IfStatement" :
252- if ( ! node . alternate ) {
253- return true ;
254- }
255- return hasUnsafeIf ( node . alternate ) ;
256- case "ForStatement" :
257- case "ForInStatement" :
258- case "ForOfStatement" :
259- case "LabeledStatement" :
260- case "WithStatement" :
261- case "WhileStatement" :
262- return hasUnsafeIf ( node . body ) ;
263- default :
264- return false ;
265- }
266- }
267-
268- /**
269- * Determines whether the existing curly braces around the single statement are necessary to preserve the semantics of the code.
270- * The braces, which make the given block body, are necessary in either of the following situations:
271- *
272- * 1. The statement is a lexical declaration.
273- * 2. Without the braces, an `if` within the statement would become associated with an `else` after the closing brace:
274- *
275- * if (a) {
276- * if (b)
277- * foo();
278- * }
279- * else
280- * bar();
281- *
282- * if (a)
283- * while (b)
284- * while (c) {
285- * while (d)
286- * if (e)
287- * while(f)
288- * foo();
289- * }
290- * else
291- * bar();
292- * @param {ASTNode } node `BlockStatement` body with exactly one statement directly inside. The statement can have its own nested statements.
293- * @returns {boolean } `true` if the braces are necessary - removing them (replacing the given `BlockStatement` body with its single statement content)
294- * would change the semantics of the code or produce a syntax error.
295- */
296- function areBracesNecessary ( node ) {
297- const statement = node . body [ 0 ] ;
298-
299- return isLexicalDeclaration ( statement ) ||
300- hasUnsafeIf ( statement ) && isFollowedByElseKeyword ( node ) ;
301- }
302-
303165 /**
304166 * Prepares to check the body of a node to see if it's a block statement.
305167 * @param {ASTNode } node The node to report if there's a problem.
@@ -318,7 +180,7 @@ module.exports = {
318180 const hasBlock = ( body . type === "BlockStatement" ) ;
319181 let expected = null ;
320182
321- if ( hasBlock && ( body . body . length !== 1 || areBracesNecessary ( body ) ) ) {
183+ if ( hasBlock && ( body . body . length !== 1 || astUtils . areBracesNecessary ( body , sourceCode ) ) ) {
322184 expected = true ;
323185 } else if ( multiOnly ) {
324186 expected = false ;
0 commit comments