lib/rules/key-spacing.js
function report(property, side, whitespace, expected, mode) {
            const diff = whitespace.length - expected,
                nextColon = getNextColon(property.key),
                tokenBeforeColon = sourceCode.getTokenBefore(nextColon, { includeComments: true }),
                tokenAfterColon = sourceCode.getTokenAfter(nextColon, { includeComments: true }),
                isKeySide = side === "key",
                locStart = isKeySide ? tokenBeforeColon.loc.start : tokenAfterColon.loc.start,
                isExtra = diff > 0,
                diffAbs = Math.abs(diff),
                spaces = Array(diffAbs + 1).join(" ");

            if ((
                diff && mode === "strict" ||
                diff < 0 && mode === "minimum" ||
                diff > 0 && !expected && mode === "minimum") &&
                !(expected && containsLineTerminator(whitespace))
            ) {
                let fix;

                if (isExtra) {
                    let range;

                    // Remove whitespace
                    if (isKeySide) {
                        range = [tokenBeforeColon.range[1], tokenBeforeColon.range[1] + diffAbs];
                    } else {
                        range = [tokenAfterColon.range[0] - diffAbs, tokenAfterColon.range[0]];
                    }
                    fix = function(fixer) {
                        return fixer.removeRange(range);
                    };
                } else {

                    // Add whitespace
                    if (isKeySide) {
                        fix = function(fixer) {
                            return fixer.insertTextAfter(tokenBeforeColon, spaces);
                        };
                    } else {
                        fix = function(fixer) {
                            return fixer.insertTextBefore(tokenAfterColon, spaces);
                        };
                    }
                }

                let messageId = "";

                if (isExtra) {
                    messageId = side === "key" ? "extraKey" : "extraValue";
                } else {
                    messageId = side === "key" ? "missingKey" : "missingValue";
                }

                context.report({
                    node: property[side],
                    loc: locStart,
                    messageId,
                    data: {
                        computed: property.computed ? "computed " : "",
                        key: getKey(property)
                    },
                    fix
                });
            }
        }
function report(property, side, whitespace, expected, mode) {
            const diff = whitespace.length - expected,
                nextColon = getNextColon(property.key),
                tokenBeforeColon = sourceCode.getTokenBefore(nextColon, { includeComments: true }),
                tokenAfterColon = sourceCode.getTokenAfter(nextColon, { includeComments: true }),
                isKeySide = side === "key",
                locStart = isKeySide ? tokenBeforeColon.loc.start : tokenAfterColon.loc.start,
                isExtra = diff > 0,
                diffAbs = Math.abs(diff),
                spaces = Array(diffAbs + 1).join(" ");

            if ((
                diff && mode === "strict" ||
                diff < 0 && mode === "minimum" ||
                diff > 0 && !expected && mode === "minimum") &&
                !(expected && containsLineTerminator(whitespace))
            ) {
                let fix;

                if (isExtra) {
                    let range;

                    // Remove whitespace
                    if (isKeySide) {
                        range = [tokenBeforeColon.range[1], tokenBeforeColon.range[1] + diffAbs];
                    } else {
                        range = [tokenAfterColon.range[0] - diffAbs, tokenAfterColon.range[0]];
                    }
                    fix = function(fixer) {
                        return fixer.removeRange(range);
                    };
                } else {

                    // Add whitespace
                    if (isKeySide) {
                        fix = function(fixer) {
                            return fixer.insertTextAfter(tokenBeforeColon, spaces);
                        };
                    } else {
                        fix = function(fixer) {
                            return fixer.insertTextBefore(tokenAfterColon, spaces);
                        };
                    }
                }

                let messageId = "";

                if (isExtra) {
                    messageId = side === "key" ? "extraKey" : "extraValue";
                } else {
                    messageId = side === "key" ? "missingKey" : "missingValue";
                }

                context.report({
                    node: property[side],
                    loc: locStart,
                    messageId,
                    data: {
                        computed: property.computed ? "computed " : "",
                        key: getKey(property)
                    },
                    fix
                });
            }
        }
function report ( property , side , whitespace , expected , mode ) { const diff = whitespace . length - expected , nextColon = getNextColon ( property . key ) , tokenBeforeColon = sourceCode . getTokenBefore ( nextColon , { includeComments : true } ) , tokenAfterColon = sourceCode . getTokenAfter ( nextColon , { includeComments : true } ) , isKeySide = side === "key" , locStart = isKeySide ? tokenBeforeColon . loc . start : tokenAfterColon . loc . start , isExtra = diff > 0 , diffAbs = Math . abs ( diff ) , spaces = Array ( diffAbs + 1 ) . join ( " " ) ; if ( ( diff && mode === "strict" || diff < 0 && mode === "minimum" || diff > 0 && ! expected && mode === "minimum" ) && ! ( expected && containsLineTerminator ( whitespace ) ) ) { let fix ; if ( isExtra ) { let range ; // Remove whitespace if ( isKeySide ) { range = [ tokenBeforeColon . range [ 1 ] , tokenBeforeColon . range [ 1 ] + diffAbs ] ; } else { range = [ tokenAfterColon . range [ 0 ] - diffAbs , tokenAfterColon . range [ 0 ] ] ; } fix = function ( fixer ) { return fixer . removeRange ( range ) ; } ; } else { // Add whitespace if ( isKeySide ) { fix = function ( fixer ) { return fixer . insertTextAfter ( tokenBeforeColon , spaces ) ; } ; } else { fix = function ( fixer ) { return fixer . insertTextBefore ( tokenAfterColon , spaces ) ; } ; } } let messageId = "" ; if ( isExtra ) { messageId = side === "key" ? "extraKey" : "extraValue" ; } else { messageId = side === "key" ? "missingKey" : "missingValue" ; } context . report ( { node : property [ side ] , loc : locStart , messageId , data : { computed : property . computed ? "computed " : "" , key : getKey ( property ) } , fix } ) ; } }
Reports an appropriately-formatted error if spacing is incorrect on one
side of the colon.
@param {ASTNode} property Key-value pair in an object literal.
@param {string} side Side being verified - either "key" or "value".
@param {string} whitespace Actual whitespace string.
@param {int} expected Expected whitespace length.
@param {string} mode Value of the mode as "strict" or "minimum"
@returns {void}
Reports an appropriately - formatted error if spacing is incorrect on one side of the colon .