Commit Graph

594 Commits

Author SHA1 Message Date
Joseph Wunderlich
21ab39649c remove any annotation from declare method quickfix 2019-02-22 17:24:21 -08:00
Jesse Trinity
f0a6154e9b Added auto setting to quotePreference 2019-02-06 14:37:26 -08:00
xiaofa
ddbac8f3fe get appropriate parent jsdoc node for arrow functions 2019-01-26 23:59:32 +08:00
Wesley Wigham
3d2bf6a75f Fix implement interface quickfix import types (#29410)
* Pass module specifier resolution host thru types constructed by implements quickfixes

* Add regression test

* Fix scope node for generated methods, fix lints
2019-01-14 13:56:27 -08:00
Josh Goldberg
7b6adae6dd Extracted compilerOptions setting to helper function 2019-01-11 15:05:24 -05:00
Josh Goldberg
8d28f9230c Added codefix to enable experimentalDecorators in the user's config file
Starts on #29035 by creating a codefix to enable the `experimentalDecorators` setting in a user's config file, if one exists. The issue's discussion also mentions giving a more precise error message if the user has a jsconfig or tsconfig or creating one if not; I'd rather tackle those in separate PRs to keep this one small.

Doesn't create the code action if no config file is present. Otherwise keeps to the precedent of returning without action when the config file contents aren't the expected JSON structure (looking at `fixCannotFindModule.ts`).  Moves a couple JSON helpers from that file into the sibling `helpers.ts` so both codefixes can use them.
2019-01-11 09:20:12 -05:00
Sheetal Nandi
93249db692 Use the sourceFile to determine the jsxNamespace at the location for organizingImports
Fixes #28827
2019-01-04 14:12:04 -08:00
Kerem
de4803658e Disable truncation for codefixes in signatureToMethodDeclaration. (#28188) 2018-12-13 11:21:36 -08:00
Nathan Shively-Sanders
594430f113 Infer from arrows from usage. (#28832)
* Infer from arrows from usage.

Previously only function expressions were, and only those with an easily
accessible name. Now any arrow function or function expression will
infer from usage.

* remove isApplicableFunctionForInference

*all* functions are applicable for inference now.
2018-12-04 09:03:08 -08:00
Nathan Shively-Sanders
ea8ccc2ce4 In JS, constructor functions infer from call+construct (#28353)
* constructor functions infer from call+construct

Also fix an incorrect combining of inferences for rest parameters: the
inferred types will be arrays in the body of the function and the
arguments from outside the function will be the element type.

* All functions infer from call+construct contexts
2018-11-16 09:51:07 -08:00
Nathan Shively-Sanders
6cee7c3080 Better nameless parameter implicit any error (#28554)
* Initial version, doesn't work for primitives yet.

Need to find out why.

* Primitives now work, plus improve error message

null and void don't even parse without parameter names so they are not
tested.

* Codefix: Add names to nameless parameters

* Improve error wording

* Add detail to error message
2018-11-15 12:45:39 -08:00
Nathan Shively-Sanders
7a7328a17f string|number inferences are low priority (#28381)
* string|number inferences are low priority

Also, refactor unifyFromContext to explicitly handle priorities

* string/number/strnum are not mutually exclusive

* Assert that high/low can't apply to same element
2018-11-15 08:32:28 -08:00
Daniel Rosenwasser
fe263708be Merge pull request #27019 from iliashkolyar/codefix_add_missing_new_operator
Codefix: add quick fix for missing 'new' operator
2018-11-14 16:53:25 -08:00
Wesley Wigham
11eee2b6ee Slightly improve missing property errors (#28298)
* Slightly improve missing property errors

* Add missing quote

* Fix jsx case

* Add related span

* Fix crash (why can declarations be undefined)

* Only skip top elaboration when no variant message is provided
2018-11-12 14:31:15 -08:00
Andy
fe1ba9bee3 Improvements to generateTypes (#28458)
* Improvements to generateTypes

* createProperty only if necessary
2018-11-12 11:50:48 -08:00
iliashkolyar
047b76f979 Merge branch 'master' into codefix_add_missing_new_operator 2018-11-10 01:45:11 +02:00
Daniel Rosenwasser
2585ccbe39 Merge pull request #28281 from ryanclarke/master
Add codefix for 'convert to unknown' diagnostic
2018-11-09 14:43:25 -08:00
Markus Wolf
499bed540b Better reference usage detection 2018-11-09 09:45:20 +01:00
Markus Wolf
6bd298b884 add test for remove modifier and parameter 2018-11-09 09:45:12 +01:00
Markus Wolf
13e85ac3a9 add more modifiers 2018-11-09 09:45:12 +01:00
Markus Wolf
de7faa1b7e Remove obsolte ts namespace 2018-11-09 09:45:11 +01:00
Markus Wolf
e6e71978df Correct codefix by removing private modifier
In case of private attribute and private constructor parameter with
assignment in the constructor body, the parameter is flagged as unused.
This is caused by the private modifier which is shadowed by the
explicity assignment in the body.
This commit updates the codefix to just remove the private modifier in
this cases.

Closes #24931
2018-11-09 09:45:11 +01:00
Ryan Clarke
ecb88f5a3e Fix comments on pull request 2018-11-07 21:43:02 -05:00
Daniel Rosenwasser
41d3f0ad57 Merge pull request #28071 from pesca/improveCodeFix
Improve code fix for missing members
2018-11-06 11:29:51 -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
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
Ryan Clarke
2dc3c8e49f Refactor convert to unknown codefix 2018-11-01 07:46:15 -04:00
Ryan Clarke
4598943ed3 Add codefix for 'convert to unknown' diagnostic
Codefix for diagnostic error 2352: "Conversion of type '{0}' to type '{1}' may be
a mistake because neither type sufficiently overlaps with the other. If this was
intentional, convert the expression to 'unknown' first."

Add codefix for both AsExpressions and TypeAssertions
2018-11-01 06:13:25 -04: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
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
672b0e3e16 Have flatMap return a ReadonlyArray by default (#28205) 2018-10-29 11:12:51 -07:00
iliashkolyar
21feb204f6 Merge branch 'master' into codefix_add_missing_new_operator 2018-10-28 23:27:16 +02:00
Andy
77d8e15905 Remove unused property ParameterInference#typeNode (#28115) 2018-10-26 14:44:49 -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
Andy
42740d66c2 Improve performance of suggestionDiagnostics for convertToAsyncFunction (#28089) 2018-10-24 21:23:47 -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
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
Philip Pesca
de318651f3 Improve code fix for missing members 2018-10-22 23:17:41 -07:00
Andy
dd764b318f importFixes: Skip alias when testing isTypeOnlySymbol (#27674) 2018-10-10 13:45:52 -07:00
Nathan Shively-Sanders
88d3c6fd5f inferFromUsage codefix now emits JSDoc in JS files (#27610)
* Now adding @type to variable declarations, at least

Probably everything else, but not as well.

* Improve @param output and add test

It's still bad, but at least it's not wrong.

* Add some js/inferFromUsage tests and fixes

Also, remove redundant is(Set|Get)Accessor functions.

* Fix @typedef refactor

* Emit JSDoc optional parameters

By surrounding the parameter name with brackets. It is super, super ugly
right now.

* Get rest of existing tests working

* Correct location of comments

* Handle @param blocks

1. Format multiple params nicely in a single-multiline block.
2. Generate only params that haven't already been documented. Existing
documentation is not touched.

* Re-add isGet/SetAccessor -- it is part of the API

* Move isSet/GetAccessor back to the original location

* Oh no I missed a newline and a space

* Switch to an object type

* A lot of cleanup

More to come, though. annotate is only called in
annotateVariableDeclaration where we don't know whether we're in JS or
not.

* Move and delegate to annotateJSDocParameters

* Address PR comments

* Lint: newline problems!!!!

* Switch another call to getNonformattedText

* Update baseline missed after merge
2018-10-09 10:38:46 -07:00
Nathan Shively-Sanders
ca94d8efd9 Infer from usage better import types (#27626)
* Use host to improve SymbolTracker implementation

* inferFromUsage: Provide a better moduleResolverHost

This produces better paths on import types.
2018-10-09 07:12:09 -07:00
iliashkolyar
6bd9b766b3 Code review - remove 'isCallExpression' check 2018-10-07 22:19:09 +03:00
Benjamin Lichtman
2ebd986d99 Always await expression of promise type in return position 2018-10-05 09:59:22 -07:00
Alessandro Vergani
bbf77538c4 Remove unneeded check from getTransformationBody
Remove unneeded `argName` check, because it always evaluates to `true` (the `false` case is handled just before the modified line)
2018-10-02 16:02:04 +02:00
Benjamin Lichtman
f356cd6c89 Insert async keyword as last modifier (#27491) 2018-10-01 17:43:17 -07:00
iliashkolyar
58b262e987 Merge branch 'master' into codefix_add_missing_new_operator 2018-09-30 13:27:36 +03:00
Dhruv Rajvanshi
bde81deed2 Issue #27301: Fixed crash when converting function to async (#27396) 2018-09-27 18:56:37 -07:00
Andy
0a97663843 Add 'prefixText' and 'suffixText' when renaming shorthand properties (#27356)
* Add 'prefixText' and 'suffixText' when renaming shorthand properties

* Make prefixText and suffixText missing instead of undefined

* Fix test
2018-09-26 16:58:01 -07:00
Andy
5f563c99f6 convertToAsyncFunction: Use ReadonlyArray / ReadonlyMap where possible (#27190) 2018-09-20 16:45:28 -07:00
iliashkolyar
35665d1036 Merge branch 'master' into codefix_add_missing_new_operator 2018-09-19 00:06:39 +03:00