question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using Autofix's .remove to remove an object property results in invalid code

See original GitHub issue

Tell us about your environment

  • ESLint Version: 4.10.0
  • Node Version: 9.0.0
  • npm Version: 5.5.1

What parser (default, Babel-ESLint, etc.) are you using? babel-eslint

Please show your full configuration:

Configuration

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

export default {
  meta: {
    fixable: "code"
  },
  create(context) {
    return {
      Property(node) {
        if (node.key.name === "b") {
          context.report({
            node,
            message: "Remove b",
            fix(fixer) {
              return fixer.remove(node);
            }
          });
        }
      }
    };
  }
};

Running --fixwith this rule on the following code:

const hey = {
 a: 'a',
 b: 'b',
 c: 'c'
}
eslint --fix

What did you expect to happen? It produces:

const hey = {
 a: 'a',
 c: 'c'
}

What actually happened? Please include the actual, raw output from ESLint. It produces:

const hey = {
 a: 'a',
 ,
 c: 'c'
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mysticateacommented, Nov 3, 2017

Oh, sorry, the 2nd example does not regard the case multiple properties exist on the same line…

module.exports = {
    meta: {
        fixable: "code"
    },
    create(context) {
        const sourceCode = context.getSourceCode()

        return {
            Property(node) {
                if (node.key.name === "b") {
                    context.report({
                        node,
                        message: "Remove b",
                        fix(fixer) {
                            let prevToken = sourceCode.getTokenBefore(node)
                            while (prevToken.value !== "," && prevToken.value !== "{") {
                                prevToken = sourceCode.getTokenBefore(node)
                            }
                            let lastToken = sourceCode.getTokenAfter(node)
                            if (lastToken.value !== ",") {
                                lastToken = sourceCode.getTokenBefore(lastToken)
                            }

                            const startLine = node.loc.start.line
                            const endLine = node.loc.end.line
                            return fixer.removeRange([prevToken.range[1], lastToken.range[1]])
                        }
                    });
                }
            }
        };
    }
};
1reaction
mysticateacommented, Nov 3, 2017

It needs to handle tokens around the property with SourceCode object.

module.exports = {
    meta: {
        fixable: "code"
    },
    create(context) {
        const sourceCode = context.getSourceCode()

        return {
            Property(node) {
                if (node.key.name === "b") {
                    context.report({
                        node,
                        message: "Remove b",
                        * fix(fixer) {
                            yield fixer.remove(node);

                            const comma = sourceCode.getTokenAfter(node)
                            if (comma.value === ",") {
                                yield fixer.remove(comma)
                            }
                        }
                    });
                }
            }
        };
    }
};

But whitespaces are not token, so this code does not remove whitespaces. If you remove whitespaces as well, you have to use fixer.removeRange() method.

module.exports = {
    meta: {
        fixable: "code"
    },
    create(context) {
        const sourceCode = context.getSourceCode()

        return {
            Property(node) {
                if (node.key.name === "b") {
                    context.report({
                        node,
                        message: "Remove b",
                        fix(fixer) {
                            const startLine = node.loc.start.line
                            const endLine = node.loc.end.line
                            return fixer.removeRange([
                                sourceCode.getIndexFromLoc({ line: startLine, column: 0 }),
                                sourceCode.getIndexFromLoc({ line: endLine + 1, column: 0 })
                            ])
                        }
                    });
                }
            }
        };
    }
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Removing all properties from a object - Stack Overflow
@VisioN's answer works if you want to clear that specific reference, but if you actually want to clear an object I found that...
Read more >
java.util.Properties.remove java code examples - Tabnine
Extracts properties that start with or are equals to the specific prefix and returns them in a new Properties * object with the...
Read more >
delete object inside object javascript Code Example
How do I remove a property from a JavaScript object? how remove prperty or object in javscript · remove object from array of...
Read more >
Chapter 1. eXoJCR - JBoss.org
Returns an iterator * over the resulting <code>NodeType</code> objects. ... Node type is possible to remove only when the repository does not contain...
Read more >
Using and Operating Ceph - CERN
We provide here a script to help user make sure all versions of their objects are deleted. Usage: $> ./s3-delete-all-object-versions.sh -b <bucket> [- ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found