Skip to content

BREAKING CHANGE: fix: detect the premature usage of @global variant #2624

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

Closed
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
8 changes: 8 additions & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7339,6 +7339,14 @@ export class Compiler extends DiagnosticEmitter {
}
case ElementKind.Global: {
let global = <Global>target;
if (!global.hasDecorator(DecoratorFlags.Lazy) && global.hasDecorator(DecoratorFlags.Global) && !global.is(CommonFlags.Compiled)) {
this.error(
DiagnosticCode.Variable_0_used_before_its_declaration,
expression.range,
global.internalName
);
return module.unreachable();
}
Comment on lines +7342 to +7349
Copy link
Member

Choose a reason for hiding this comment

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

Does this cover all possible cases? For example, if the global is not read (a = t1), but assigned (t1 = something), it looks like this branch alone would not suffice.

if (!this.compileGlobal(global)) { // reports; not yet compiled if a static field
return module.unreachable();
}
Expand Down
6 changes: 6 additions & 0 deletions tests/compiler/issues/2622/2622-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"asc_flags": [],
"stderr": [
"TS2448: Variable 'issues/2622/node_modules_1/t1' used before its declaration."
]
}
3 changes: 3 additions & 0 deletions tests/compiler/issues/2622/2622-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let a = t1;

import {} from "./node_modules_1";
Loading