js2-mode icon indicating copy to clipboard operation
js2-mode copied to clipboard

Automatically add missing semi-colons

Open Silex opened this issue 10 years ago • 18 comments

Hello,

I wrote a little hack:

(defun js2-add-semi-colons ()
  "Add missing semi-colons."
  (interactive)
  (--each (--select (string-equal (caar it) "msg.missing.semi") (js2-warnings))
    (let ((pos (nth 1 it))
          (length (nth 2 it)))
      (goto-char (+ pos length))
      (insert ";"))))

This is obviously not mature enough to include "as is", but what do you think of it? Maybe the *js-lint* buffer could offer some js2-error-apply-suggestion which would do the right thing in many cases?

Silex avatar Feb 20 '15 10:02 Silex

Looks like a fine idea. I'm not sure where best to put it, though.

If we continue this train of thought, "undeclared variable" warnings would probably be next (with choices: declare a local var, or declare the global in the top comment), and js2r-add-to-globals-annotation is already implemented in js2-refactor.

dgutov avatar Feb 20 '15 11:02 dgutov

Hum, I think I found a bug in js-lint about length of where the semi-colon should go:

var ratio = (clientMax - clientMin) / (max - min)

This underlines the whole line but stops at the ending ), so one char not enough. Any idea what triggers it? It seems related to the ending ), because the following is fine:

var ratio = (clientMax - clientMin) / (max - min) + 23

Silex avatar Feb 20 '15 13:02 Silex

Yep, it's a bug. Apparently, when js2-paren-node is parsed, its length is assigned an off-by-one value.

dgutov avatar Feb 20 '15 13:02 dgutov

Wow, that was quick :) Is there a fix planned or should I just work around it?

Silex avatar Feb 20 '15 13:02 Silex

Well, since you insist. :)

dgutov avatar Feb 20 '15 13:02 dgutov

:+1:

Silex avatar Feb 20 '15 13:02 Silex

I'll try to write some js2-fix-problem-at-point which would lookup into some table like

(setq js2-errors-suggestions '(("msg.missing.semi" js2-add-semi-colon-at-point)
                               ("msg.undeclared.var" js2-query-declare-var)))

Something like that.

Silex avatar Feb 20 '15 14:02 Silex

Sounds fine. Though keep in mind that you'll need copyright assignment, if you want this added to js2-mode proper.

dgutov avatar Feb 20 '15 14:02 dgutov

Does that require the sending of written documents to the FSF or whoever? Can't a simple "I'm ok with it" be enough?

I never quite understood why it needs to be that complicated to contribute, and don't get me wrong I know you have nothing to do with it :) To me it looks like it closes the door to so many people/packages/resources.

Silex avatar Feb 20 '15 15:02 Silex

Yes, and no. :)

dgutov avatar Feb 20 '15 15:02 dgutov

Do you have a link for the rationale behind it? I understand this line of reasoning in 1980 but I don't get it nowadays. If you make a PR you're obviously ok with the license. Can't they put in the license a clause that makes future contributors to give their copyright away? I mean if it bothers me I can't imagine the amount of people/contributions they lost just because of the bureaucratic process :)

Silex avatar Feb 20 '15 15:02 Silex

https://www.gnu.org/licenses/why-assign.html

Will this suffice? :)

Can't they put in the license a clause that makes future contributors to give their copyright away?

Apparently, they can't. Or even if they could, it would be an entirely different license, with different, more restricted, notion of software freedom.

dgutov avatar Feb 20 '15 15:02 dgutov

Alright I see, thanks for the link. It's interesting because it means in all my previous companies where I didn't assign copyrights I can prevent them from claiming copyright infringments :)

Anyway, why not use something like https://www.clahub.com ?

No need for slow-written-mail anymore! Just "sign" online that you agree and be done with it :) I had to "sign" one to contribute to https://github.com/openalpr/openalpr

Silex avatar Feb 20 '15 16:02 Silex

It's interesting because it means in all my previous companies where I didn't assign copyrights I can prevent them from claiming copyright infringments :)

Probably not. If you signed employment contracts, either they include a clause about you assigning copyright for the produced work, or otherwise the laws of your country do that by default anyway, as long as the work was performed during work hours, on company property.

Here's a recent thread about CLAHub: https://lists.gnu.org/archive/html/emacs-devel/2014-12/msg01454.html https://lists.gnu.org/archive/html/emacs-devel/2014-12/msg01505.html

Also note that a US or Germany resident should be able to sign the FSF CA electronically.

Let's not continue this discussion here: I don't have any power over FSF policies anyway.

dgutov avatar Feb 20 '15 16:02 dgutov

Alright, thanks for the infos!

Silex avatar Feb 20 '15 16:02 Silex

IMHO functionality like this is a better fit for js2-refactor mode anyway :)

apchamberlain avatar Mar 23 '15 08:03 apchamberlain

@apchamberlain: ah, yeah maybe you're right. I'm kinda stuck working on other stuffs but when I get back to it I'll consider js2-refactor.

Silex avatar Mar 23 '15 10:03 Silex

@dgutov: FYI, I finally sent the assignements paper to the FSF. I'll try to resurrect this PR, maybe on js2-refactor tho.

Silex avatar Aug 23 '16 09:08 Silex