Commit graph

2347 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
Ron Buckton 2eb60c2cb2
Fix decoding of HTML entities in TSX/JSX (#35739) 2019-12-17 17:32:48 -08:00
Anders Hejlsberg 71a91763f4
Fix33448 (#35513)
* Filter out discriminants of type 'never'.

* Add tests

* Accept new baselines

* Remove unnecessary '!' assertion
2019-12-12 06:45:46 -08:00
Nathan Shively-Sanders b98cb0645d
Fix binding of this-assignments w/computed properties (#35639)
Top-level this-assignments do not support computed properties. But the
binder forgets to check for computed properties and tries to bind them
normally. This hits a helpful assert.

This change stop binding this-properties with computed properties at the
top-level.  There's nothing sensible we could do with them; we're unable
to late-bind entries to the global scope or to modules.
2019-12-11 15:55:08 -08:00
Anders Hejlsberg 09271f107d
Make no inferences from binding patterns with no defaults (#35454)
* Use nonInferrableAnyType in types inferred from binding patterns

* Add regression tests

* Accept new baselines
2019-12-05 07:09:45 -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
Wesley Wigham b9689228b5
When calculating spreads, merge empty object into nonempty object to … (#34853)
* When calculating spreads, merge empty object into nonempty object to produce partial object to reduce complexity

* Actually accept remainder of baselines

* Limit simplification to single prop or partial types
2019-11-22 17:19:17 -08:00
Nathan Shively-Sanders 369900bb07
Emit defineProperty calls before param prop assignments (#34987)
* Emit defineProperty calls before param prop assignments

Note that I restricted this to --useDefineForClassFields is true.
Nothing changes when it's off. I think this is the correct fix for a
patch release.

However, in principal there's nothing wrong with moving parameter
property initialisation after property declaration initialisation. It
would be Extremely Bad and Wrong to rely on this working:

```ts
class C {
  p = this.q // what is q?
  constructor(public q: number) { }
}
```

But today it does, and probably somebody relies on it without knowing.

* Put parameter property initialiser into defineProperty's value

* Combine ES5/ESNext into one test
2019-11-22 15:37:24 -08:00
Nathan Shively-Sanders 2075f74fef useDefineForClassFields skips emit of ambient properties (#35058)
* useDefineForClassFields skips emit of ambient properties

Previously:

```ts
class C {
  declare p
}
```

would incorrectly emit

```js
class C {
    constructor() {
        Object.defineProperty(this, "p", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
    }
}
```

when useDefineForClassFields was turned on (for targets <ESNext).

* Fix bug for ESNext as well

This moves the check earlier in the pipeline.

* update baselines
2019-11-22 14:52:29 -08:00
Anders Hejlsberg 94d4023043
Add inference priority level for conditional types in contravariant positions (#35199)
* Add inference priority level for conditional types in contravariant positions

* Accept new API baselines

* Add regression tests

* Accept new baselines
2019-11-21 13:05:44 -08:00
Kārlis Gaņģis 17f5469a2c Fix crash with Object.defineProperty for imported alias (--allowJs) (#35198)
Fixes #35196
2019-11-20 10:50:47 -08:00
Ron Buckton 6c59dc34ac
More tests for super.method call chain, improve vary-by (#35013) 2019-11-18 18:03:37 -08:00
Klaus Meinhardt fce728e07f fix emit for delete on optional chain (#35090)
* fix emit for delete on optional chain

* Apply suggestions from code review

Co-Authored-By: Ron Buckton <ron.buckton@microsoft.com>
2019-11-18 16:34:47 -08:00
Anders Hejlsberg 8b83703632
Properly strip readonly from rest argument types (#35169)
* Properly strip readonlyness from rest argument types

* Add tests

* Accept new baselines
2019-11-18 13:11:36 -08:00
Klaus Meinhardt 5321dcb09f disallow 'await' and 'yield' in property and enum member initializer (#34892)
* disallow 'await' and 'yield' in property and enum member initializer

* accept baseline changes

* Add a test for #34887

Ensures that this fixes #34887
2019-11-14 17:44:48 -08:00
Klaus Meinhardt 8f40ac06cc optimize transform of optional chaining and nullish coalescing (#34951)
* optimize transform of optional chaining and nullish coalescing

* remove unnecessary condition

* typo

* fix lint

* prevent capturing of super

* swap branches again

* accept new baselines

* avoid temporary objects
2019-11-14 17:34:13 -08:00
Anders Hejlsberg 38db7ae59e
Properly analyze switch statement bypass control flow (#35087)
* Properly analyze switch statement bypass control flow

* Add regression test

* Accept new baselines
2019-11-13 09:22:18 -08: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
Klaus Meinhardt 94f85901d7 strip QuestionToken from MethodDeclaration and PropertyDeclaration emit (#34954)
* strip QuestionToken from MethodDeclartion emit

Fixes: #34953

* test property emit
2019-11-12 10:30:46 -08:00
Anders Hejlsberg 3a5230ab3d
Defer switch exhaustiveness checks (#35000)
* Defer switch exhaustiveness checks until they're actually needed

* Add regression test

* Accept new baselines
2019-11-09 07:52:39 -08:00
Anders Hejlsberg 95be956320
Fix switch statement exhaustiveness checking (#34840)
* Don't optimize away CFA nodes representing missing default clauses

* Add regression test

* Accept new baselines
2019-11-05 12:05:41 -08:00
Ron Buckton ba5e86f140
Propagate 'undefined' instead of the optional type marker at an optional chain boundary (#34588)
* Propagate 'undefined' instead of the optional type marker at an optional chain boundary

* Update src/compiler/types.ts

Co-Authored-By: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
2019-11-01 11:36:22 -07: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 00dd1f0609
Add isIntersectionConstituent to relation key (#34789)
* Add isIntersectionConstituent to relation key

isIntersectionConstituent controls whether relation checking performs
excess property and common property checks. It is possible to fail a
relation check with excess property checks turned on, cache the result,
and then skip a relation check with excess property checks that would
have succeeded. #33133 provides an example of such a program.

Fixes #33133 the right way, so I reverted the fix at #33213
Fixes #34762 (by reverting #33213)
Fixes #33944 -- I added the test from #34646

* Update comments in test
2019-10-29 15:08:59 -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
Nathan Shively-Sanders 080e41dc90
Fix type reference to merged prototype property assignment (#34764)
The constructor function code path in the return type checking of
signatures needs to pass the *merged* symbol of the declaration to
getDeclaredTypeOfClassOrInterface. Other callers of
getDeclaredTypeOfClassOrInterface do this, or used an already-merged
symbol.

Fixes #33993
2019-10-28 10:14:04 -07:00
Anders Hejlsberg c404c08fde
Fix reachability analysis for ?? operator (#34702)
* Exclude ?? operator from true/false literal check in createFlowCondition

* Accept new API baselines

* Add tests

* Accept new baselines

* Address CR feedback

* Accept new API baselines
2019-10-24 15:57:14 -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
Kārlis Gaņģis 479d30646f Fix crash in assigning function with this ref to alias (#34650) 2019-10-22 14:33:45 -07:00
Nathan Shively-Sanders 1cbbe288ac
Treat any mix of element/prop access as declaration in JS (#34649)
Fixes #34642 but, notably, doesn't actually make the assignment into a
working declaration. It just fixes the crash.
2019-10-22 11:28:28 -07:00
Anders Hejlsberg f84fd300b8
Merge pull request #34607 from microsoft/fix33490
Fix type inference regression
2019-10-21 12:50:38 -07:00
Anders Hejlsberg ff6626f869
Merge pull request #34597 from microsoft/optionalChainControlFlow
More optional chaining control flow analysis
2019-10-21 12:36:07 -07:00
Anders Hejlsberg 56520da6f0 Add regression tests 2019-10-20 18:00:08 -07:00
Anders Hejlsberg d218a31a7d Add tests 2019-10-19 10:51:59 -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 fa1884ed1b
Fix crash in expando assignment to alias (#34566)
* Fix crash in expando assignment to alias

This PR disallows expando assignments

Fixes #34493, but disallows the prototype assignment nonetheless.

* Revert mistaken changes
2019-10-18 13:31:44 -07:00
Nathan Shively-Sanders 1d5add528d
Emit computed property temps even w/o init w/useDefineForClassFields (#34406)
Fixes #33857
2019-10-18 13:23:38 -07:00
Nathan Shively-Sanders 82f927f8dd
Fix stack overflow in circular assignment declaration (#34543)
* Fix stack overflow in circular assignment declaration

It also needs to have multiple assignments so that it has a ValueModule
flag.

Fixes #33006

* remove errant comment

* Remove other possible circularity

* Restore fallback with additional condition
2019-10-18 09:01:21 -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
Klaus Meinhardt 45d0ef9441 factory: parenthesize for-of expression when necessary (#34229)
Fixes: #33856
2019-10-17 10:17:33 -07:00
Klaus Meinhardt 178417f431 factory: correctly parenthesize conditional head (#34227)
Fixes: #34109
2019-10-16 10:41:52 -07:00
Nathan Shively-Sanders 29f9493d87
Fix crash when exporting+aliasing globalThis inside declare global (#34408)
* global module:Fix crash when exporting+aliasing globalThis

* Fix another globalThis crash

find-all-refs assumed that an export inside a `declare x` was always an
ambient module, but it is not -- `declare global` does not allow
`export`, so find-all-refs shouldn't return any refs for this error case.
2019-10-15 14:05:39 -07:00
Anders Hejlsberg e0bd810176 Update tests 2019-10-05 18:58:51 -07:00
Anders Hejlsberg eeaa752833 Add tests 2019-10-05 18:33:00 -07:00
Anders Hejlsberg afe6f4c95c
Merge pull request #33821 from microsoft/fix33806
Reflect control flow effects of optional chains in equality checks
2019-10-05 17:48:53 -07:00
Anders Hejlsberg f88518187b Add tests 2019-10-05 13:08:31 -07:00