Commit graph

185 commits

Author SHA1 Message Date
Nathan Shively-Sanders d2443a5df1
@typedef: Improve error spans from declaration emit (#42501)
* @typedef: Improve error spans from declaration emit

This is a proof-of-concept fix. I think it could be expanded for all of
jsdoc, but I only set it up for jsdoc type aliases. It could use a lot
of polish too.

* track error node in isSymbolAccessible instead

* Switch to using enclosingDeclaration

Remove trueErrorNode

* add test of @callback and @enum

* Better error + fix @enum error

Since enums don't have a name property, you *have* to call
`getNameOfDeclaration` to go looking through the AST for one.
2021-01-28 08:35:05 -08:00
Oleksandr T 303ed3a357
fix(42099): fix JSDoc optional properties declaration emit (#42116) 2020-12-28 13:34:55 -08:00
Oleksandr T a763600cc4
fix(41818): use last JSDoc comment related to host (#41858) 2020-12-18 13:24:58 -08:00
Wesley Wigham 69143ecc5b
Reuse input nodes where possible when serializing jsdoc implements clauses (#41783)
* Reuse input nodes where possible when serializing jsdoc implements clauses

* Whitespace changes, per PR feedback
2020-12-08 12:13:37 -08:00
Wesley Wigham 360958e04c
JSDoc declaration emit should reuse input nodes where possible when serializing typedefs (#41760)
* JSDoc declaration emit should reuse input nodes where possible when serializing typedefs

* Style comments
2020-12-04 13:42:35 -08:00
Wesley Wigham 9f9eed400c
Read the base construct signature from the static base type, not the instance base (#41767) 2020-12-02 13:30:46 -08:00
Wesley Wigham d1bc6b1ba6
Bind module.export = {Thing} with alias symbols 2020-10-27 15:43:00 -07:00
Wesley Wigham f4255dd237
Handle the new js binding element alias symbols in JS declaration emit 2020-10-21 11:24:19 -07:00
Nathan Shively-Sanders a109b5d5c8
Fix relative paths in commonjs decl emit w/property access (#40986)
```js
const x = require('./foo').y
```

was incorrectly using the unmangled require path as the temp name in
emit:

```
import ./foo_1 = require('./foo')
import x = ./foo_1.y
```

It now uses the imported identifier:

```
import x_1 = require('./foo')
import x = x_1.y
```

Discovered while fixing #37832
2020-10-09 16:32:57 -07:00
Nathan Shively-Sanders 4283428906
Fix export binding of namespaced typedefs (#40980)
The binder incorrectly rejected implicit namespace declarations in
typedefs.
2020-10-07 13:21:02 -07:00
Wesley Wigham da86332120
Limit export= js declaration emit to only json source files (#40882) 2020-10-01 14:55:24 -07:00
Wesley Wigham ad2a07440c
Fix crash on js declaration emit of export assigned default augmented function (#40596)
* Fix crash on js declaration emit of export assigned default augmented function

* {sp}
2020-09-23 00:50:12 -07:00
Nathan Shively-Sanders e350c357d2
Alias for module.exports.x = x (#40228)
* Alias for `module.exports.x = x`

This fixes #40155 in a surprisingly small amount of code.

* Treat any aliasable expression as an alias

* test internal references to exported class
2020-09-10 11:23:48 -07:00
Wesley Wigham 683979246f
Fix JS declaration emit for acessors in a class/interface merge (#40456) 2020-09-10 10:39:41 -07:00
Wenlu Wang ee5f51bc0f
Add see tag support (#39760)
* Add see tag parser

* add baseline

* fix symbol resolve

* add more case

* fix unittests

* improve tests and parser

* accept baseline

* Adopt package-lock.json and npm ci

* Add a workflow to update package-lock.json daily

* Git ignore package-lock.json and forcibly update in workflow

* Update bot email address

* Delete extra npm update

* Update package-lock.json

* Add compactDisplay and signDisplay to NumberFormatOptions (#40039)

* Fix typo in (Readonly)Set.keys comment (fixes #40164) (#40176)

* fix(26325): use a unique name for reserved words in 'constructor like' function name (#39684)

* fix(25770): add diagnostic message for the possible mapped type used as an index (#39973)

* fix(31046): add new diagnostic message for incompatible constructor signature (#40073)

* Update package-lock.json

* Update package-lock.json

* Add rename support

* Accpet baseline

* wip

* fix anders

* Revert "fix anders"

This reverts commit b3178d4618.

* Fix call hierarchy item serialization and server tests (#40348)

* Avoid error

* accept baseline

* Add more tests

* Add signature name resolve

Co-authored-by: Andrew Casey <andrew.casey@microsoft.com>
Co-authored-by: TypeScript Bot <typescriptbot@microsoft.com>
Co-authored-by: Neil Kistner <neil.kistner@gmail.com>
Co-authored-by: cherryblossom000 <31467609+cherryblossom000@users.noreply.github.com>
Co-authored-by: Alexander T <alexander.tarasyuk@outlook.com>
Co-authored-by: Erich Gamma <egamma@microsoft.com>
Co-authored-by: Andrew Branch <andrewbranch@users.noreply.github.com>
2020-09-09 10:45:09 -07:00
Wesley Wigham 155610e114
Better support class instances assigned to the module object for JS declarations (#40037)
* Better support class instances assigned to the module object for JS declarations

* Extract constant
2020-09-05 02:23:12 -07:00
Nathan Shively-Sanders c3d41bbb73
Alias for commonjs require in JS (#39770)
* First attempt at aliases for require

* test+initial support for const x=require

* 1st round of baseline improvements

* 2nd round of baseline updates

* support property access after require

* check @type tag on require

* forbid expando missing namespaces on aliases

taken from #39558 as soon as it was created

* accept error baselines that are good, actually

* Scribbling on d.ts emit code

* use getSpecifierForModuleSymbol

* hideous hack for module.exports of aliases

* Fix module.exports.x --> export list emit

* fix isLocalImport predicate

* require only creates aliases in JS

* re-handle json imports

* update fourslash baseline

* Cleanup in the checker

1. Simplify alias resolution.
2. Simplify variable-like checking.
3. Make binding skip require calls with type tags -- they fall back to
the old require-call code and then check from there.

I haven't started on the declaration emit code since I don't know what
is going on there nearly as well.

* Function for getting module name from require call

* First round of cleanup plus a new test

Found one missing feature, not sure it's worth adding.

* more small cleanup

* more cleanup, including lint

* use trackSymbol, not serializeTypeForDeclaration

* Code review comments, plus remove unneeded code

Ad-hoc type reference resolution for `require` isn't needed anymore.

* find all refs works

* remove old ad-hoc code

* make it clear that old behaviour is not that correct

* update api baselines

* remove outdated comment

* PR feedback

1. Fix indentation
2. Add comment for exported JSON emit
3. Add test case for nested-namespace exports.

* add a fail-case test (which passes!)
2020-08-17 14:00:37 -07:00
Eli Barzilay 620e260576 Avoid the double-symbol trick for enums
Nameless jsdoc typedefs have their exportedness controlled by the
exportedness of the location they pull their name from.

Fixes #33575.
2020-08-12 18:40:57 -04:00
Wesley Wigham 94989789df
In JS declaration emit, move imports painted in nested contexts to the root private context (#39818)
* In JS declaration emit, move imports painted in nested contexts to the root private context

* Add test for nathan
2020-07-31 18:25:37 -07:00
Nathan Shively-Sanders 9b2d487392
Fix this-parameter emit for JSDocFunction types (#39814)
* Fix this parameter emit for JSDocFunction types

Previously, parameters with names that were not `new` were treated like
rest parameters. This is incorrect: parameters with the name `this`
should emit a `this` parameter.

Fixes #38550

* ❤️ quote style
2020-07-29 14:11:59 -07:00
Nathan Shively-Sanders 3b107fec3b
Fix @param type parameter lookup (#39532)
Previously, getObjectTypeInstantiation had special-case code to look up
type parameters for `@param` as if they were in the parameter location.

This should occur in the main lookup loop of `getOuterTypeParameters`,
however. The current code only runs once, which is not sufficient, and
it also jumps to the parameter for any type contained in a `@param`,
which skips type parameters that occur in the tag itself.
2020-07-10 08:42:25 -07:00
Wesley Wigham dd2b2447d0
Add regression test for #38834 (#39479) 2020-07-08 13:32:15 -07:00
Nathan Shively-Sanders 53a756ea63
Type this in more constructor functions (#39447)
* Type `this` in more constructor functions

Previously,  `this: this` in constructor functions only when there was
an explicit `@constructor` tag on the function. Now, `this: this` for
any function that's known to be a constructor function.

This improves completions inside constructor functions; also note that
previously the compiler *did* type `this: this` inside methods of constructor
functions, so this fix makes us more consistent. This is reflected in
the large number of baselines that improve.

The fix is a simple switch to `isJSConstructor`, which is the standard
way to detect constructor functions. I'm not sure why the original PR
didn't use this method.

I remember discussing this limitation in the original bug, #25979, and
I guess I decided that it made sense. But I was heavily primed by the bug's
framing of the problem in terms of `noImplicitThis`, which *should*
require an explicit `@constructor` tag.

With better typing comes better detection of `@readonly` assignment; I
had to fix the readonly detection code to use `isJSConstructor` as well.

* Remove `Add @class tag` fix for noImplicitThis.

The new rules mean that it never applies. It's possible that it should
apply to functions like

```js
function f() {
  this.init()
}
```

In which `init` is never defined, but I think this program is incomplete
enough that not offering the fix is fine.

* Fix precedence of `@this`

Previously, both `@class` and `@this` in a jsdoc would cause the `@this`
annotation to be ignored. This became a worse problem with this PR,
because `this` is correctly typed even without the annotation.

This commit makes sure that `@this` is checked first and used if
present.
2020-07-08 08:44:17 -07:00
Anders Hejlsberg 8c6b85835f
Properly handle rest parameters in function declarations with @type annotations (#39473)
* Properly handle rest parameters in function declarations with @type annotations

* Add tests
2020-07-07 16:35:37 -07:00
Wesley Wigham d2b32b422f
Ensure type/namespaceish statics are included in the list of namespace merge members (#38920)
* Ensure type/namespaceish statics are included in the list of namespace merge members

* Simplit into two lines

* Update baseline post-merge
2020-07-07 16:28:09 -07:00
Nathan Shively-Sanders 9a65658b28
Parse *= separately in types (#39457)
* Parse *= separately in types

Previously, when the scanner produced `*=` as a single token, the type
parser ran special-case code to produce an "optional all type", which
only makes sense when the `=` really should be attached to the `*`. This
is often not the case.

The correct solution, which I missed when I first wrote this code, is to
have the scanner go back and produce a separate `=` token, which is what
this PR does.

* add test from #38551

* we ❤️ semicolons
2020-07-07 08:30:42 -07:00
Nathan Shively-Sanders c3c6be6618
In JSDoc, parse postfix-? below conditional types/tuple types (#39123)
Outside of JSDoc comments, postfix-? is parsed at lower precedence than
the `?` of conditional types, and a postfix-? inside a tuple type
results in the type being marked optional.

This PR changes JSDoc parsing to behave the same way, which means that

1. Conditional types are allowed in JSDoc. Fixes #37166.
2. Tuple types' postfix-? syntax is interpreted correctly in JSDoc.
Fixes #38747.

The breaking change is that a postfix-? type followed by another postfix type,
like `[]` or `!`, is parsed as a conditional type. [Postfix-? is not
common](https://github.com/microsoft/TypeScript/issues/37166#issuecomment-612274456),
so this is an acceptable breaking change.

A postfix-? type `T?` is still parsed everywhere else and treated as `T | null`.
2020-06-17 14:06:39 -07:00
Nathan Shively-Sanders 9f872c01e1 Merge branch 'master' into fix-implements-tag-emit 2020-06-11 16:05:21 -07:00
Wesley Wigham 08cb0b23e8
Serialize (noncontextual) keyword named namespace members with export declarations in both declaration emitters (#38982)
* fix(38750): create unique names for keywords and re-export them with original names

* Serialize (noncontextual) keyword named namespace members with export declarations in both declaration emitters

* Add exhaustive keyword emit test for js declaration emitter and fix it up

Co-authored-by: Alexander T <alexander.tarasyuk@outlook.com>
2020-06-10 14:42:49 -07:00
Wesley Wigham 2287dbc7e2
Handle missing return type nodes and nested type references missing type arguments in existing jsdoc node serialization (#39011)
* Handle missing return type nodes and nested type references missing type arguments in existing jsdoc node serialization

* Accept updated baselines
2020-06-10 12:42:38 -07:00
Nathan Shively-Sanders c5f66716cf Fix @implements emit for namespaced base types
Fixes #38640
2020-05-20 10:29:39 -07:00
Ron Buckton 1cbe7ef000
Fix crash in JS declaration emit (#38508)
* Fix crash in JS decl emit

* Emit as class with private ctor
2020-05-15 14:00:59 -07:00
Ron Buckton d07e866a28
Fix for jsdoc modifiers on constructor params (#38403)
* Fix for jsdoc modifiers on constructor params

* Update Public API baseline and fix unique symbol grammar check for js
2020-05-11 15:07:43 -07:00
Wenqi 1b7b3eb0f7
report error for duplicate @type declaration (#38340) 2020-05-11 12:47:49 -07:00
Wesley Wigham 7b03835b77
Fix js missing type arguments on existing nodes and jsdoc object literal declaration emit (#38368)
* Fix js missing type arguments on existing nodes and jsdoc object literal declaration emit

* Add special lookups test case, rename helper

* Accept slightly modified baselines
2020-05-08 10:46:28 -07:00
Alexander T c219fdae08
fix(37703): forbid required parameter after optional (#38155) 2020-05-05 10:29:17 -07:00
Wesley Wigham 815dc90dc5
Issue an error on cross-file merges we cant emit (#38148) 2020-04-23 19:01:16 -07:00
Wesley Wigham 136f728bb0
Fix js declaration emit for inherited and this-typed inherited fields (#37970) 2020-04-21 13:13:50 -07:00
Alexander T a2628c9088 fix(37242): add reference to return type for JSDocFunctionType 2020-04-17 09:05:23 +03:00
Wesley Wigham b4838c8b62
Use comments from host variable declaration when exporting a signature in js declarations (#37594) 2020-04-08 13:32:17 -07:00
Nathan Shively-Sanders 7ca6334dbd
Look for outer type parameters on VariableStatements (#37819)
This only applies in JS, where `@template` tags can apply to
initialisers of variable declarations:

```js
/**
 * @template T
 * @returns {(b: T) => T}
 */
const seq = a => b => b
```

Fixes #36201
2020-04-07 08:04:33 -07:00
Nathan Shively-Sanders eac073894b
Fix serialisation of static class members in JS (#37780)
* Fix serialisation of static class members in JS

Previously static class members would be treated the same way as expando
namespace assignments to a class:

```ts
class C {
  static get x() { return 1 }
}
C.y = 12
```

This PR adds a syntactic check to the static/namespace filter that
treats symbols whose valueDeclaration.parent is a class as statics.

Fixes #37289

* fix messed-up indent

* Extract function
2020-04-03 20:06:05 -07:00
Wesley Wigham 78a99241d8
Reuse input type nodes when serializing signature parameter and return types (#37444)
* Accept change

* Accept the huge set of ever so slightly changed baselines

* Update return type logic to only reuse nodes if original nodes share scope with current node, like property types, only reuse nodes if symbols referened are acessible, reuse nodes for property signatures, too

* Only reuse nodes when a context is provided (otherwise identifier printback may fail)

* Only track symbol if symbol is found and no error is recorded

* Fix type parameter reuse lookup

* Forbid cjs module.exports references in retained nodes

* Adjust check for cjs export references to not include bad module type in output

* Add symbol to all identifiers we see in existing nodes for quickinfo

* Accept fourslash baseline updates

* Accept slightly updated baseline post-merge

* Do not copy original nodes for error types, replace empty type references with any
2020-04-01 19:50:21 -07:00
Wesley Wigham dfc0b58fe7
Preserve arity for preserving js optional parameters (#37173) 2020-03-04 00:48:53 -08:00
Wesley Wigham 5941c6e1b1
Emit an any for namepath types (#37176) 2020-03-03 10:55:21 -08:00
Wesley Wigham d209092ccc
Use merged symbol to pick up js-alias-merged members on export assignments (#37162) 2020-03-02 12:57:00 -08:00
Nathan Shively-Sanders 392fd0ac0b
Remove bogus @implements errors (#37114)
* Remove bogus @implements errors

Make the search for the actual host more comprehensive by reusing the
code that previously only searched for functions. I don't know what to
call this function now, since the old name wasn't accurate either.

* undo gratuitous name change

* Improve name and make calling more uniform

It's slightly less efficient but I think worthwhile for readability.
2020-03-02 09:48:53 -08:00
Titian Cernicova-Dragomir f883bf3acb
Adding support for @implements. (#36292)
* Adding support for @implements.

* Fixed code review issues for @implements, added some more tests.

* Fixed declaration emit for @interface

* Improved getImplementsTypes to not cache the results since it is only used once.

* Removed unnecessary checks from getImplementsTypes
2020-02-27 09:27:37 -08:00
Nathan Shively-Sanders 3e3df8702c
Fix crash on aliased,exported @enum tag in jsdoc (#36996)
THe code to bind `@enum` and `@typedef` didn't handle the case that the
`@enum` was on a property assignment to an alias of module.exports.
Specifically, `x` needs to be correctly aliased to the file's symbol in
the example below:

```
var x = module.exports = {};
/** @enum {string} */
x.E = {
  A: "A"
};
```
2020-02-24 16:13:18 -08:00
Nathan Shively-Sanders 1bbcb5553a
@typedef's nested Object syntax disallows type arguments (#36172)
Previously the jsdoc index signature syntax was incorrectly treated the
same as Object:

```js
/** @typedef {Object} AllowsNesting
 * @property ... */
/** @typedef {Object.<string,string>} IncorrectlyAllowsNesting */
```

Fixes #34911
2020-01-29 12:53:00 -08:00