tern icon indicating copy to clipboard operation
tern copied to clipboard

Where to extend tern to allow for (specific) non-valid JavaScript

Open JamesThorpe opened this issue 10 years ago • 2 comments

In our application we have an area where users have the ability to enter JavaScript calculations to work with fields on the page. These fields are denoted by using square brackets (and we're unlikely to be able to change this any time soon), so a calculation could look something like:

[Full Name] = [First Name] + ' ' + [Last Name];

When these calculations are saved, we swap out these tokens for the actual underlying html objects, so it's actually transformed into something like:

F1.value = F2.value + ' ' + F3.value;

We've been using CodeMirror for some time to allow for syntax highlighting of these calculations, and I've written an overlay that will happily parse these out and colour them. We've also introduced linting through CodeMirror, with each of these being replaced with a representative number of underscores so the linting code just "sees":

___________ = ____________ + ' ' + ___________;

and reports any errors as though they were normal variables. We're now at the stage however where we'd also like to introduce autocompletion. We had hacked together something before based on some very custom code running on top of CodeMirror, but I'd like to instead use tern to allow for all the support it has.

Getting to the point - given the fact that our placeholders look like arrays as far as tern is concerned, is there an obvious starting point for me to begin looking at what I might need to extend to support custom tokens in this fashion? Ideally I want tern to just treat each one as an identifier, along with support for the autocompletion lists.

JamesThorpe avatar Jul 14 '16 10:07 JamesThorpe

You could either pre-process the code to, for example, replace [Full Name] with ø.Full_Name, and add a definition file that defines ø in the proper way, or you could write an Acorn plugin that parses these as special nodes, and then a Tern plugin that handles these nodes correctly. The latter would involve learning quite a lot about the internals of both those libraries, though.

marijnh avatar Jul 14 '16 10:07 marijnh

(Closing, as this isn't an issue with Tern. Next time, prefer https://discuss.ternjs.net/ for discussion threads.)

marijnh avatar Jul 14 '16 10:07 marijnh