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

Ban old-style type assertions as arrow function bodies under erasableSyntaxOnly #61244

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37312,6 +37312,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (file && fileExtensionIsOneOf(file.fileName, [Extension.Cts, Extension.Mts])) {
grammarErrorOnNode(node, Diagnostics.This_syntax_is_reserved_in_files_with_the_mts_or_cts_extension_Use_an_as_expression_instead);
}
if (
compilerOptions.erasableSyntaxOnly
&& node.expression.kind === SyntaxKind.ObjectLiteralExpression
&& node.parent.kind === SyntaxKind.ArrowFunction && (node.parent as ArrowFunction).body === node
) {
const start = node.type.pos - "<".length;
const end = skipTrivia(file.text, node.type.end) + ">".length;
diagnostics.add(createFileDiagnostic(file, start, end - start, Diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled));
}
}
return checkAssertionWorker(node, checkMode);
}
Expand Down
21 changes: 20 additions & 1 deletion tests/baselines/reference/erasableSyntaxOnly.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ index.ts(17,15): error TS1294: This syntax is not allowed when 'erasableSyntaxOn
index.ts(22,6): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
index.ts(26,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
index.ts(28,12): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
index.ts(67,6): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
index.ts(68,6): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
index.ts(69,7): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.


==== index.ts (8 errors) ====
==== index.ts (11 errors) ====
class MyClassErr {
// No parameter properties
constructor(public foo: string) { }
Expand Down Expand Up @@ -92,6 +95,22 @@ index.ts(28,12): error TS1294: This syntax is not allowed when 'erasableSyntaxOn
import FineAlias = EnumInAmbientContext.B;
}

// Not erasable
(()=><any>{})();
~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
(()=>< any >{})();
~~~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
(()=> < any > {})();
~~~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.

// Erasable
(()=><any>({}))();
(()=>(<any>{}))();
<any>{};

==== commonjs.cts (2 errors) ====
import foo = require("./other.cjs");
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
18 changes: 18 additions & 0 deletions tests/baselines/reference/erasableSyntaxOnly.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ declare namespace AmbientStuff {

import FineAlias = EnumInAmbientContext.B;
}

// Not erasable
(()=><any>{})();
(()=>< any >{})();
(()=> < any > {})();

// Erasable
(()=><any>({}))();
(()=>(<any>{}))();
<any>{};

//// [commonjs.cts]
import foo = require("./other.cjs");
Expand Down Expand Up @@ -120,6 +130,14 @@ var MyClassOk = /** @class */ (function () {
}
return MyClassOk;
}());
// Not erasable
(function () { return ({}); })();
(function () { return ({}); })();
(function () { return ({}); })();
// Erasable
(function () { return ({}); })();
(function () { return ({}); })();
({});
//// [commonjs.cjs]
"use strict";
var foo = require("./other.cjs");
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/erasableSyntaxOnly.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ declare namespace AmbientStuff {
>B : Symbol(FineAlias, Decl(index.ts, 58, 31))
}

// Not erasable
(()=><any>{})();
(()=>< any >{})();
(()=> < any > {})();

// Erasable
(()=><any>({}))();
(()=>(<any>{}))();
<any>{};

=== commonjs.cts ===
import foo = require("./other.cjs");
>foo : Symbol(foo, Decl(commonjs.cts, 0, 0))
Expand Down
72 changes: 72 additions & 0 deletions tests/baselines/reference/erasableSyntaxOnly.types
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,78 @@ declare namespace AmbientStuff {
> : ^^^^^^^^^^^^^^^^^^^^^^
}

// Not erasable
(()=><any>{})();
>(()=><any>{})() : any
> : ^^^
>(()=><any>{}) : () => any
> : ^^^^^^
>()=><any>{} : () => any
> : ^^^^^^
><any>{} : any
> : ^^^
>{} : {}
> : ^^

(()=>< any >{})();
>(()=>< any >{})() : any
> : ^^^
>(()=>< any >{}) : () => any
> : ^^^^^^
>()=>< any >{} : () => any
> : ^^^^^^
>< any >{} : any
> : ^^^
>{} : {}
> : ^^

(()=> < any > {})();
>(()=> < any > {})() : any
> : ^^^
>(()=> < any > {}) : () => any
> : ^^^^^^
>()=> < any > {} : () => any
> : ^^^^^^
>< any > {} : any
> : ^^^
>{} : {}
> : ^^

// Erasable
(()=><any>({}))();
>(()=><any>({}))() : any
> : ^^^
>(()=><any>({})) : () => any
> : ^^^^^^
>()=><any>({}) : () => any
> : ^^^^^^
><any>({}) : any
> : ^^^
>({}) : {}
> : ^^
>{} : {}
> : ^^

(()=>(<any>{}))();
>(()=>(<any>{}))() : any
> : ^^^
>(()=>(<any>{})) : () => any
> : ^^^^^^^^^
>()=>(<any>{}) : () => any
> : ^^^^^^^^^
>(<any>{}) : any
> : ^^^
><any>{} : any
> : ^^^
>{} : {}
> : ^^

<any>{};
><any>{} : any
> : ^^^
>{} : {}
> : ^^

=== commonjs.cts ===
import foo = require("./other.cjs");
>foo : () => void
Expand Down
10 changes: 10 additions & 0 deletions tests/cases/compiler/erasableSyntaxOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ declare namespace AmbientStuff {
import FineAlias = EnumInAmbientContext.B;
}

// Not erasable
(()=><any>{})();
(()=>< any >{})();
(()=> < any > {})();

// Erasable
(()=><any>({}))();
(()=>(<any>{}))();
<any>{};

// @filename: commonjs.cts
import foo = require("./other.cjs");
export = foo;
Expand Down