Commit graph

2417 commits

Author SHA1 Message Date
Alexander T 696413749b
fix(37456): add tests for JsxOpeningElement nodes (#37752) 2020-04-02 10:38:31 -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
Alexander T c546988e50
fix(37456): omit type arguments from JsxSelfClosingElement, JsxOpeningElement nodes (#37739) 2020-04-01 16:27:33 -07:00
Ron Buckton 4fc4c4e3d6
Revert 'awaited' type (#37610) 2020-03-25 18:39:45 -07:00
Ron Buckton b58a29b808
Fix emit for optional chain with non-null assertion (#36539)
* Fix emit for optional chain with non-null assertion

* Treat '!' differently inside chain vs end of chain

* remove dead code and fix comment
2020-03-25 15:28:13 -07:00
Ron Buckton e3ec7b18b8
Add the 'awaited' type operator (#35998)
* Add the 'awaited' type operator

* Add script to manually add reviewers to a PR when GH 'Suggested Reviewers' breaks

* Fix lint error in review script

* Only defer generic awaited type for possible thenable

* Add variance-like behavior for awaited

* Switch awaited type params to 'unreliable' variance

* fix typo in inferTypes

* LKG without syntax in lib

* LKG with new syntax in lib

* Add 'strictAwaitedTypes' flag

* Treat strictAwaitedTypes as strict-mode flag

* Rename TAll, remove duplicate definition of 'race'

* Apply suggestions from code review

Co-Authored-By: ExE Boss <3889017+ExE-Boss@users.noreply.github.com>

* Fix inference priority

* Update comment to isGenericAwaitableType

* Add overloads for then/catch to Promise

* Add inference heuristic for T | PromiseLike<T> (for any PromiseLike)

* Remove strictAwaitedTypes flag

Co-authored-by: ExE Boss <3889017+ExE-Boss@users.noreply.github.com>
2020-03-20 16:09:01 -07:00
Anders Hejlsberg 0222211acb
Support for/of variables in assertion checking (#37432)
* Support for/of variables in assertion checking

* Integrate with "dotted name" logic

* Add tests
2020-03-17 19:20:56 -07:00
Alexander T f1eb9898fb
feat(36908): add 'property overwritten by spread' error for jsx attributes. add related span for conflicting declaration in spread (#37329) 2020-03-17 13:13:38 -07:00
Andrew Branch bc0e5a241c
Fix longer type-only property access in non-emitting heritage clauses (#37264)
* Fix longer type-only property access in non-emitting heritage clauses

* Rename misnomer function
2020-03-09 12:05:36 -07:00
Orta ffde92349d
Added @ts-expect-error to @ts-ignore directives (#36014)
* Added @ts-expect-error to @ts-ignore directives

Similar to `// @ts-ignore`, but will itself cause a new error diagnostic if it does not cause an existing diagnostic to be ignored.

Technical summary:
1. The scanner will now keep track of `CommentDirective`s it comes across: both `@ts-expect-error` and `@ts-ignore`
2. During type checking, the program will turn those directives into a map keying them by line number
3. For each diagnostic, if it's preceded by a directive, that directive is marked as "used"
4. All `@ts-expect-error` directives not marked as used generate a new diagnostic error

* Renamed to getDiagnosticsWithPrecedingDirectives per suggestion

* Added JSDoc comment I thought I did already

Co-authored-by: Orta <orta.therox+github@gmail.com>
2020-03-05 10:37:36 -05:00
Nathan Shively-Sanders 061338e82b
Don't inferFromIndexTypes() twice (#34501)
* Don't inferFromIndexTypes() twice

* Add tests
2020-03-04 13:19:35 -08:00
Wesley Wigham dfc0b58fe7
Preserve arity for preserving js optional parameters (#37173) 2020-03-04 00:48:53 -08:00
Nathan Shively-Sanders b481dd4d4b
More precise property-overwritten-by-spread errors (#37192)
* More precise property-overwritten-by-spread errors

Trying to do this check in getSpreadType just doesn't have enough
information, so I moved it to checkObjectLiteral, which is a better
place for issuing errors anyway.

Unfortunately, the approach is kind of expensive in that it

1. creates a new map for each property and
2. iterates over all properties of the spread type, even if it's a
union.

I have some ideas to improve (1) that might work out. I'm not sure how
bad (2) is since we're going to iterate over all properties of all
constituents of a union.

Fixes #36779

* another test and rename
2020-03-03 15:10:19 -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
Anders Hejlsberg be4b814a4c
Reduce intersections by discriminants (#36696)
* Treat never-like intersections as never

* Accept new baselines

* Fix compiler issues revealed by increased intersection correctness

* Delete fourslash tests that are no longer applicable

* Include isNeverLikeIntersection check in getNormalizedType

* Erase never-like types in several more places

* Check that base types are not never-like

* Add comments

* Revert isNeverLikeType check in getIndexType (keyof shouldn't resolve member types)

* Introduce getReducedType for union and intersection types

* Don't reduce in getApparentType

* Avoid relationship check in resolveMappedTypeMembers

* Accept new baselines

* Don't call getReducedType in getIndexType

* Ensure reduced and unreduced forms of a type can compare identical

* Reduce types before converting them to string representation

* Accept new baselines

* Reduce intersections before obtaining keyof X

* Add tests

* Accept new baselines

* Fix comment in tests

* Don't infer from empty intersection types

* Add tests

* Accept new baselines

* Defer instantiation of mapped type property types

* Accept new baselines

* Include more precise type in diagnostic

* Accept new baselines

* Minor optimization

* Improve error message

* Optional properties in intersections are never discriminants
2020-02-28 17:06:44 -08:00
Andrew Branch 0a6ee7753d
Grammar error on export type * (#37064)
* Recognize `export type *` syntax, but disallow it

* Add more comments to test

* Revert recognizing invalid forms as type-only

* Revert more
2020-02-27 16:35:15 -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
Klaus Meinhardt 24dd93fc2b
Fix access of protected members on generic this-type (#36928)
Fixes: #36926
2020-02-21 09:33:21 -08:00
Andrew Branch 1236a1006c
Fix initialization error when destructuring from object literal that includes a spread assignment (#36865)
* Add test

* Fix superfluous error when destructuring from object that includes spread assignment

* Update baseline to include error case

* Remove redundant check
2020-02-19 08:28:12 -08:00
Klaus Meinhardt 9b518c8f53
Exempt ambient private properties from noImplicitAny (#36640) 2020-02-14 15:01:18 -08:00
Nathan Shively-Sanders a772c26a71
Error when property is specified more than once via a spread (#36727)
* add tests but not baselines or fixes

* Update original change

Still probably wrong; probably doesn't even compile beacuse I'm just
typing on my laptop.

* fix error code ok

* notes to self

* Error: property is specified more than once via spread

* make jsx tests stricter

* update semicolon error message

* use ?. because it is great

* use maybeTypeOfKind in new code

* restore jsx error

* add tests
2020-02-11 10:23:15 -08:00
Brad Zacher 348c4dddc6
Throw syntax error for } and > in JSX text (#36636)
* Throw syntax error for `}` and `>` in JSX text

Fixes #36341

* Add codefix for error
2020-02-11 11:44:17 -05:00
Wenlu Wang 70399e146e
add support for Lift Template Literal Restriction (#23801)
* add support for Lift Template Literal Restriction

* rename file and improve comment and tests

* fix NoSubstitutionTemplateLiteral support

* extract tagged template and add more test

* avoid useless parameter

* fix incorrect return node if cannot transform

* accept baseline

* correctly baseline

* accept baseline

* fix merge break

* fix merge break

* inline rescan template head or no subsititution template

* update scan error

* add comment and fix lint

* refactor and fix lint

* avoid blank

* fix merge conflict

* fix again

* fix again

* use multiple target

* fix space lint

Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
2020-02-05 08:59:51 -08:00
Andrew Branch 86556d6c03
Fix export * that resolves to something type-only (#36558)
* Fix `export *` that resolves to something type-only

* Add same tests but non-tsserver
2020-01-31 15:09:27 -08:00
Eli Barzilay 1a1ed7464d Add a similar test for target.symbol.valueDeclaration
(With the same question still open.)
2020-01-30 22:42:04 -05:00
Eli Barzilay 68a9d4592f Avoid testing isClassDeclaration if there's no valueDeclaration
I'm not sure if this is enough -- perhaps it's better to make the test
dig through the interface/s to the class?

Fixes 36059.
2020-01-30 22:42:04 -05:00
Hye Sung Jung 8ed129771f
fix spelling errors (#36523)
* fix spelling errors

* remove changes in lib/*
2020-01-30 13:39:54 -08:00
Nathan Shively-Sanders c1e45ac8af
Fix use-before-def errors for ESNext property declarations (#36465)
* Fix use-before-def errors for ESNext property declarations

Fixes #36441
Fixes #36442

* Handle property declarations in nested classes
2020-01-30 10:58:07 -08:00
Andrew Branch 2fac535158
Fix type-only imports in interface 'extends' and import=/export= (#36496)
* Handle when files get checked in different orders

* Fix interface extends clause

* Fix import= something type only from a module

* Revert apparently unnecessary addition

* Revert "Revert apparently unnecessary addition"

This reverts commit 7444b0b72e.

* Disallow `import = a.b.c` on anything with type-only imports

* Safety first

* Add test for TS Server single-file open

* Add big comment

* Extract error reporting function for import aliases

* Delete blank line

* Un-export, comment, and colocate some utils

* Combine 3 type-only marking function calls into one

* Add more export default tests
2020-01-29 15:00:17 -08:00
Nathan Shively-Sanders 36169b4d13
Class fields w/esnext+[[Define]]:no shadow error (#36405)
* Class fields w/esnext+[[Define]]:no shadow error

With useDefineForClassFields: true and ESNext target, initializer
expressions for property declarations are evaluated in the scope of
the class body and are permitted to reference parameters or local
variables of the constructor. This is different from classic
Typescript behaviour, with useDefineForClassFields: false. There,
initialisers of property declarations are evaluated in the scope of
the constructor body.

Note that when class fields are accepted in the ECMAScript
standard, the target will become that year's ES20xx

* add negative test case

* Add explanatory comment
2020-01-29 14:47:44 -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
Nathan Shively-Sanders 0cf100dcf8
Add constructor functions to aliasable expressions (#36108)
* Add constructor functions to aliasable expressions

Fixes #35228, at least the crash. There are still a couple of errors
that are probably incorrect.

Note that isJSConstructor relies on parent pointers and the ability to
merge symbols, so I had to move isAliasSymbolDeclaration (back?) to the
checker.

* add simple test case

* remove errors in test

* fix bad merge
2020-01-29 09:36:59 -08:00
Andrew Branch 1bb6ea038f
Allow type-only namespace imports in implements clauses (#36464)
* Add test

* Allow type-only symbols in implements clauses

* Add more complex test
2020-01-27 16:13:30 -08:00
Nathan Shively-Sanders 757e67041e
Fix getExpandoSymbol for already-expando symbols (#36457)
* Fix getExpandoSymbol for already-expando symbols

getExpandoSymbol is intended to get the symbol of the expando in a
declaration like

```js
var C = class {
}
```

However, when this occurs in a `module.exports` assignment, alias
resolution already jumps to the exported symbol -- in this case the
expando symbol:

``js
// @filename: first.js
module.exports = class {
}
// @filename: other.js
/* @type {import("./first")} */
var C
```

`getExpandoSymbol` is then called on `class { }` instead of
`module.exports = class { }`.

Previously, `getExpandoSymbol` would incorrectly treat `class { }` the
same as the export assignment and get the expando ... from the expando
itself. This resulted in merging the expando with itself, which causes
bogus errors and could cause other problems.

Now `getExpandoSymbol` checks that the expando "initializer" is
different from the declaration.

* Better check for getExpandoSymbol of expando

Check the declaration directly instead of the initialiser.
2020-01-27 12:32:56 -08:00
Andrew Branch 20e8eba143
Issue better error for exporting primitive type (#36424)
* Issue better error for exporting primitive type

* Delete nonsense
2020-01-27 08:11:21 -08:00
Nathan Shively-Sanders 368db997ed
ESNext+[[Define]]: reference to param props illegal (#36425)
* ESNext+[[Define]]: reference to param props illegal

When target: "esnext" and useDefineForClassFields: true, property
declaration initialisers should not be able to reference parameter
properties; class fields are initialised before the constructor runs,
but parameter properties are not:

```ts
class C {
  foo = this.bar
  constructor(public bar: string) { }
}
```

emits code that looks like this:

```js
class C {
  bar
  foo = this.bar
  constructor(bar) {
    this.bar = bar
  }
}
new C('x').foo.length // crashes; foo is undefined
```

This PR adds an error on foo's declaration with ESNext+[[Define]].

* improve test
2020-01-24 14:53:28 -08:00
Nathan Shively-Sanders 43fc19c958
Emit statements before super (#36417)
* Emit statements before super

When statements come before super, Typescript's emit is incorrect,
whether there is an error or not. This change preserves statements that
come before super whether there is an error or not.

Here is the case with no errors:

```ts
class Test extends Array {
  p: number
  constructor() {
    console.log("p is initialised in the constructor below super()")
    super()
    this.p = 1
  }
}
```

Notice that `p` is manually initialised in the constructor after
`super()` instead of at the property declaration.

* Update baselines

Parameter properties in the error case now move below the super call.
This is an improvement because it means the code is more likely to execute
correctly.

* remove outdated comments
2020-01-24 14:52:48 -08:00
Wesley Wigham 9e712dd097
Consider SymbolFlags.Method as function-esque during js declaration emit (#36274) 2020-01-24 10:29:23 -08:00
Klaus Meinhardt 49eb0d7a2b Disallow 'declare' modifier on private named properties (#36381)
Fixes: #36345
2020-01-23 13:18:10 -08:00
Andrew Branch b05dde747c
Update type-only import semantics to allow type queries (#36092)
* Change type-only semantics to allow type queries

* Don’t error using type-only import in ambient context

* Fix default import

* Fix namespace import

* Update more baselines

* Prevent circular resolution

* Track const enum expression usage

* Update baselines

* Perf tuning 1

* Test commit for perf impact

* Weave type-only alias declaration finding into alias resolution

* Fix namespace import of type-only exported symbols

* type-only exports do not contribute to the module object type

* Update APIs

* Fix enum casing, remove type-only conversion suggestion

* Short circuit type-only checks in resolveEntityName faster

* Fix casing in API

* Remove unused parameter

* Fix error on qualified names in type queries

* Allow type-only imports in computed property names

* Fix computed property names of types and abstract members

* Remove unused util

* Commit missing baselines

* Rename “check” functions so as not to overload the word “check”
2020-01-23 12:53:36 -08:00
Andrew Branch eac2180e40
Be more tolerant with private identifier parsing, issue more targeted errors, and support private identifiers in forgotten 'this' codefix (#36188)
* Support private identifiers in forgotten this codefix

* Parse invalid private identifiers as identifiers and issue targeted errors

* Update codefix

* Remove accidentally deleted newline
2020-01-22 13:41:15 -08:00
Andrew Branch 1d5749ef78
Fix local and exported type alias merging (#36237)
* Fix local and exported type alias merging

* Add baselines
2020-01-22 13:16:49 -08:00
Anders Hejlsberg da61231039
Include super.XXX(...) assertion method calls in CFA (#36293)
* Support super.XXX in assertions

* Add tests
2020-01-22 11:21:11 -08:00
Anders Hejlsberg 8517df6fa2
Fix erroneous optional chain narrowing (#36145)
* Not all optional chains are narrowable references

* Add regression test
2020-01-16 16:49:51 -08:00
Klaus Meinhardt 6e3ab1529f remove private named properties from rest and spread types (#35950)
* remove private named properties from rest and spread types

Fixes: #35943

* code review
2020-01-14 14:47:39 -08:00
Nathan Shively-Sanders 13cddae3f7
Allow references to uninitialized ambient properties (#36112)
Previously these were incorrectly treated just like normal properties:

```ts
class Parent {
    a: any;
    constructor(arg: any) {
        this.a = arg;
    }
}
class Child extends Parent {
    declare a: number;
    constructor(arg: number) {
        super(arg);
        console.log(this.a);  // Property 'a' is used before being assigned. (2565)
    }
}
```

Fixes #35327
2020-01-10 10:50:05 -08:00
Anders Hejlsberg 357f715382
Check combined intersection properties against target index signatures (#35143)
* Check combined intersection properties against target index signatures

* Add tests

* Accept new baselines

* Less aggressive check for index signatures

* Track intersection membership state for both source and target

* Minor fixes
2020-01-10 10:46:10 -08:00