Commit graph

129 commits

Author SHA1 Message Date
Nathan Shively-Sanders 3c5ecc2a60
Add jsdoc support for @public/@private/@protected (#35731)
* Add @private/@protected/@public test

* Fix @declaration

* draft abstraction + one usage

* Fill in necessary parsing etc

* make general getEffectiveModifierFlags

move to utilities, make the right things call it

* reorder public/private/protected

* JS declaration emit works with @public/@private/@protected

* revert unneeded/incorrect changes

* Update baselines and skip @public/etc when parsing

1. Update the API baselines with the new functions.
2. Do not check for @public/etc during parsing, because parent pointers
aren't set, so non-local tags will be missed; this wrong answer will
then be cached.

* Parser: don't call hasModifier(s) anymore.

Then move jsdoc modifier tag checks into getModifierFlagsNoCache where
they should be. The jsdoc checks are skipped when the parent is
undefined. There are 3 cases when this can happen:

1. The code is in the parser (or a few places in the binder, notably
declareSymbol of module.exports assignments).
2. The node is a source file.
3. The node is synthetic, which I assume to be from the transforms.

It is fine to call getModifierFlags in cases (2) and (3). It is not fine
for (1), so I removed these calls and replaced them with simple
iteration over the modifiers. Worth noting: Ron's uniform node construction
PR removes these calls anyway; this PR just does it early.

* Fix constructor emit

1. Emit protected for protected, which I missed earlier.
2. Emit a constructor, not a property named "constructor".
3. Split declaration emit tests out so that errors are properly reported
there.
2019-12-18 12:58:12 -08:00
Eli Barzilay d6740cf410 Fix getTypeFromJSDocValueReference
When using `{import('./b').FOO}` which is defined as a string literal,
`valueType` doesn't have a `symbol`.  Leave it for the fallback value
for now.

This was exposed in 8223c0752.

Fixes #34869.
2019-11-27 17:53:11 -06:00
Nathan Shively-Sanders f3344767dd
Fix import type resolution in jsdoc, mark 2 (#35057)
Fake alias resolution only applies when the import type is followed by a
qualified name. Otherwise the alias is sufficiently resolved already.
2019-11-12 12:44:30 -08:00
Wesley Wigham 8b7664ae15
Generate more correct property name nodes in declaration emit (#34741)
* Generate more correct property name nodes in declaration emit

* Silly only-on-CI lint rule T.T
2019-10-30 12:40:59 -07:00
Wesley Wigham d28672d97f
Fix alias naming and structure bugs in js declarations (#34786)
* Fix alias naming and structure bugs in js declarations

* Add another test case and change condition for ns merge to require signature or export content

* Fix typo in comment
2019-10-30 12:40:06 -07:00
Nathan Shively-Sanders 7635884224
JSDoc type reference understands require with entity name (#34804)
* resolve require with entity name postfix

For example, `require("x").c`. This is the value equivalent of
`import("x").a.b.c`, but the syntax tree is not as nicely designed for
this purpose.

Fixes #34802

* Add bug number to test

* Add optional chain test
2019-10-29 14:56:33 -07:00
Martin Probst a03227d60e Avoid a crash with @typedef in a script file. (#33847)
* Avoid a crash with `@typedef` in a script file.

Scripts (as opposed to modules) do not have a symbol object. If a script
contains a `@typdef` defined on a namespace called `exports`, TypeScript
crashes because it attempts to create an exported symbol on the
(non-existent) symbol of the SourceFile.

This change avoids the crash by explicitly checking if the source file
has a symbol object, i.e. whether it is a module.

* Add usage of exports.SomeName typedef.

* Fix bug at bind site rather than in declare func
2019-10-24 14:04:42 -07:00
Nathan Shively-Sanders d892fd408f
Fix expando handling in getTypeReferenceType (#34712)
* Fix expando handling in getTypeReferenceType

getExpandoSymbol looks for the initialiser of a symbol when it is an
expando value (IIFEs, function exprs, class exprs and empty object
literals) and returns the symbol.

Previously, however, it returned the symbol of the initialiser without
merging with the declaration symbol itself. This missed, in particular,
the prototype assignment in the following pattern:

```js
var x = function x() {
  this.y = 1
}
x.prototype = {
  z() { }
}

/** @type {x} */
var xx;
xx.z // missed!
```

getJSDocValueReference had weird try-again code that relied on calling
getTypeOfSymbol, which *does* correctly merge the symbols. This PR
re-removes that code and instead makes getExpandoSymbol call
mergeJSSymbols itself.

* Remove extra newline
2019-10-24 13:12:44 -07:00
Nathan Shively-Sanders 969634b97c
Restore delayed merge check to getTypeFromJSDocValueReference (#34706)
* Restore delayed merge check to getTypeFromJSDocValueReference

This is needed when a function merges with a prototype assignment. The
resulting *merged* symbol is a constructor function marked with
SymbolFlags.Class. However, the merge doesn't happen until
getTypeOfFuncClassEnumModule is called, which, in the
getTypeReferenceType code path, doesn't happen until
getTypeFromJSDocValueReference. That means the check for
SymbolFlags.Class is missed.

Previously, getTypeFromJSDocValueReference had a weird check
`symbol !== getTypeOfSymbol(symbol).symbol`, which, if true, ran
getTypeReferenceType again on `getTypeOfSymbol(symbol).symbol`. For
JS "aliases", this had the effect of dereferencing the alias, and for
function-prototype merges, this had the effect of ... just trying again
after the merge had happened.

This is a confusing way to run things. getTypeReferenceType should
instead detect a function-prototype merge, cause it to happen, and
*then* run the rest of its code instead of relying on try-again logic at
the very end. However, for the RC, I want to fix this code by restoring
the old check, with an additional check to make sure that #33106 doesn't
break again:

```ts
const valueType = getTypeOfSymbol(symbol)
symbol !== valueType.symbol && getMergedSymbol(symbol) === valueType.symbol
```

I'll work on the real fix afterwards and put it into 3.8.

* Add bug number
2019-10-24 09:24:58 -07:00
Nathan Shively-Sanders 8223c07527
getTypeFromJSDocValueReference: handle import types (#34683)
Previously it only handled types whose declaration was from `require`,
but now it handles types whose reference is an import type as well.
2019-10-23 15:53:38 -07:00
Nathan Shively-Sanders cdf1ab2dec
Bind @class in the right place -- bindWorker not bindChildrenWorker (#34575)
Also add an assert to make future mismatches fail in an obvious place
instead of in a while loop.
2019-10-18 14:13:14 -07:00
Nathan Shively-Sanders f5dbcb78af
Resolve more jsdoc value references as types (#34515)
* Resolve more jsdoc value references as types

* add test
2019-10-17 11:21:34 -07:00
Andrew Branch e2b4c12596
Detect unbindable exports before setting CommonJS module indicator (#33784)
* Detect unbindable exports before setting CommonJS module indicator

* Fix comment

* Fix unrelated lint?
2019-10-04 11:52:38 -05:00
Andrew Branch f89de95955
Allow special element access assignments to create declarations (#33537)
* Start enabling element access special assignment

* Treat element access assignment as special assignment in JS

* Make declarations for bindable element access expressions

* Fix navigationBar crash

* Add multi-level test for JS

* Propagate element access expressions to more code paths

* Fix property access on `this`

* Add quick info test

* Uhhh I guess this is fine

* Fix module["exports"] and property access chained off element access

* Add test for this property assignment

* Add test for and fix prototype property assignment

* Fix teeeest???

* Update APIs

* Fix element access declarations on `this`

* Fix go-to-definition

* Add declaration emit to tests

* Reconcile with late-bound symbol element access assignment

* Fix baselines

* Add JS declaration back to tests

* Fix JS declaration emit of non-late-bound string literal property names

* Revert accidental auto-format

* Use `isAccessExpression`

* Add underscore escaping member to test

* Fix and test navBar changes
2019-09-30 15:08:44 -05:00
Wesley Wigham 7f3c03d441
Implement declaration emit for accessors in the js declaration emitter (#33649)
* Implement declaration emit for accessors in the js declaration emitter

* Use `or`
2019-09-30 11:09:35 -07:00
Wesley Wigham 8d7fd2e691
Respect //@ts-nocheck in TS files (#33383) 2019-09-27 12:06:05 -07:00
Wesley Wigham 61cb06ce40
Allow allowJs and declaration to be used together (#32372)
* Allow allowJs and declaration to be used together

This intorduces a new symbol-based declaration emitter - currently this
is only used for JSON and JavaScript, as the output is likely worse than
what the other declaration emitter is capable of. In addition, it is
still incomplete - it does not yet support serializaing namespaces.

* Add tests for various import/export forms, add notes on export as namespace and fix export * from

* Tests & fixes for computed names

* Add test with current @enum tag behavior

* fix declaration emit for jsdoc @enum tags

* Small adjustments to base class serialization to fix bugs in it

* Guard against type/type parameter confusion when using typeParameterToName a bit

* Integrate feedback from PR

* Fix issue with export= declarations visibility calculation and type declaration emit that impacted all forms of declaration emit

* Only make one merged getCommonJsExportEquals symbol for a symbol

* Support preserving type reference directives in js declarations

* Skip declare mdoifiers for namespace members in ambient contexts

* FAKE ALIASES AND NAMESPACES EVERYWHERE

* Dont do namespace sugar when type members contain keyword names

* Fix json source file export modifier under new output

* Such clean nested aliasing, very wow

* Fix lint

* Add visibility errors, reuse type nodes where possible

* Suppoer having correctly named import types in bundled js declaration emit & adjust binding to allow namespaces with aliases to merge when the aliases look to be type-only

* Better support for module.exports = class expression

* Fix discovered crash bug

* Allow export assigned class expressions to be reachable symbols from external declarations

* Add missing semicolon

* Support @enum tag post-merge

* preserve comments on signatures and declarations where possible

* Basic support for js classy functions

* Add example we should do better with

* Prototype assignments make things a bit wonky, but the example from the PR seems OK

* Make a ton of changes to support the new way js classes are bound

* Remove some old comments, fix import and export default names

* Fix bug in object define handling and add tests for object define property declaration emit

* Fix organization nits from PR comments

* Preserve comments from jsdoc declarations on properties and js declaration type aliases

* Merge export declarations with identical specifiers

* Remove completed TODO comment

* Split lint

* Remove now-unused function

* PR feedback

* Add some project references tests, remove some checks from project refs codepaths that are now invalid

* Update project references tests again

* Merge and update project references tests

* Rename case

* Update test to include declaration output

* Remove yet another project refernces redirect extension check

* Update comment

* Add additional import ref to test

* Add shorthand prop to test

* Fix comment text

* Extract var to temp

* Simplify function and add whitespace

* Update project refs test to use incremental edit entry

* Stylistic refactors in the symbol serializer

* Another round of PR feedback, mostly style, small bugfix with constructors, and test showing bug in export assigned class expression name shadowing

* Use x instead of index
2019-09-26 14:27:16 -07:00
Anders Hejlsberg bf992a57f6
Merge pull request #32695 from microsoft/assertionsInControlFlow
Assertions in control flow analysis
2019-09-23 16:57:56 -07:00
Wesley Wigham 367b82055c
Hoist and distribute type parameter constraints over type parameters … (#33453)
* Hoist and distribute type parameter constraints over type parameters when comparing against union targets when fetching union constraints

* Fix PR nits
2019-09-23 16:52:03 -07:00
Anders Hejlsberg 21b5418cef Add tests 2019-09-20 17:17:39 -07:00
Nathan Shively-Sanders b542bdfbe4
Bind typedef/enum on all assignment decl kinds (#33240)
This fixes a crash on exports, but the code now handles all kinds
returned from getAssignmentDeclarationPropertyAccessKind.
2019-09-04 13:11:03 -07:00
Nathan Shively-Sanders 6ca9d04b60
Constructor functions as classes (#32944)
* Initial implementation

The original test passes but I haven't run any other tests yet, so I
assume the world is now broken.

* Append constructor function construct sigs

Instead of overwriting them

* Grab bag of improvements.

1. Mark @class-tagged functions with Class too.
2. Only gather local type parameters of constructor functions.
3. Remove getJSClassType calls with getDeclaredTypeOfSymbol.
4. Add a couple more failing tests.

getDeclaredTypeOfClassOrInterface now needs to understand prototype
assignment. That's next, I think.

* Prototype assignments work now

1. Binder marks prototype assignments as Class now.
2. Checker merges prototype assignments using the same merge code as for
functions and their declarations. No more intersections.

Many fewer failing tests now.

* Mark prototype-property assignments as Class

Even if there are no this-property assignments in them. (Then why are
you using a class?).

* Simplify getJSClassType, remove calls to its guts

It's probably not needed because now it's just a conditional call to
getDeclaredTypeOfSymbol, and I think most callers already know whether
they have a JS constructor function beforehand.

* isJSDocConstructor doesn't need to check prototype anymore

Because all the properties are merged during getDeclaredTypeOfSymbol.

* outer type parameter lookup follow prototype assignment

* this-type and -expression support in ctor funcs

Pretty cool!

* Fix remaining tests

* Fix minor lint

* Delete now-unused code

* Add class flag to nested class declarations

Also remove old TODOs
2019-08-19 14:12:53 -07:00
Brendan Kenny f45add0a7d checkJs: require JSDoc type argument for Array, Object, and Promise in noImplicitAny (#32829)
* Require type argument for JSDoc Array, Object, and Promise in noImplicitAny

* add jsdoc Array/Object/Promise noImplicitAny tests
2019-08-16 12:41:09 -07:00
Nathan Shively-Sanders 8454ef114d JSDoc:Treat tokens between backticks as comments
even `@`, which would otherwise start a new tag.
2019-06-26 16:04:46 -07:00
Nathan Shively-Sanders dabf2a6af2 Always check extends clause of classes
Even if (1) @extends is provided and (2) we're not producing
diagnostics. That's because we need to know whether the extends clause
references an imported alias.
2019-04-05 16:37:27 -07:00
Gabriela Britto b3633fab52 Add more tests for qualified name param without top level object error 2019-01-10 15:04:16 -08:00
Gabriela Britto ebe193c6d7 Minor refactor in paramTagNestedWithoutTopLevelObject.ts 2019-01-10 14:05:10 -08:00
Gabriela Britto e2524e3750 Add test for qualified name param without top level object error 2019-01-10 09:55:06 -08:00
Nathan Shively-Sanders 53bb4e84a2
Better checking of assignment declarations (#28387)
Previously, type checking was turned off for all assignment
declarations. This is a problem when the declarations are annotated with
jsdoc types.

This PR checks assignment declarations, *except* for expando
initialisers. Expando initialisers are

1. Empty object types.
2. Function types.
3. Class types.
4. Non-empty object types when the assignment declaration kind is
prototype assignment or module.exports assignment.
2018-11-15 08:46:11 -08:00
Nathan Shively-Sanders bf393ae1cd
Check EOF token to get errors for JSDoc (#28000)
* Check EOF token to get errors for JSDoc

* outputFile instead of noEmit for test
2018-10-19 16:23:34 -07:00
Wesley Wigham 69b1cb5bac
Add new special assignment kinds for recognizing Object.defineProperty calls (#27208)
* Add new special assignment kinds for recognizing Object.defineProperty calls

* Add support for prototype assignments, fix nits

* Fix code review comments

* Add test documenting behavior in a few more odd scenarios
2018-10-19 14:31:55 -07:00
Nathan Shively-Sanders c184184713
Add getEffectiveConstructSignatures (#27561)
* Add helpers that understand constructor functions

* getEffectiveConstructSignatures gets construct signatures from type, and
  call signatures from constructor functions if there are no construct
  signatures.
* getEffectiveConstructSignatureReturnType gets the "JS Class type" for
  constructor functions, and the return type of signatures for all other
  declarations.

This is a first step toward making constructor functions have construct
signatures instead of call signatures, which will also contribute to
fixing instantiation of generic constructor functions, which is basically
broken right now.

Note that the baselines *improve* but, because of the previously
mentioned generic problem, are still not correct. Construct signatures
for constructor functions and generic constructor functions turns out to
be an intertwined problem.

* Correct correct originalBaseType

And, for now, return anyType for generic constructor functions used as
base types. Don't give an incorrect error based on the function's return
type, which is usually void.

* Add error examples to tests

* Add construct signatures instead of getEffective* functions

* Fix typo in baseline

* Remove pesky newline

I thought I got rid of it!

* Test of constructor tag on object literal method

It doesn't work, and shouldn't in the future, because it's a runtime
error.
2018-10-15 12:47:57 -07:00
Nathan Shively-Sanders ca840ee683 Fix name resolution of exports in JS (#27394)
The ad-hoc name resolution rule for `exports` forgets to check the
requested meaning. When `getTypeReferenceType` calls`
resolveTypeReferenceName` with `Type` only in order to give an error
when the program uses a value like a type, it is incorrectly able to
resolve `exports` instead of producing an error. Then this incorrect
symbol gets treated like an alias, which it isn't, causing the assert.

The fix, for now, is to make resolution of `exports` check the requested
meaning so that it only resolves when `Value` is requested. This makes
the above code an error ("Cannot use the namespace 'exports' as a
type."), but I think this is fine for a bug fix. We can decide later if
`exports` should behave like other expandos and be a legal type
reference.

Note that the name actually does resolve correctly, so JS users will get
the desired completions. They'll just have an error to suppress if they
have checkJs on.
2018-10-08 13:01:14 -07:00
Nathan Shively-Sanders b185784708 Only functions can be constructor functions (#27369)
`@constructor` put on anything incorrectly makes it a JS constructor. This
is a problem for actual constructors, because getJSClassType doesn't
work on actual classes. The fix is to make isJSConstructor require that
its declaration is a function.
2018-10-08 10:14:31 -07:00
Nathan Shively-Sanders a4a5b3806e Report circular JSDoc type references (#27404)
JSDoc types references can often be to values, which can often be
circular in ways that types tied to declarations cannot. I decided to
create a separate property on SymbolLinks rather than reusing
declaredType, although I'm not sure that's strictly required.
2018-10-08 08:56:29 -07:00
Nathan Shively-Sanders 6d92a2942f
Fix parent points in unreachable code (#27400) (#27406)
In the binder, unreachable code mistakenly skips the `bindJSDoc` call in
`bindChildrenWorker`, which sets parent pointers. The fix is to call
`bindJSDoc` in the case of unreachable code as well.
2018-09-28 08:31:56 -07:00
Nathan Shively-Sanders 4fac5f26dc
Fix crash in use-before-def checking of enum tag (#27350) (#27354) 2018-09-26 09:05:18 -07:00
Nathan Shively-Sanders 59e4770a51
Fix enum tag circular references (#27161)
* Fix enum tag circular references

Also, don't try to resolve enum tag types in Typescript.

* Improve comment
2018-09-17 16:06:17 -07:00
Andy f71d6005a2
Use nextToken() after parsing a tag name so we can parse type keywords (#26915)
* Use nextToken() after parsing a tag name so we can parse type keywords

* Make callback to skipWhitespaceOrAsterisk non-optional
2018-09-13 15:49:06 -07:00
Tim Schaub 262ea5b06e Skip asterisks after newline when parsing JSDoc types (#26528)
* Skip asterisks after newline when parsing JSDoc types

* Single boolean expression

* Test for parsing and printing multiline function signatures with *
2018-09-04 15:41:08 -07:00
Nathan Shively-Sanders 64ac5a53f4
Fixes for type parameter name resolution in JS (#26830)
* check for expando initializers in resolveEntityName

when resolving type parameters in a prototype property assignment
declaration. For example, this already works:

```js
/** @template T */
function f(x) { this.x = x }
/** @returns {T} */
f.protototype.m = function () { return this.x }
```

This now works too:

```js
/** @template T */
var f = function (x) { this.x = x }
/** @returns {T} */
f.prototype.m = function () { return this.x }
```

Fixes #26826

* Lookup type parameters on prototype-assignment methods

In the same way that they're looked up on prototype-property methods.

That is, this previously worked:

```js
/** @template T */
function f() { }
/** @param {T} p */
f.prototype.m = function () { }
```

And this now works too:

```js
/** @template T */
function f() { }
f.prototype = {
  /** @param {T} p */
  m() { }
}
```

Note that the baselines still have errors; I'll file a followup bug for
them.

* Look up types on property assignments too
2018-09-04 14:47:18 -07:00
Tim Schaub 20a2b0cade Ignore newline and asterisk when parsing JSDoc typedef (#26775) 2018-08-30 10:01:33 -07:00
Nathan Shively-Sanders a2e4a282e7
Get [type] parameter types from @type tag (#26694)
* Get [type] parameter types from @type tag

Previously only the return type was used in cases like this:

```js
/** @type {<T>(param?: T) => T | undefined} */
function g(param) {
  return param;
}
```

Now the type parameters from the type tag are used, and the compiler
gets the type of the parameter by using the position in the signature of
the type tag.

Fixes #25618

* Fix split ifs according to PR comments
2018-08-27 16:52:35 -07:00
Nathan Shively-Sanders b50c37de78
No assert for nameless typedefs (#26695)
The assert is over-optimistic and should be removed until we can parse
every possible thing that people might put in a JSDoc type position.

Fixes #26693
2018-08-27 14:12:14 -07:00
Tim Schaub 6fd725f3ea Skip whitespace or asterisk in JSDoc param type and name (#26067) 2018-08-16 16:16:09 -07:00
James Keane a1089893bd Fixes #26128 - signature comp for jsdoc @class. (#26160)
* Fixes #26128 - signature comp for  jsdoc @class.

Another issue caused by js functions tagged with jsdoc
`@constructor` not having construct signatures.

A jsdoc function type that constructs a type (`function(new: Ex)`),
has a construct signature and return value inferred as the
constructed type where as a jsdoc `@constructor` has no construct
signatures, and it's call signature has a void return type
(or undefined).

i.e:
```javascript
/** @constructor **/ function E() {};

// typeof E -> call signature: () => void

/** @param {function(new: E)} d */ function c(d) {}

// typeof d -> construct: () => E
```

--

This commit fixes this (in an inelegant way) by considering `@class` function signatures as construct signatures and synthesizing it's return value _only for signature comparison_.

There might be a slight performance hit, since the synthesized return value is not cached; but changing the `@class` function's return type in `getReturnTypeOfSignature` causes other issues.

* Update jsdoc function test to fix mistake.
2018-08-14 13:35:51 -07:00
Nathan Shively-Sanders a6c5d50749
Allow type predicates in JSDoc (#26343)
* Allow type predicates

1. Parse type predicates. Note that they are parsed everywhere, and get
the appropriate error when used places besides a return type.
2. When creating a type predicate, correctly find the function's parameters
starting from the jsdoc return type.

* Fix type of TypePredicateNode.parent: add JSDocTypeExpression

* Update API baselines

* Handle JSDoc signature inside @type annotations

* Fix circularity when getting type predicates

Also move createTypePredicateFromTypePredicateNode closer to its use

* More cleanup based on review comments
2018-08-10 15:31:39 -07:00
Nathan Shively-Sanders a21ac11582
In JSDoc, resolve import types as values too (#26066)
* In JSDoc, resolve import types as values too

This is something that we probably should have been doing for some time.
Fixes #26049

* Fix whitespace lint
2018-07-31 11:07:06 -07:00
Nathan Shively-Sanders 25fb5419c0
Support the JSDoc @enum tag (#26021)
* Support the JSDoc @enum tag

`@enum` is used on a variable declaration with an object literal
initializer. It does a number of things:

1. The object literal has a closed set of properties, unlike other
object literals in Javascript.
2. The variable's name is resolvable as a type, but it just has the
declared type of the enum tag.
3. Each property's type must be assignable to the enum tag's declared type,
which can be any type.

For example,

```js
/** @enum {string} */
const Target = {
  START: "START",
  END: "END",
  MISTAKE: 0, // error 'number' is not assignable to 'string' -- see (3)
}

Target.THIS_IS_AN_ERROR; // See (1)
/** @type {Target} See (2) */
var target = Target.START;
```

* Fix lint, add new test case, update API baselines
2018-07-28 07:53:08 -07:00
Nathan Shively-Sanders 1cedab18be
Fix parsing of parenthesized JSDoc parameters (#25799)
* Fix parsing of parenthesized JSDoc parameters

Parenthesis can start a jsdoc function parameter since it is just a
type, and parenthesis can start a type:

```js
/** @type {function(((string))): void} */
```

However, this is not legal in other parameter lists:

```ts
function x((((a))): string) { }
```

This change makes jsdoc function parameter lists parse differently than
normal parameter lists by allowing parenthesis as a start character of
jsdoc parameters.

* Parse nested uses of jsdoc function types

* Fix test
2018-07-19 12:50:36 -07:00