Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing syntax error for new A?.b() #48653

Closed
nicolo-ribaudo opened this issue Apr 12, 2022 · 3 comments Β· Fixed by #48656
Closed

Missing syntax error for new A?.b() #48653

nicolo-ribaudo opened this issue Apr 12, 2022 · 3 comments Β· Fixed by #48656
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@nicolo-ribaudo
Copy link

Bug Report

πŸ”Ž Search Terms

syntax error optional chain new arguments parentheses

πŸ•— Version & Regression Information

Only tested with 4.7 Nightly in the playground

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

class A {
    b() {}
}

new A?.b()
Output
"use strict";
var _a;
class A {
    b() { }
}
(_a = new A) === null || _a === void 0 ? void 0 : _a.b();
Compiler Options
{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "target": "ES2017",
    "jsx": "react",
    "module": "ESNext",
    "moduleResolution": "node"
  }
}

Playground Link: Provided

πŸ™ Actual behavior

new A?.b() is parsed as if it was new A()?.b()

πŸ™‚ Expected behavior

It should be a syntax error, because it's not valid JavaScript syntax.

@fatcerberus
Copy link

fatcerberus commented Apr 12, 2022

new A()?.b() is still invalid JS (not true, see comments below). What's interesting is that when targeting esnext, the code is emitted as-is (and is a runtime error), but downlevel emit is doing the equivalent of (new A)?.b() instead.

@nicolo-ribaudo
Copy link
Author

new A()?.b() is valid JS (or at least, it's valid in Node.js and Firefox).

@fatcerberus
Copy link

Ah, you're right, I thought I had tried it and gotten an "invalid optional chain" error. But yeah, really weird because the esnext emit preserves the runtime SyntaxError while the downlevel emit changes the meaning of the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants