1

I have some JavaScript that I want to check to make sure I have no occurrences of using the assignment operator (=) as opposed to the equality operator (==). I've looked at JShint, JSLint, and the Google Closure Compiler. Unfortunately, no matter how I set the previous two tools, I get mounds of junk "errors" about all kinds of nit-picky little things, and the Google Closure Compiler does not detect this very common error.

Are there any tools I may use as an alternative to the three above? Really, I just want to check for this one error. I don't care about anything else.

9
  • The only other option ignoring the ones you've mentioned would be to use some form of regexp to look for it, good luck though. I would just use jshint or jslint and fix the rest of the "junk errors" while you're there. Commented Jan 29, 2015 at 20:27
  • 1
    You could also use your favorite editor to search for = and look at each occurrence manually. Commented Jan 29, 2015 at 20:28
  • Eek... I was hoping to avoid that... the project has over 12,000 lines. Commented Jan 29, 2015 at 20:34
  • 1
    Ouch. bet ya wish the project started out using jslint or similar from the beginning. Commented Jan 29, 2015 at 20:34
  • Best bet would be to use jslint or jshint and turn off as many of the optional checks as possible to reduce the amount of errors it catches Commented Jan 29, 2015 at 20:35

1 Answer 1

1

You can do that with grasp, it is similar to grep, but it searches Javascript abstract syntax trees.

For example, if you checkout the keybase node-client, and then run:

 grasp -r 'if.test[op==]'

It will match all the lines in .js files that contain if-statements that have assignment int their test, here's a screenshot:

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

Can you give a quick example call for this issue?
@ruffin, I am not entirely sure what you mean. I just want a tool that can scour all the lines of code I have to detect something like this: function test(x){ if(x=10){ //do something } }
@Tobi Thanks! I am definitely going to install that tool for use in the future. In the meantime, I ran all the code through various formatting applications so that the code style became uniform and I could search using a simple regular expression. In my case, I used UltraEdit to do so. Given there doesn't appear to be a tool that does exactly what I need, I think I just might make an open source program based on JSLint that looks for specific errors. I think that could help a lot of people.
@JoshuaDannemann This: grasp -r 'if.test[right.value=0]' app/assets/javascripts will match all if statements with the right value as 0. I can come up with an example for assignment.
@JoshuaDannemann Was a question for Tobi. Was trying to grok grasp usage, which he provided -- just looking for one that'd answer your question exactly. That's an interesting, and useful, tool. Edit: Ah, his latest edit is money. Sorry I can't upvote twice. That's neat.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.