TypeScript/tests/baselines/reference/spellingUncheckedJS.symbols
Nathan Shively-Sanders e53f19f8f2
Issue "Cannot find name did-you-mean" errors as suggestions in plain JS (#44271)
* Always issue cannot find name did-you-mean error

This PR issues "cannot find ${name}, did you mean ${name}" errors for
identifiers and propery access expressions in JS files *without*
`// @ts-check` and without `// @ts-nocheck`. This brings some benefits of
Typescript's binder to all Javascript users, even those who haven't
opted into Typescript checking.

```js
export var inModule = 1
inmodule.toFixed() // errors on exports

function f() {
    var locals = 2
    locale.toFixed() // errors on locals
}
var object = {
    spaaace: 3
}
object.spaaaace // error on read
object.spaace = 2 // error on write
object.fresh = 12 // OK, no spelling correction to offer
```

To disable the errors, add `// @ts-nocheck` to the file. To get the
normal checkJs experience, add `// @ts-check`.

== Why This Works ==

In a word: precision. This change has low recall — it misses lots
of correct errors that would be nice to show — but it has high
precision: almost all the errors it shows are correct. And they come
with a suggested correction.

Here are the ingredients:

1. For unchecked JS files, the compiler suppresses all errors except
two did-you-mean name resolution errors.
2. Did-you-mean spelling correction is already tuned for high
precision/low recall, and doesn't show many bogus errors even in JS.
3. For identifiers, the error is suppressed for suggestions from global files.
These are often DOM feature detection, for example.
4. For property accesses, the error is suppressed for suggestions from
other files, for the same reason.
5. For property accesses, the error is suppressed for `this` property
accesses because the compiler doesn't understand JS constructor
functions well enough.
In particular, it doesn't understand any inheritance patterns.

== Work Remaining ==

1. Code cleanup.
2. Fix a couple of failures in existing tests.
3. Suppress errors on property access suggestions from large objects.
4. Combine (3) and (4) above to suppress errors on suggestions from other, global files.
5. A little more testing on random files to make sure that precision
is good there too.
6. Have people from the regular Code editor meeting test the code and
suggest ideas.

* all (most?) tests pass

* NOW they all pass

* add tonnes of semi-colons

* restore this.x check+add a test case

* make ts-ignore/no-check codefix work in unchecked js

* Issues errors only in the language service

* add a few more tests

* fix incorrect parentheses

* More cleanup in program.ts

* Improve readability of isExcludedJSError

* make diff in program.ts smaller via closure

* Switch unchecked JS did-you-mean to suggestion

Instead of selectively letting errors through.

* undo more missed changes

* disallow ignoring suggestions

* Issue different messages for plain JS than others

Straw text for the messages, I just changed the modals to avoid name
collisions.
2021-06-15 08:54:08 -07:00

96 lines
3 KiB
Plaintext

=== tests/cases/conformance/salsa/spellingUncheckedJS.js ===
export var inModule = 1
>inModule : Symbol(inModule, Decl(spellingUncheckedJS.js, 0, 10))
inmodule.toFixed()
function f() {
>f : Symbol(f, Decl(spellingUncheckedJS.js, 1, 18))
var locals = 2 + true
>locals : Symbol(locals, Decl(spellingUncheckedJS.js, 4, 7))
locale.toFixed()
// @ts-expect-error
localf.toExponential()
// @ts-expect-error
"this is fine"
}
class Classe {
>Classe : Symbol(Classe, Decl(spellingUncheckedJS.js, 10, 1))
non = 'oui'
>non : Symbol(Classe.non, Decl(spellingUncheckedJS.js, 11, 14))
methode() {
>methode : Symbol(Classe.methode, Decl(spellingUncheckedJS.js, 12, 15))
// no error on 'this' references
return this.none
>this : Symbol(Classe, Decl(spellingUncheckedJS.js, 10, 1))
}
}
class Derivee extends Classe {
>Derivee : Symbol(Derivee, Decl(spellingUncheckedJS.js, 17, 1))
>Classe : Symbol(Classe, Decl(spellingUncheckedJS.js, 10, 1))
methode() {
>methode : Symbol(Derivee.methode, Decl(spellingUncheckedJS.js, 18, 30))
// no error on 'super' references
return super.none
>super : Symbol(Classe, Decl(spellingUncheckedJS.js, 10, 1))
}
}
var object = {
>object : Symbol(object, Decl(spellingUncheckedJS.js, 26, 3), Decl(spellingUncheckedJS.js, 29, 15), Decl(spellingUncheckedJS.js, 30, 18))
spaaace: 3
>spaaace : Symbol(spaaace, Decl(spellingUncheckedJS.js, 26, 14))
}
object.spaaaace // error on read
>object : Symbol(object, Decl(spellingUncheckedJS.js, 26, 3), Decl(spellingUncheckedJS.js, 29, 15), Decl(spellingUncheckedJS.js, 30, 18))
object.spaace = 12 // error on write
>object : Symbol(object, Decl(spellingUncheckedJS.js, 26, 3), Decl(spellingUncheckedJS.js, 29, 15), Decl(spellingUncheckedJS.js, 30, 18))
object.fresh = 12 // OK
>object : Symbol(object, Decl(spellingUncheckedJS.js, 26, 3), Decl(spellingUncheckedJS.js, 29, 15), Decl(spellingUncheckedJS.js, 30, 18))
other.puuuce // OK, from another file
>other : Symbol(other, Decl(other.js, 3, 3))
new Date().getGMTDate() // OK, from another file
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --))
// No suggestions for globals from other files
const atoc = setIntegral(() => console.log('ok'), 500)
>atoc : Symbol(atoc, Decl(spellingUncheckedJS.js, 36, 5))
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
AudioBuffin // etc
Jimmy
>Jimmy : Symbol(Jimmy, Decl(other.js, 0, 3))
Jon
=== tests/cases/conformance/salsa/other.js ===
var Jimmy = 1
>Jimmy : Symbol(Jimmy, Decl(other.js, 0, 3))
var John = 2
>John : Symbol(John, Decl(other.js, 1, 3))
Jon // error, it's from the same file
var other = {
>other : Symbol(other, Decl(other.js, 3, 3))
puuce: 4
>puuce : Symbol(puuce, Decl(other.js, 3, 13))
}