-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix hasShadowedGlobals check to properly ignore top-level declarations (
#367) We'll want top-level declaration detection for #228 anyway, and hasShadowedGlobals was buggy because it was treating export declarations as shadowing themselves, so this adds some bookkeeping so we know when a declaration is top-level or not. This doesn't affect correctness, it was just an optimization that wasn't kicking in when it was supposed to. Tracking scope depth throughout the parser is a little scary and adds another field to the state, so there may be a way to improve it in the future, but it certainly should work for now.
- Loading branch information
1 parent
e476734
commit b7f3650
Showing
10 changed files
with
138 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import * as assert from "assert"; | ||
import CJSImportProcessor from "../src/CJSImportProcessor"; | ||
import {hasShadowedGlobals} from "../src/identifyShadowedGlobals"; | ||
import NameManager from "../src/NameManager"; | ||
import {parse} from "../src/parser"; | ||
import TokenProcessor from "../src/TokenProcessor"; | ||
|
||
function assertHasShadowedGlobals(code: string, expected: boolean): void { | ||
const file = parse(code, false, false, false); | ||
const tokenProcessor = new TokenProcessor(code, file.tokens, false); | ||
const nameManager = new NameManager(tokenProcessor); | ||
nameManager.preprocessNames(); | ||
const importProcessor = new CJSImportProcessor(nameManager, tokenProcessor, false); | ||
importProcessor.preprocessTokens(); | ||
assert.strictEqual( | ||
hasShadowedGlobals(tokenProcessor, importProcessor.getGlobalNames()), | ||
expected, | ||
); | ||
} | ||
|
||
describe("identifyShadowedGlobals", () => { | ||
it("properly does an up-front that there are any shadowed globals", () => { | ||
assertHasShadowedGlobals( | ||
` | ||
import a from 'a'; | ||
function foo() { | ||
const a = 3; | ||
console.log(a); | ||
} | ||
`, | ||
true, | ||
); | ||
}); | ||
|
||
it("properly detects when there are no shadowed globals", () => { | ||
assertHasShadowedGlobals( | ||
` | ||
import a from 'a'; | ||
export const b = 3; | ||
`, | ||
false, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.