marijnh/tern — pull request #188 by sqs (2013-07-06 23:06:13+00:00)
Title: Handle primitive-!type spec in def files

Outputting spans on primitive-typed defs seems to be confusing passOne in def.js when the defs file is used.

Suppose we have the def:

```json
{
"!name": "a/b.js",
"foo": {
"!type": "string",
"!span": "1[1:1]-2[2:2]"
}
}
```

Then reading in this def causes the following error:

```
$HOME/src/tern/lib/def.js:283
else throw new Error("Invalid !type spec: " + tp);
^
Error: Invalid !type spec: string
at passOne ($HOME/src/tern/lib/def.js:283:20)
at passOne ($HOME/src/tern/lib/def.js:297:7)
at doLoadEnvironment ($HOME/src/tern/lib/def.js:355:5)
at Object.exports.load ($HOME/src/tern/lib/def.js:379:7)
at $HOME/src/tern/lib/infer.js:608:13
at Object.exports.withContext ($HOME/src/tern/lib/infer.js:618:18)
at new exports.Context ($HOME/src/tern/lib/infer.js:592:13)
at Object.signal.mixin.reset ($HOME/src/tern/lib/tern.js:113:17)
at Object.exports.Server ($HOME/src/tern/lib/tern.js:99:10)
at $HOME/src/tern/test/runcases.js:69:18
```

The problem only occurs when isSimpleAnnotation() on the def is false (if there is only a `"!type"` key, it works).

I've included my attempt at a fix, but it may not handle all of the edge cases.

-----------------------

Motivation: I encountered this issue when running `bin/condense backbone.js` on http://backbonejs.org/backbone.js (v1.0.0). It outputs the following snippet:

```
"Backbone": {
"!doc": "Save the previous value of the `Backbone` variable, so that it can be restored later on, if `noConflict` is used.",
"VERSION": {
"!type": "string",
"!span": "1089[36:11]-1096[36:18]"
},
```

in which `VERSION` triggers the error.