diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7c5958106334a..fbf8d4e342dcc 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -37312,6 +37312,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; + diagnostics.add(createFileDiagnostic(file, start, end - start, Diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)); + } } return checkAssertionWorker(node, checkMode); } diff --git a/tests/baselines/reference/erasableSyntaxOnly.errors.txt b/tests/baselines/reference/erasableSyntaxOnly.errors.txt index 2005ef65de8e7..523da93fd9d39 100644 --- a/tests/baselines/reference/erasableSyntaxOnly.errors.txt +++ b/tests/baselines/reference/erasableSyntaxOnly.errors.txt @@ -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 + (()=>{})(); + ~~~~~ +!!! 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 + (()=>({}))(); + ~~~~~ +!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled. + (()=>({}))(); + ~~~~~ +!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled. + {}; + ~~~~~ +!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled. + + + // return and yield ASI + function *gen() { + yield + ~~~~~ +!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled. + 1; + return + ~~~~~ +!!! 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 + {foo() {}}.foo(); + ~~~~~~~~~ +!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled. + + // at the start of an ExpressionStatement if followed by function keyword + function() {}(); + ~~~~~~~~~ +!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled. + 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) + class {} + ~~~~~~~~~ +!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled. + ==== commonjs.cts (2 errors) ==== import foo = require("./other.cjs"); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/baselines/reference/erasableSyntaxOnly.js b/tests/baselines/reference/erasableSyntaxOnly.js index 9edf749d7b038..beb8262097152 100644 --- a/tests/baselines/reference/erasableSyntaxOnly.js +++ b/tests/baselines/reference/erasableSyntaxOnly.js @@ -65,6 +65,36 @@ declare namespace AmbientStuff { import FineAlias = EnumInAmbientContext.B; } + +// Not erasable +(()=>{})(); +(()=>< any >{})(); +(()=> < any > {})(); + +// Erasable +(()=>({}))(); +(()=>({}))(); +{}; + + +// return and yield ASI +function *gen() { + yield + 1; + 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() {}}.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 {} //// [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"); diff --git a/tests/baselines/reference/erasableSyntaxOnly.symbols b/tests/baselines/reference/erasableSyntaxOnly.symbols index 8e057dc990217..659c5df9d59fc 100644 --- a/tests/baselines/reference/erasableSyntaxOnly.symbols +++ b/tests/baselines/reference/erasableSyntaxOnly.symbols @@ -118,6 +118,41 @@ declare namespace AmbientStuff { >B : Symbol(FineAlias, Decl(index.ts, 58, 31)) } +// Not erasable +(()=>{})(); +(()=>< any >{})(); +(()=> < any > {})(); + +// Erasable +(()=>({}))(); +(()=>({}))(); +{}; + + +// return and yield ASI +function *gen() { +>gen : Symbol(gen, Decl(index.ts, 73, 8)) + + yield + 1; + 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() {}}.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 +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 {} + === commonjs.cts === import foo = require("./other.cjs"); >foo : Symbol(foo, Decl(commonjs.cts, 0, 0)) diff --git a/tests/baselines/reference/erasableSyntaxOnly.types b/tests/baselines/reference/erasableSyntaxOnly.types index 4d4ddaea49701..b252f35f322e3 100644 --- a/tests/baselines/reference/erasableSyntaxOnly.types +++ b/tests/baselines/reference/erasableSyntaxOnly.types @@ -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 +> : ^^^ +>{} : {} +> : ^^ + +// Erasable +(()=>({}))(); +>(()=>({}))() : any +> : ^^^ +>(()=>({})) : () => any +> : ^^^^^^ +>()=>({}) : () => any +> : ^^^^^^ +>({}) : any +> : ^^^ +>({}) : {} +> : ^^ +>{} : {} +> : ^^ + +(()=>({}))(); +>(()=>({}))() : any +> : ^^^ +>(()=>({})) : () => any +> : ^^^^^^^^^ +>()=>({}) : () => any +> : ^^^^^^^^^ +>({}) : any +> : ^^^ +>{} : any +> : ^^^ +>{} : {} +> : ^^ + +{}; +>{} : any +> : ^^^ +>{} : {} +> : ^^ + + +// return and yield ASI +function *gen() { +>gen : () => {} +> : ^^^^^^^^ + + yield +>yield 1 : any +> : ^^^ +> 1 : any +> : ^^^ + + 1; +>1 : 1 +> : ^ + + return +> 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 +{foo() {}}.foo(); +>{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 +function() {}(); +>function() {}() : unknown +> : ^^^^^^^ +>function() {}() : void +> : ^^^^ +>function() {} : () => void +> : ^^^^^^^^^^ + +function() {}; +>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) +class {} +>class {} : unknown +> : ^^^^^^^ +>class {} : typeof (Anonymous class) +> : ^^^^^^^^^^^^^^^^^^^^^^^^ + === commonjs.cts === import foo = require("./other.cjs"); >foo : () => void diff --git a/tests/cases/compiler/erasableSyntaxOnly.ts b/tests/cases/compiler/erasableSyntaxOnly.ts index d2ff6d72ce8f3..1df8b3116442c 100644 --- a/tests/cases/compiler/erasableSyntaxOnly.ts +++ b/tests/cases/compiler/erasableSyntaxOnly.ts @@ -66,6 +66,36 @@ declare namespace AmbientStuff { import FineAlias = EnumInAmbientContext.B; } +// Not erasable +(()=>{})(); +(()=>< any >{})(); +(()=> < any > {})(); + +// Erasable +(()=>({}))(); +(()=>({}))(); +{}; + + +// return and yield ASI +function *gen() { + yield + 1; + 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() {}}.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 {} + // @filename: commonjs.cts import foo = require("./other.cjs"); export = foo;