Skip to content

[experimental] Force strict binding #61888

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
10 changes: 2 additions & 8 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
getSourceFileOfNode,
getSourceTextOfNodeFromSourceFile,
getSpanOfTokenAtPosition,
getStrictOptionValue,

Check warning on line 114 in src/compiler/binder.ts

View workflow job for this annotation

GitHub Actions / lint

'getStrictOptionValue' is defined but never used. Allowed unused vars must match /^(_+$|_[^_])/u
getSymbolNameForPrivateIdentifier,
getTextOfIdentifierOrLiteral,
getThisContainer,
Expand Down Expand Up @@ -630,14 +630,8 @@
emitFlags = NodeFlags.None;
}

function bindInStrictMode(file: SourceFile, opts: CompilerOptions): boolean {
if (getStrictOptionValue(opts, "alwaysStrict") && !file.isDeclarationFile) {
// bind in strict mode source files with alwaysStrict option
return true;
}
else {
return !!file.externalModuleIndicator;
}
function bindInStrictMode(_file: SourceFile, _opts: CompilerOptions): boolean {
return true;
}

function createSymbol(flags: SymbolFlags, name: __String): Symbol {
Expand Down
15 changes: 15 additions & 0 deletions tests/baselines/reference/ES5For-of19.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ES5For-of19.ts(3,14): error TS1250: Function declarations are not allowed inside blocks in strict mode when targeting 'ES5'.


==== ES5For-of19.ts (1 errors) ====
for (let v of []) {
v;
function foo() {
~~~
!!! error TS1250: Function declarations are not allowed inside blocks in strict mode when targeting 'ES5'.
for (const v of []) {
v;
}
}
}

4 changes: 4 additions & 0 deletions tests/baselines/reference/ES5For-of19.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@
=== ES5For-of19.ts ===
for (let v of []) {
>v : any
> : ^^^
>[] : undefined[]
> : ^^^^^^^^^^^

v;
>v : any
> : ^^^

function foo() {
>foo : () => void
> : ^^^^^^^^^^

for (const v of []) {
>v : any
> : ^^^
>[] : undefined[]
> : ^^^^^^^^^^^

v;
>v : any
> : ^^^
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FunctionDeclaration11_es6.ts(1,12): error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.


==== FunctionDeclaration11_es6.ts (1 errors) ====
function * yield() {
~~~~~
!!! error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FunctionDeclaration12_es6.ts(1,20): error TS1359: Identifier expected. 'yield' is a reserved word that cannot be used here.
FunctionDeclaration12_es6.ts(1,20): error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.


==== FunctionDeclaration12_es6.ts (1 errors) ====
var v = function * yield() { }
~~~~~
!!! error TS1359: Identifier expected. 'yield' is a reserved word that cannot be used here.
!!! error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
FunctionDeclaration13_es6.ts(3,11): error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
FunctionDeclaration13_es6.ts(3,11): error TS2304: Cannot find name 'yield'.


==== FunctionDeclaration13_es6.ts (1 errors) ====
==== FunctionDeclaration13_es6.ts (2 errors) ====
function * foo() {
// Legal to use 'yield' in a type context.
var v: yield;
~~~~~
!!! error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
~~~~~
!!! error TS2304: Cannot find name 'yield'.
}

8 changes: 8 additions & 0 deletions tests/baselines/reference/FunctionDeclaration2_es6.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FunctionDeclaration2_es6.ts(1,12): error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.


==== FunctionDeclaration2_es6.ts (1 errors) ====
function f(yield) {
~~~~~
!!! error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
}
1 change: 1 addition & 0 deletions tests/baselines/reference/FunctionDeclaration2_es6.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ function f(yield) {
>f : (yield: any) => void
> : ^ ^^^^^^^^^^^^^^
>yield : any
> : ^^^
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
FunctionDeclaration3_es6.ts(1,12): error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
FunctionDeclaration3_es6.ts(1,20): error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
FunctionDeclaration3_es6.ts(1,20): error TS2372: Parameter 'yield' cannot reference itself.


==== FunctionDeclaration3_es6.ts (1 errors) ====
==== FunctionDeclaration3_es6.ts (3 errors) ====
function f(yield = yield) {
~~~~~
!!! error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
~~~~~
!!! error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
~~~~~
!!! error TS2372: Parameter 'yield' cannot reference itself.
}
8 changes: 8 additions & 0 deletions tests/baselines/reference/FunctionDeclaration4_es6.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FunctionDeclaration4_es6.ts(1,10): error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.


==== FunctionDeclaration4_es6.ts (1 errors) ====
function yield() {
~~~~~
!!! error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/FunctionDeclaration5_es6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FunctionDeclaration5_es6.ts(1,14): error TS1359: Identifier expected. 'yield' is a reserved word that cannot be used here.
FunctionDeclaration5_es6.ts(1,14): error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.


==== FunctionDeclaration5_es6.ts (1 errors) ====
function*foo(yield) {
~~~~~
!!! error TS1359: Identifier expected. 'yield' is a reserved word that cannot be used here.
!!! error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
}
8 changes: 7 additions & 1 deletion tests/baselines/reference/FunctionDeclaration6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
FunctionDeclaration6.ts(2,14): error TS1250: Function declarations are not allowed inside blocks in strict mode when targeting 'ES5'.
FunctionDeclaration6.ts(3,14): error TS1250: Function declarations are not allowed inside blocks in strict mode when targeting 'ES5'.
FunctionDeclaration6.ts(3,14): error TS2389: Function implementation name must be 'foo'.


==== FunctionDeclaration6.ts (1 errors) ====
==== FunctionDeclaration6.ts (3 errors) ====
{
function foo();
~~~
!!! error TS1250: Function declarations are not allowed inside blocks in strict mode when targeting 'ES5'.
function bar() { }
~~~
!!! error TS1250: Function declarations are not allowed inside blocks in strict mode when targeting 'ES5'.
~~~
!!! error TS2389: Function implementation name must be 'foo'.
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
FunctionDeclaration8_es6.ts(1,12): error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
FunctionDeclaration8_es6.ts(1,12): error TS2304: Cannot find name 'yield'.
FunctionDeclaration8_es6.ts(1,20): error TS2304: Cannot find name 'foo'.


==== FunctionDeclaration8_es6.ts (2 errors) ====
==== FunctionDeclaration8_es6.ts (3 errors) ====
var v = { [yield]: foo }
~~~~~
!!! error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
~~~~~
!!! error TS2304: Cannot find name 'yield'.
~~~
!!! error TS2304: Cannot find name 'foo'.
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
NonInitializedExportInInternalModule.ts(2,8): error TS1123: Variable declaration list cannot be empty.
NonInitializedExportInInternalModule.ts(3,5): error TS1212: Identifier expected. 'let' is a reserved word in strict mode.
NonInitializedExportInInternalModule.ts(3,5): error TS2304: Cannot find name 'let'.
NonInitializedExportInInternalModule.ts(4,10): error TS1123: Variable declaration list cannot be empty.


==== NonInitializedExportInInternalModule.ts (3 errors) ====
==== NonInitializedExportInInternalModule.ts (4 errors) ====
module Inner {
var;

!!! error TS1123: Variable declaration list cannot be empty.
let;
~~~
!!! error TS1212: Identifier expected. 'let' is a reserved word in strict mode.
~~~
!!! error TS2304: Cannot find name 'let'.
const;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
VariableDeclaration6_es6.ts(1,1): error TS1212: Identifier expected. 'let' is a reserved word in strict mode.
VariableDeclaration6_es6.ts(1,1): error TS2304: Cannot find name 'let'.


==== VariableDeclaration6_es6.ts (1 errors) ====
==== VariableDeclaration6_es6.ts (2 errors) ====
let
~~~
!!! error TS1212: Identifier expected. 'let' is a reserved word in strict mode.
~~~
!!! error TS2304: Cannot find name 'let'.
5 changes: 4 additions & 1 deletion tests/baselines/reference/YieldExpression1_es6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
YieldExpression1_es6.ts(1,1): error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
YieldExpression1_es6.ts(1,1): error TS2304: Cannot find name 'yield'.


==== YieldExpression1_es6.ts (1 errors) ====
==== YieldExpression1_es6.ts (2 errors) ====
yield;
~~~~~
!!! error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
~~~~~
!!! error TS2304: Cannot find name 'yield'.
5 changes: 4 additions & 1 deletion tests/baselines/reference/YieldExpression8_es6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
YieldExpression8_es6.ts(1,1): error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
YieldExpression8_es6.ts(1,1): error TS2304: Cannot find name 'yield'.


==== YieldExpression8_es6.ts (1 errors) ====
==== YieldExpression8_es6.ts (2 errors) ====
yield(foo);
~~~~~
!!! error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
~~~~~
!!! error TS2304: Cannot find name 'yield'.
function* foo() {
yield(foo);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
YieldStarExpression1_es6.ts(1,1): error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
YieldStarExpression1_es6.ts(1,1): error TS2304: Cannot find name 'yield'.
YieldStarExpression1_es6.ts(1,9): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.


==== YieldStarExpression1_es6.ts (2 errors) ====
==== YieldStarExpression1_es6.ts (3 errors) ====
yield * [];
~~~~~
!!! error TS1212: Identifier expected. 'yield' is a reserved word in strict mode.
~~~~~
!!! error TS2304: Cannot find name 'yield'.
~~
!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
5 changes: 4 additions & 1 deletion tests/baselines/reference/ambientWithStatements.errors.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
ambientWithStatements.ts(2,5): error TS1036: Statements are not allowed in ambient contexts.
ambientWithStatements.ts(3,5): error TS1104: A 'continue' statement can only be used within an enclosing iteration statement.
ambientWithStatements.ts(11,5): error TS1108: A 'return' statement can only be used within a function body.
ambientWithStatements.ts(25,5): error TS1101: 'with' statements are not allowed in strict mode.
ambientWithStatements.ts(25,5): error TS2410: The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.


==== ambientWithStatements.ts (4 errors) ====
==== ambientWithStatements.ts (5 errors) ====
declare module M {
break;
~~~~~
Expand Down Expand Up @@ -36,6 +37,8 @@ ambientWithStatements.ts(25,5): error TS2410: The 'with' statement is not suppor
finally {
}
with (x) {
~~~~
!!! error TS1101: 'with' statements are not allowed in strict mode.
~~~~~~~~
!!! error TS2410: The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
argumentsBindsToFunctionScopeArgumentList.ts(1,5): error TS1100: Invalid use of 'arguments' in strict mode.
argumentsBindsToFunctionScopeArgumentList.ts(3,5): error TS1100: Invalid use of 'arguments' in strict mode.
argumentsBindsToFunctionScopeArgumentList.ts(3,5): error TS2322: Type 'number' is not assignable to type 'IArguments'.


==== argumentsBindsToFunctionScopeArgumentList.ts (1 errors) ====
==== argumentsBindsToFunctionScopeArgumentList.ts (3 errors) ====
var arguments = 10;
~~~~~~~~~
!!! error TS1100: Invalid use of 'arguments' in strict mode.
function foo(a) {
arguments = 10; /// This shouldnt be of type number and result in error.
~~~~~~~~~
!!! error TS1100: Invalid use of 'arguments' in strict mode.
~~~~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'IArguments'.
}
31 changes: 31 additions & 0 deletions tests/baselines/reference/argumentsReferenceInConstructor5_Js.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,34 @@ declare class A {
*/
bar: object;
}


//// [DtsFileErrors]


/a.d.ts(2,9): error TS1100: Invalid use of 'arguments' in strict mode.


==== /a.d.ts (1 errors) ====
declare namespace bar {
let arguments: {};
~~~~~~~~~
!!! error TS1100: Invalid use of 'arguments' in strict mode.
}
declare class A {
/**
* Constructor
*
* @param {object} [foo={}]
*/
constructor(foo?: object);
/**
* @type object
*/
foo: object;
/**
* @type object
*/
bar: object;
}

29 changes: 29 additions & 0 deletions tests/baselines/reference/argumentsReferenceInMethod5_Js.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,32 @@ declare class A {
*/
bar: object;
}


//// [DtsFileErrors]


/a.d.ts(2,9): error TS1100: Invalid use of 'arguments' in strict mode.


==== /a.d.ts (1 errors) ====
declare namespace bar {
let arguments: {};
~~~~~~~~~
!!! error TS1100: Invalid use of 'arguments' in strict mode.
}
declare class A {
/**
* @param {object} [foo={}]
*/
m(foo?: object): void;
/**
* @type object
*/
foo: object;
/**
* @type object
*/
bar: object;
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/a.js(16,9): error TS18004: No value exists in scope for the shorthand property 'arguments'. Either declare one or provide an initializer.
/a.js(21,11): error TS1100: Invalid use of 'arguments' in strict mode.


==== /a.js (1 errors) ====
==== /a.js (2 errors) ====
const a = () => {
return {
arguments: [],
Expand All @@ -25,6 +26,8 @@

const d = () => {
const arguments = undefined;
~~~~~~~~~
!!! error TS1100: Invalid use of 'arguments' in strict mode.
return {
arguments,
};
Expand Down
Loading
Loading