Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 47abd82

Browse files
armano2platinumazure
authored andcommitted
Fix: issue with global augmentation when tokens are not present (#574)
1 parent c5ffad3 commit 47abd82

File tree

3 files changed

+4
-38
lines changed

3 files changed

+4
-38
lines changed

analyze-scope.js

+1-34
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,8 @@ const Reference = require("eslint-scope/lib/reference");
99
const OriginalReferencer = require("eslint-scope/lib/referencer");
1010
const Scope = require("eslint-scope/lib/scope").Scope;
1111
const fallback = require("eslint-visitor-keys").getKeys;
12-
const lodash = require("lodash");
1312
const childVisitorKeys = require("./visitor-keys");
1413

15-
/**
16-
* Get `.range[0]` of a given object.
17-
* @param {{range: number[]}} x The object to get.
18-
* @returns {number} The gotten value.
19-
*/
20-
function byRange0(x) {
21-
return x.range[0];
22-
}
23-
24-
/**
25-
* Check the TSModuleDeclaration node is `declare global {}` or not.
26-
* @param {TSModuleDeclaration} node The TSModuleDeclaration node to check.
27-
* @param {Token[]} tokens The token list.
28-
* @returns {boolean} `true` if the node is `declare global {}`.
29-
*/
30-
function isGlobalAugmentation(node, tokens) {
31-
const i = lodash.sortedIndexBy(tokens, node, byRange0);
32-
const token1 = tokens[i];
33-
const token2 = tokens[i + 1];
34-
35-
return Boolean(
36-
token1 &&
37-
token2 &&
38-
(token1.type === "Keyword" || token1.type === "Identifier") &&
39-
token1.value === "declare" &&
40-
(token2.type === "Keyword" || token2.type === "Identifier") &&
41-
token2.value === "global"
42-
);
43-
}
44-
4514
/**
4615
* Define the override function of `Scope#__define` for global augmentation.
4716
* @param {Function} define The original Scope#__define method.
@@ -570,12 +539,10 @@ class Referencer extends OriginalReferencer {
570539
* @returns {void}
571540
*/
572541
TSModuleDeclaration(node) {
573-
const astRoot = this.scopeManager.globalScope.block;
574542
const scope = this.currentScope();
575543
const { id, body } = node;
576544

577-
// https://github.com/JamesHenry/typescript-estree/issues/27
578-
if (isGlobalAugmentation(node, astRoot.tokens)) {
545+
if (node.global) {
579546
this.visitGlobalAugmentation(node);
580547
return;
581548
}

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
"dependencies": {
4747
"eslint-scope": "^4.0.0",
4848
"eslint-visitor-keys": "^1.0.0",
49-
"lodash": "^4.17.11",
50-
"typescript-estree": "5.0.0"
49+
"typescript-estree": "5.3.0"
5150
},
5251
"devDependencies": {
5352
"eslint": "^4.19.1",

parser.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ exports.parseForESLint = function parseForESLint(code, options) {
4141
traverser.traverse(ast, {
4242
enter: node => {
4343
switch (node.type) {
44-
// Just for backword compatibility.
44+
// Just for backward compatibility.
4545
case "DeclareFunction":
4646
if (!node.body) {
4747
node.type = `TSEmptyBody${node.type}`;
@@ -58,7 +58,7 @@ exports.parseForESLint = function parseForESLint(code, options) {
5858

5959
// Import/Export declarations cannot appear in script.
6060
// But if those appear only in namespace/module blocks, `ast.sourceType` was `"script"`.
61-
// This doesn't modify `ast.sourceType` directly for backrard compatibility.
61+
// This doesn't modify `ast.sourceType` directly for backward compatibility.
6262
case "ImportDeclaration":
6363
case "ExportAllDeclaration":
6464
case "ExportDefaultDeclaration":

0 commit comments

Comments
 (0)