lib/rules/curly.js
function prepareCheck(node, body, name, opts) {
            const hasBlock = (body.type === "BlockStatement");
            let expected = null;

            if (node.type === "IfStatement" && node.consequent === body && requiresBraceOfConsequent(node)) {
                expected = true;
            } else if (multiOnly) {
                if (hasBlock && body.body.length === 1) {
                    expected = false;
                }
            } else if (multiLine) {
                if (!isCollapsedOneLiner(body)) {
                    expected = true;
                }
            } else if (multiOrNest) {
                if (hasBlock && body.body.length === 1 && isOneLiner(body.body[0])) {
                    const leadingComments = sourceCode.getCommentsBefore(body.body[0]);

                    expected = leadingComments.length > 0;
                } else if (!isOneLiner(body)) {
                    expected = true;
                }
            } else {
                expected = true;
            }

            return {
                actual: hasBlock,
                expected,
                check() {
                    if (this.expected !== null && this.expected !== this.actual) {
                        if (this.expected) {
                            context.report({
                                node,
                                loc: (name !== "else" ? node : getElseKeyword(node)).loc.start,
                                messageId: opts && opts.condition ? "missingCurlyAfterCondition" : "missingCurlyAfter",
                                data: {
                                    name
                                },
                                fix: fixer => fixer.replaceText(body, `{${sourceCode.getText(body)}}`)
                            });
                        } else {
                            context.report({
                                node,
                                loc: (name !== "else" ? node : getElseKeyword(node)).loc.start,
                                messageId: opts && opts.condition ? "unexpectedCurlyAfterCondition" : "unexpectedCurlyAfter",
                                data: {
                                    name
                                },
                                fix(fixer) {

                                    /*
                                     * `do while` expressions sometimes need a space to be inserted after `do`.
                                     * e.g. `do{foo()} while (bar)` should be corrected to `do foo() while (bar)`
                                     */
                                    const needsPrecedingSpace = node.type === "DoWhileStatement" &&
                                        sourceCode.getTokenBefore(body).range[1] === body.range[0] &&
                                        !astUtils.canTokensBeAdjacent("do", sourceCode.getFirstToken(body, { skip: 1 }));

                                    const openingBracket = sourceCode.getFirstToken(body);
                                    const closingBracket = sourceCode.getLastToken(body);
                                    const lastTokenInBlock = sourceCode.getTokenBefore(closingBracket);

                                    if (needsSemicolon(closingBracket)) {

                                        /*
                                         * If removing braces would cause a SyntaxError due to multiple statements on the same line (or
                                         * change the semantics of the code due to ASI), don't perform a fix.
                                         */
                                        return null;
                                    }

                                    const resultingBodyText = sourceCode.getText().slice(openingBracket.range[1], lastTokenInBlock.range[0]) +
                                        sourceCode.getText(lastTokenInBlock) +
                                        sourceCode.getText().slice(lastTokenInBlock.range[1], closingBracket.range[0]);

                                    return fixer.replaceText(body, (needsPrecedingSpace ? " " : "") + resultingBodyText);
                                }
                            });
                        }
                    }
                }
            };
        }
function prepareCheck(node, body, name, opts) {
            const hasBlock = (body.type === "BlockStatement");
            let expected = null;

            if (node.type === "IfStatement" && node.consequent === body && requiresBraceOfConsequent(node)) {
                expected = true;
            } else if (multiOnly) {
                if (hasBlock && body.body.length === 1) {
                    expected = false;
                }
            } else if (multiLine) {
                if (!isCollapsedOneLiner(body)) {
                    expected = true;
                }
            } else if (multiOrNest) {
                if (hasBlock && body.body.length === 1 && isOneLiner(body.body[0])) {
                    const leadingComments = sourceCode.getCommentsBefore(body.body[0]);

                    expected = leadingComments.length > 0;
                } else if (!isOneLiner(body)) {
                    expected = true;
                }
            } else {
                expected = true;
            }

            return {
                actual: hasBlock,
                expected,
                check() {
                    if (this.expected !== null && this.expected !== this.actual) {
                        if (this.expected) {
                            context.report({
                                node,
                                loc: (name !== "else" ? node : getElseKeyword(node)).loc.start,
                                messageId: opts && opts.condition ? "missingCurlyAfterCondition" : "missingCurlyAfter",
                                data: {
                                    name
                                },
                                fix: fixer => fixer.replaceText(body, `{${sourceCode.getText(body)}}`)
                            });
                        } else {
                            context.report({
                                node,
                                loc: (name !== "else" ? node : getElseKeyword(node)).loc.start,
                                messageId: opts && opts.condition ? "unexpectedCurlyAfterCondition" : "unexpectedCurlyAfter",
                                data: {
                                    name
                                },
                                fix(fixer) {

                                    /*
                                     * `do while` expressions sometimes need a space to be inserted after `do`.
                                     * e.g. `do{foo()} while (bar)` should be corrected to `do foo() while (bar)`
                                     */
                                    const needsPrecedingSpace = node.type === "DoWhileStatement" &&
                                        sourceCode.getTokenBefore(body).range[1] === body.range[0] &&
                                        !astUtils.canTokensBeAdjacent("do", sourceCode.getFirstToken(body, { skip: 1 }));

                                    const openingBracket = sourceCode.getFirstToken(body);
                                    const closingBracket = sourceCode.getLastToken(body);
                                    const lastTokenInBlock = sourceCode.getTokenBefore(closingBracket);

                                    if (needsSemicolon(closingBracket)) {

                                        /*
                                         * If removing braces would cause a SyntaxError due to multiple statements on the same line (or
                                         * change the semantics of the code due to ASI), don't perform a fix.
                                         */
                                        return null;
                                    }

                                    const resultingBodyText = sourceCode.getText().slice(openingBracket.range[1], lastTokenInBlock.range[0]) +
                                        sourceCode.getText(lastTokenInBlock) +
                                        sourceCode.getText().slice(lastTokenInBlock.range[1], closingBracket.range[0]);

                                    return fixer.replaceText(body, (needsPrecedingSpace ? " " : "") + resultingBodyText);
                                }
                            });
                        }
                    }
                }
            };
        }
function prepareCheck ( node , body , name , opts ) { const hasBlock = ( body . type === "BlockStatement" ) ; let expected = null ; if ( node . type === "IfStatement" && node . consequent === body && requiresBraceOfConsequent ( node ) ) { expected = true ; } else if ( multiOnly ) { if ( hasBlock && body . body . length === 1 ) { expected = false ; } } else if ( multiLine ) { if ( ! isCollapsedOneLiner ( body ) ) { expected = true ; } } else if ( multiOrNest ) { if ( hasBlock && body . body . length === 1 && isOneLiner ( body . body [ 0 ] ) ) { const leadingComments = sourceCode . getCommentsBefore ( body . body [ 0 ] ) ; expected = leadingComments . length > 0 ; } else if ( ! isOneLiner ( body ) ) { expected = true ; } } else { expected = true ; } return { actual : hasBlock , expected , check ( ) { if ( this . expected !== null && this . expected !== this . actual ) { if ( this . expected ) { context . report ( { node , loc : ( name !== "else" ? node : getElseKeyword ( node ) ) . loc . start , messageId : opts && opts . condition ? "missingCurlyAfterCondition" : "missingCurlyAfter" , data : { name } , fix : fixer => fixer . replaceText ( body , ` ${ sourceCode . getText ( body ) } ` ) } ) ; } else { context . report ( { node , loc : ( name !== "else" ? node : getElseKeyword ( node ) ) . loc . start , messageId : opts && opts . condition ? "unexpectedCurlyAfterCondition" : "unexpectedCurlyAfter" , data : { name } , fix ( fixer ) { /*
                                     * `do while` expressions sometimes need a space to be inserted after `do`.
                                     * e.g. `do{foo()} while (bar)` should be corrected to `do foo() while (bar)`
                                     */ const needsPrecedingSpace = node . type === "DoWhileStatement" && sourceCode . getTokenBefore ( body ) . range [ 1 ] === body . range [ 0 ] && ! astUtils . canTokensBeAdjacent ( "do" , sourceCode . getFirstToken ( body , { skip : 1 } ) ) ; const openingBracket = sourceCode . getFirstToken ( body ) ; const closingBracket = sourceCode . getLastToken ( body ) ; const lastTokenInBlock = sourceCode . getTokenBefore ( closingBracket ) ; if ( needsSemicolon ( closingBracket ) ) { /*
                                         * If removing braces would cause a SyntaxError due to multiple statements on the same line (or
                                         * change the semantics of the code due to ASI), don't perform a fix.
                                         */ return null ; } const resultingBodyText = sourceCode . getText ( ) . slice ( openingBracket . range [ 1 ] , lastTokenInBlock . range [ 0 ] ) + sourceCode . getText ( lastTokenInBlock ) + sourceCode . getText ( ) . slice ( lastTokenInBlock . range [ 1 ] , closingBracket . range [ 0 ] ) ; return fixer . replaceText ( body , ( needsPrecedingSpace ? " " : "" ) + resultingBodyText ) ; } } ) ; } } } } ; }
Prepares to check the body of a node to see if it's a block statement.
@param {ASTNode} node The node to report if there's a problem.
@param {ASTNode} body The body node to check for blocks.
@param {string} name The name to report if there's a problem.
@param {{ condition: boolean }} opts Options to pass to the report functions
@returns {Object} a prepared check object, with "actual", "expected", "check" properties.
"actual" will be `true` or `false` whether the body is already a block statement.
"expected" will be `true` or `false` if the body should be a block statement or not, or
`null` if it doesn't matter, depending on the rule options. It can be modified to change
the final behavior of "check".
"check" will be a function reporting appropriate problems depending on the other
properties.
Prepares to check the body of a node to see if it s a block statement .