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

🤖 Pick PR #61244 (Ban old-style type assertions under...) into release-5.8 #61320

Merged
merged 1 commit into from
Mar 19, 2025
Merged
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
5 changes: 5 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
@@ -37311,6 +37311,11 @@ 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) {
const start = node.type.pos - "<".length;
const end = skipTrivia(file.text, node.type.end) + ">".length;
Comment on lines +37315 to +37316
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have just used skipTrivia(file.text, node.pos) and expression.pos

diagnostics.add(createFileDiagnostic(file, start, end - start, Diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled));
}
}
return checkAssertionWorker(node, checkMode);
}
70 changes: 69 additions & 1 deletion tests/baselines/reference/erasableSyntaxOnly.errors.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
error TS2318: Cannot find global type 'IterableIterator'.
commonjs.cts(1,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
commonjs.cts(2,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
index.ts(3,17): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
@@ -8,9 +9,22 @@ 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(72,6): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
index.ts(73,7): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
index.ts(74,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
index.ts(79,11): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
index.ts(81,12): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
index.ts(86,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
index.ts(89,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
index.ts(90,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
index.ts(94,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.


==== index.ts (8 errors) ====
!!! error TS2318: Cannot find global type 'IterableIterator'.
==== index.ts (20 errors) ====
class MyClassErr {
// No parameter properties
constructor(public foo: string) { }
@@ -92,6 +106,60 @@ 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>({}))();
~~~~~
!!! 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.


// return and yield ASI
function *gen() {
yield <any>
~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
1;
return <any>
~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
1;
}

// at the start of an ExpressionStatement if followed by an object literal; though I'm not sure why one would use it there
<unknown>{foo() {}}.foo();
~~~~~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.

// at the start of an ExpressionStatement if followed by function keyword
<unknown>function() {}();
~~~~~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
<unknown>function() {};
~~~~~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.

// at the start of an ExpressionStatement if followed by an anonymous class expression
// note that this exact syntax currently emits invalid JS (no parenthesis added like for function above)
<unknown>class {}
~~~~~~~~~
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.

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

import FineAlias = EnumInAmbientContext.B;
}

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

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


// return and yield ASI
function *gen() {
yield <any>
1;
return <any>
1;
}

// at the start of an ExpressionStatement if followed by an object literal; though I'm not sure why one would use it there
<unknown>{foo() {}}.foo();

// at the start of an ExpressionStatement if followed by function keyword
<unknown>function() {}();
<unknown>function() {};

// at the start of an ExpressionStatement if followed by an anonymous class expression
// note that this exact syntax currently emits invalid JS (no parenthesis added like for function above)
<unknown>class {}

//// [commonjs.cts]
import foo = require("./other.cjs");
@@ -82,6 +112,33 @@ export default foo;


//// [index.js]
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var MyClassErr = /** @class */ (function () {
// No parameter properties
function MyClassErr(foo) {
@@ -120,6 +177,37 @@ var MyClassOk = /** @class */ (function () {
}
return MyClassOk;
}());
// Not erasable
(function () { return ({}); })();
(function () { return ({}); })();
(function () { return ({}); })();
// Erasable
(function () { return ({}); })();
(function () { return ({}); })();
({});
// return and yield ASI
function gen() {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, 1];
case 1:
_a.sent();
return [2 /*return*/, 1];
}
});
}
// at the start of an ExpressionStatement if followed by an object literal; though I'm not sure why one would use it there
({ foo: function () { } }.foo());
// at the start of an ExpressionStatement if followed by function keyword
(function () { })();
(function () { });
// at the start of an ExpressionStatement if followed by an anonymous class expression
// note that this exact syntax currently emits invalid JS (no parenthesis added like for function above)
/** @class */ (function () {
function class_1() {
}
return class_1;
}());
//// [commonjs.cjs]
"use strict";
var foo = require("./other.cjs");
35 changes: 35 additions & 0 deletions tests/baselines/reference/erasableSyntaxOnly.symbols
Original file line number Diff line number Diff line change
@@ -118,6 +118,41 @@ declare namespace AmbientStuff {
>B : Symbol(FineAlias, Decl(index.ts, 58, 31))
}

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

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


// return and yield ASI
function *gen() {
>gen : Symbol(gen, Decl(index.ts, 73, 8))

yield <any>
1;
return <any>
1;
}

// at the start of an ExpressionStatement if followed by an object literal; though I'm not sure why one would use it there
<unknown>{foo() {}}.foo();
>{foo() {}}.foo : Symbol(foo, Decl(index.ts, 85, 10))
>foo : Symbol(foo, Decl(index.ts, 85, 10))
>foo : Symbol(foo, Decl(index.ts, 85, 10))

// at the start of an ExpressionStatement if followed by function keyword
<unknown>function() {}();
<unknown>function() {};

// at the start of an ExpressionStatement if followed by an anonymous class expression
// note that this exact syntax currently emits invalid JS (no parenthesis added like for function above)
<unknown>class {}

=== commonjs.cts ===
import foo = require("./other.cjs");
>foo : Symbol(foo, Decl(commonjs.cts, 0, 0))
135 changes: 135 additions & 0 deletions tests/baselines/reference/erasableSyntaxOnly.types
Original file line number Diff line number Diff line change
@@ -160,6 +160,141 @@ 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
> : ^^^
>{} : {}
> : ^^


// return and yield ASI
function *gen() {
>gen : () => {}
> : ^^^^^^^^

yield <any>
>yield <any> 1 : any
> : ^^^
><any> 1 : any
> : ^^^

1;
>1 : 1
> : ^

return <any>
><any> 1 : any
> : ^^^

1;
>1 : 1
> : ^
}

// at the start of an ExpressionStatement if followed by an object literal; though I'm not sure why one would use it there
<unknown>{foo() {}}.foo();
><unknown>{foo() {}}.foo() : unknown
> : ^^^^^^^
>{foo() {}}.foo() : void
> : ^^^^
>{foo() {}}.foo : () => void
> : ^^^^^^^^^^
>{foo() {}} : { foo(): void; }
> : ^^^^^^^^^^^^^^^^
>foo : () => void
> : ^^^^^^^^^^
>foo : () => void
> : ^^^^^^^^^^

// at the start of an ExpressionStatement if followed by function keyword
<unknown>function() {}();
><unknown>function() {}() : unknown
> : ^^^^^^^
>function() {}() : void
> : ^^^^
>function() {} : () => void
> : ^^^^^^^^^^

<unknown>function() {};
><unknown>function() {} : unknown
> : ^^^^^^^
>function() {} : () => void
> : ^^^^^^^^^^

// at the start of an ExpressionStatement if followed by an anonymous class expression
// note that this exact syntax currently emits invalid JS (no parenthesis added like for function above)
<unknown>class {}
><unknown>class {} : unknown
> : ^^^^^^^
>class {} : typeof (Anonymous class)
> : ^^^^^^^^^^^^^^^^^^^^^^^^

=== commonjs.cts ===
import foo = require("./other.cjs");
>foo : () => void
Loading