8544 Commits

Author SHA1 Message Date
Caleb Sander
bb99c41230 Don't error on bigint literal used in type 2018-11-05 12:23:02 -08:00
Caleb Sander
6c59a3b890 Fix ambient initializer emit for bigint 2018-11-05 11:36:33 -08:00
Caleb Sander
ecd12453f4 Add test for bigint property 2018-11-05 11:36:32 -08:00
Caleb Sander
28e9ec497c Address review comments 2018-11-05 11:36:32 -08:00
Caleb Sander
a93131319e Add bigint tests 2018-11-05 11:36:32 -08:00
Caleb Sander
95c24c6c6a experimentalBigInt compiler flag 2018-11-05 11:36:31 -08:00
Nathan Shively-Sanders
4cb210ce2e
Parameters infer from body and call sites (#28342)
* Parameters infer from body usage and call sites

* Function expressions infer from variable decl usages

If the function expression is the initialiser of a variable declaration.

* Update isApplicableFunctionForInference too

* Update baseline
2018-11-05 11:29:16 -08:00
Wesley Wigham
eba83f4ea7
Add related span for mixin constructor error (#28319)
* Add related span for mixin constructor error

* Remove unneeded casts

* Nicer style
2018-11-02 17:29:05 -07:00
Nathan Shively-Sanders
ce85ee5115
Move infer-from-usage earlier in codefix list (#28318)
This makes it appear before the ts-ignore codefix, specifically.
2018-11-02 15:39:27 -07:00
Andy
4e59096ea5
Exclude keywords from import completions (#28114)
* Exclude keywords from import completions

* Still allow contextual keywords

* Add excludes tests
2018-11-02 14:33:02 -07:00
Nathan Shively-Sanders
a682a525b8
module.exports aliases have correct flags (#28303)
* module.exports aliases have correct flags

They are marked both as (1) alias and (2) assignment declaration. This
fixes alias resolution in cases where multiple module.exports
assignments exist, but differ in whether they are aliases or not:

```js
function f() { }
module.exports = f
module.exports = 23
```

Previously, this construct would fail to resolve the alias `f` because
the `export=` symbol would be marked as Alias | Value but not
Assignment. This change just adds Assignment so that the assignment
declaration alias-following rules apply: you should always follow the
alias, regardless of other flags.

Also, isAliasSymbolDeclaration needed to be tightened up. Previously, I
missed the condition that `module.exports =` aliases required an
EntityNameDeclaration on right-hand-side, just like `export default` and
`export =` aliases.

* Address PR comments

1. Rename test to be more accurate.
2. Always mark module.exports assignments with SymbolFlags.Assignment.
2018-11-02 09:08:04 -07:00
Nathan Shively-Sanders
8056e2b12f
infer from usage's unification uses multiple passes (#28244)
* infer from usage's unification uses multiple passes

Previously, the unification step of infer-from-usage codefix would stop
as soon an answer was found. Now it continues if the result is
*incomplete*, with the idea that later passes may provide a better
inference.

Currently, an *incomplete* inference is

1. The type any.
2. The empty object type `{}` or a union or intersection that contains
`{}`.

In the checker, any takes priority over other types since it basically
shuts down type checking. For type inference, however, any is one of the least
useful inferences.

`{}` is not a good inference for a similar reason; as a parameter
inference, it doesn't tell the caller much about what is expected, and
it doesn't allow the function author to use an object as expected. But
currently it's inferred whenever there's an initialisation with the
value `{}`. With this change, subsequent property assignments to the
same parameter will replace the `{}` with a specific anonymous type. For
example:

```js
function C(config) {
  if (config === undefined) config = {};
  this.x = config.x;
  this.y = config.y;
  this.z = config.z;
}
```

* Unify all passes of inference from usage

In the previous commit, I changed inference from usage to continue
inference if a the result was *incomplete*. This commit now runs all 4
inference passes and combines them in a unification step. Currently the
unification step is simple, it:

1. Gathers all inferences in a list.
2. Makes properties of anonymous types optional if there is an empty
object in the inference list.
3. Removes *vacuous* inferences.
4. Combines the type in a union.

An inference is *vacuous* if it:

1. Is any or void, when a non-any, non-void type is also inferred.
2. Is the empty object type, when an object type that is not empty is
also inferred.
3. Is an anonymous type, when a non-nullable, non-any, non-void,
non-anonymous type is also inferred.

I think I might eventually want a concept of priorities, like the
compiler's type parameter inference, but I don't have enough examples to
be sure yet.

Eventually, unification should have an additional step that examines the
whole inference list to see if its contents are collectively
meaningless. A good example is `null | undefined`, which is not useful.

* Remove isNumberOrString

* Unify anonymous types

@andy-ms pointed out that my empty object code was a special case of
merging all anonymous types from an inference and making properties
optional that are not in all the anonymous type. So I did that instead.

* Use getTypeOfSymbolAtLocation instead of Symbol.type!

* Unify parameter call-site inferences too

Because they still have a separate code path, they didn't use the new
unification code.

Also some cleanup from PR comments.

* Add object type unification test

Also remove dead code.

* Only use fallback if no inferences were found

Instead of relying on the unification code to remove the fallback.
2018-11-02 09:07:32 -07:00
Andy
eabbaa415f
Fix symbolCanBeReferencedAtTypeLocation for namespace that exports itself (#28295) 2018-11-01 15:10:21 -07:00
Wesley Wigham
deeee77f18
Check destructuring validity the same way element accesses and indexed accesses are checked (#24700)
* Check destructuring validity the same way element accesses and indexed accesses are checked

* Accept updated test baseline

* Use raw apparent type instead of passing in flag to sometimes make one

* Use `checkComputedPropertyName`
2018-11-01 13:46:41 -07:00
Wesley Wigham
3a2f7c0df1
Allow intersections of readonlys to be assignable to a readonly intersection (#28218)
* Allow intersections of readonlys to be assignable to a readonly intersection

* Add real motivating react example to test suite
2018-10-31 14:09:12 -07:00
Anders Hejlsberg
8e4b90da00
Merge pull request #28234 from Microsoft/genericSpread
Generic spread expressions in object literals
2018-10-31 12:52:16 -07:00
Anders Hejlsberg
2ed627aaf1 Update tests 2018-10-30 16:33:14 -07:00
Wesley Wigham
e2436f331a
Use constraint for default default value if possible (#28222) 2018-10-30 14:55:30 -07:00
Wesley Wigham
4cfff8962c
Fix declaration emit for cross-file enums (#28237) 2018-10-30 14:55:01 -07:00
Nathan Shively-Sanders
0481d44501
Bad callsite inferences fall back to body usage (#28235)
For parameters, the infer-from-usage codefix uses a substantially
different codepath that previously only looked at call site uses. When
this resulted in no inferences, or bad inferences, for a single
parameter, the codefix would just use any. Only if no usages of a
function were found would the codefix use the body-inference
code.

This commit makes parameter inference fall back to body-inference code
for individual parameters when there is no inference or inference to
any.
2018-10-30 13:25:24 -07:00
Andy
437bc41e99
For path completions, include extension as a kindModifier (#28148) 2018-10-30 11:56:34 -07:00
Klaus Meinhardt
33568795e0 Suggest adding to tsconfig after installing @types (#28211)
Ref: https://github.com/Microsoft/TypeScript/pull/28168#issuecomment-433554228
2018-10-30 11:33:05 -07:00
Andy
176627c818
Support augmenting module with export as namespace (#27281)
* Support augmenting module with `export as namespace`

* Warn on use of merged symbol containing 'export as namespace'
2018-10-30 11:16:26 -07:00
Anders Hejlsberg
e0d210d027 Add tests 2018-10-30 08:18:18 -07:00
Andy
1c4590341f
Avoid reformatting body of arrow function with single unused parameter (#28217) 2018-10-29 16:40:30 -07:00
Mine Starks
0879e163c9
Merge pull request #28212 from minestarks/configure-plugins-master
Merge pull request #28106 from minestarks/configure-plugins
2018-10-29 16:00:51 -07:00
Nathan Shively-Sanders
64ff195426
Set-only accessors spread to undefined (#28213)
* Set-only accessors spread to undefined

Previously they were skipped. The runtime behaviour is to create a
property of type undefined, unlike (for example) spreading numbers or
other primitives. So now spreading a set-only accessor creates a
property of type undefined:

```ts
const o: { foo: undefined } = { ...{ set foo(v: number) { } } }
```

Notably, `o.foo: undefined` not `number`.

Fixes #26337

* Fix isSpreadableProperty oversimplification
2018-10-29 14:51:12 -07:00
Mine Starks
1fbabd5534 Merge pull request #28106 from minestarks/configure-plugins
configurePlugins command for tsserver
2018-10-29 14:06:40 -07:00
Nathan Shively-Sanders
60efb65931
infer-from-usage suggestions can't be ignored, and always do something when invoked. (#28206)
* Do not ts-ignore noImplicitAny suggestions

Still need to write tests.

* Add tests

* More tests

* Update baselines
2018-10-29 13:23:33 -07:00
Andy
24febc2445
Allow to combine --resolveJsonModule with --isolatedModules (#28207) 2018-10-29 11:56:49 -07:00
Andy
c97fc64972
Always use verify.completions when testing completions (#28137) 2018-10-29 10:03:25 -07:00
Wesley Wigham
a6952887e9
Use same condition in isReferencedAliasDeclaration as isAliasResolvedToValue (#28171) 2018-10-29 09:38:10 -07:00
Wesley Wigham
e2100cd2cc
Measure variance of aliased conditional types using variance markers (#27804)
* Measure variance of aliased conditional types using variance markers

* Just do variance probing for all type aliases

* Small limiter for predictability

* Inline property set, remove unused functions
2018-10-26 16:26:20 -07:00
Anders Hejlsberg
ccc16136b2
Merge pull request #28170 from Microsoft/fixGenericMappedTypeConstraint
No constraint for { [P in K]: XXX } where K is type variable
2018-10-26 16:02:17 -07:00
Wesley Wigham
972c403cd8
JSX uses mixed signatures and union sigs use subtype on partial match (#28141)
* JSX uses mixed signatures and union sigs use subtype on partial match

* Small improvement
2018-10-26 16:01:32 -07:00
Anders Hejlsberg
24e3745296 Add regression test 2018-10-26 15:32:10 -07:00
Andy
36dfd775b3
Parse an object literal property as shorthand unless followed by '(' or ':' (#28121) 2018-10-26 15:00:31 -07:00
Jack W
abce9ae0be Bring typeof switch inline with if (#27680)
- Narrow unknown
- Narrow union members (in addition to filtering)
2018-10-26 14:56:26 -07:00
Nathan Shively-Sanders
372c7d9b0c
infer from usage JSDoc:Don't emit nested comments (#28161)
* infer from usage JSDoc:Don't emit nested comments

Previously, the trivia on a parameter name would show up inside the
emitted JSDoc comment. If the trivia contained a C-style comment, the
emitted JSDoc comment would be invalid. For example:

```js
function call(callback /*oh no*/) {
  return callback(this)
}
```

Emitted this comment:

```js
/**
 * @param {(arg0: any) => void} callback /*oh no*/
 */
```

* Remove misleading comment used for debugging.
2018-10-26 14:09:42 -07:00
Klaus Meinhardt
3fb8873bc2 don't resolve import types in JSDoc of TS files (#28158) 2018-10-26 09:50:12 -07:00
Nathan Shively-Sanders
dc9a066f65
Do not merge commonJS exports into an alias (#28133)
* Do not merge commonsjs exports onto an alias

getCommonJSExportEquals merges export assignments and export property
assignments. Something like this, which has no equivalent structure in
TS:

```js
module.exports = function() { }
module.exports.expando = 1
```

However, it is sometimes called with an alias, when its
parent, resolveExternalModuleSymbol, is called with dontResolveAlias:
true, and when the initialiser of the export assignment is an alias:

```js
function alias() { }
module.exports = alias
module.exports.expando = 1
```

In this case, (1) the actual value `alias` will have already merged in a
previous call to getCommonJSExportEquals and
(2) getTypeOfSymbol will follow the alias symbol to get the right type.
So getCommonJSExportEquals should do nothing in this case.

This bug manifests in the code for dynamic imports, which calls
getTypeOfSymbol on the incorrectly merged alias, which now has enough
value flags--Function, for example--to take the wrong branch and
subsequently crash.

* Update baselines
2018-10-25 15:08:06 -07:00
Andy
efc831e0ba
At '.' in array literal, don't close the array (#28120) 2018-10-25 12:25:33 -07:00
Anders Hejlsberg
6e8e5c10dd
Merge pull request #28112 from Microsoft/fixInstanceofControlFlow
Fix instanceof control flow analysis
2018-10-25 06:16:39 -07:00
Nathan Shively-Sanders
fe2a33fcbc
Merge existing JSDoc comments (#27978)
* Correct indentation, using correct (I hope) indentation code

Note that part of the code, in formatting.ts, is cloned but should be
extracted to a function instead.

* Remove some possibly-superfluous code

But I see 4 failures with whitespace, so perhaps not.

* Restrict indentation change to avoid breaking baselines

The indentation code is very complex so I'm just going to avoid breaking
our single-line tests for now, plus add a simple jsdoc test to show that
multiline jsdoc indentation isn't destroyed in the common case.

* Switched over to construction for @return/@type

Still doesn't merge correctly though

* Add @return tags to emitter

* Merge multiple jsdocs

(not for @param yet)

* Merge multiple jsdoc for parameters too

* Emit more jsdoc tags

Not all of them; I got cold feet since I'll have to write tests for
them. I'll do that tomorrow.

* Many fixes to JSDoc emit

And single tests (at least) for all tags

* Cleanup in textChanges.ts

* Cleanup in formatting.ts

(Plus a little more in textChanges.ts)

* Cleanup in inferFromUsage.ts

* Fix minor omissions

* Separate merged top-level JSDoc comments with \n

instead of space.

* Don't delete intrusive non-jsdoc comments

* Cleanup from PR comments

1. Refactor emit code into smaller functions.
2. Preceding-whitespace utility is slightly easier to use.
3. Better casts and types in inferFromUsage make it easier to read.

* Fix bogus newline

* Use @andy-ms' cleanup annotateJSDocParameters
2018-10-24 16:14:52 -07:00
Andy
854f20e90f
Remove 'verify.fileAfterCodeFix', use 'verify.codeFix' (#28110) 2018-10-24 15:34:15 -07:00
Anders Hejlsberg
ba8310ab8d Add regression test 2018-10-24 13:51:24 -07:00
Nathan Shively-Sanders
0db3038b57
Fix tests for node 11 (#28108)
1. Sort is now stable in node 11, which exposed a lack in the sorting of
nested ranges. Ranges now sort based on last ending if the start
positions are the same. This means nested ranges sort the
containing range first, even if a range contains another range that
starts at the same position.
2. Symbol has a new member description which can't be accessed through
the prototype. In addition, Array now has flat and flatMap, which I
excluded to keep baselines the same between Node 6-11.
2018-10-24 13:03:29 -07:00
Andy
eadf44d073
Add generateTypesForModule to public API (#28069)
* Add generateTypesForModule to public API

* Avoid parameter initializer and update baselines
2018-10-23 16:10:26 -07:00
Wesley Wigham
0a7c92864d
Fix Object.defineProperty declaration expando-ness and renames (#28061)
* Allow object.defineProperty calls to be expando properties

* Fix rename locations for object.defineProperty assignments
2018-10-23 09:33:53 -07:00
Andy
5becc3b170
Rename of non-intrinsic JSX element should rename the declaration (#28066) 2018-10-22 18:17:32 -07:00