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

Incorrect compile result of arrow function using implicit return (starts with optional chaining) and with line comments in between #41814

Closed
QuincyHe opened this issue Dec 4, 2020 · 2 comments · Fixed by #42026
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros High Priority

Comments

@QuincyHe
Copy link

QuincyHe commented Dec 4, 2020

TypeScript Version: Since v3.8.3

Search Terms:
Arrow function, implicit return, line comments, optional chaining
Code

// Target: ES5
// arrow function, implicit return on 2nd line that starts with optional chaining, and has a line comment in between the lines.
const test = (names: string[]) => 
    // testing 1 two
    names?.filter(x => x);

console.log(test(["a"]));

Expected behavior:
Something like:

"use strict";
var test = function (names) { // testing 1 two
return names === null || 
names === void 0 ? void 0 : 
names.filter(function (x) { return x; }); };
console.log(test(["a"]));

Actual behavior:

  1. the comment is being copied 3 times.
  2. the first line comment terminates the return, making this function always return undefined.
"use strict";
var test = function (names) { return 
// testing 1 two
names === null || 
// testing 1 two
names === void 0 ? void 0 : 
// testing 1 two
names.filter(function (x) { return x; }); };
console.log(test(["a"]));

Playground Link:
https://www.typescriptlang.org/play?target=1&ts=3.8.3#code/MYewdgzgLgBFCm0YF4YAowEMC2iBcM0ATgJZgDmA2gLoCUKAfDAFAxswD0HciUZ5MAIxwA7iFbssuCAH4AdADMSAGwRE0AD0YwNtANzNmoSCGXw5ykOTQJoaSgCJMDuvqA
Related Issues:

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Dec 4, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.2.0 milestone Dec 4, 2020
@RyanCavanaugh
Copy link
Member

@rbuckton correctness-affecting emit bug here

@rbuckton
Copy link
Member

// @showEmit
// @target: es5
const test = (names: string[]) => 
    // single-line comment
    names?.filter(x => x);

Workbench Repro

@typescript-bot typescript-bot added the Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros label Dec 18, 2020
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Dec 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros High Priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants