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

Commit

Permalink
Fix: issue with global augmentation when tokens are not present (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 authored and platinumazure committed Dec 3, 2018
1 parent c5ffad3 commit 47abd82
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 38 deletions.
35 changes: 1 addition & 34 deletions analyze-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,8 @@ const Reference = require("eslint-scope/lib/reference");
const OriginalReferencer = require("eslint-scope/lib/referencer");
const Scope = require("eslint-scope/lib/scope").Scope;
const fallback = require("eslint-visitor-keys").getKeys;
const lodash = require("lodash");
const childVisitorKeys = require("./visitor-keys");

/**
* Get `.range[0]` of a given object.
* @param {{range: number[]}} x The object to get.
* @returns {number} The gotten value.
*/
function byRange0(x) {
return x.range[0];
}

/**
* Check the TSModuleDeclaration node is `declare global {}` or not.
* @param {TSModuleDeclaration} node The TSModuleDeclaration node to check.
* @param {Token[]} tokens The token list.
* @returns {boolean} `true` if the node is `declare global {}`.
*/
function isGlobalAugmentation(node, tokens) {
const i = lodash.sortedIndexBy(tokens, node, byRange0);
const token1 = tokens[i];
const token2 = tokens[i + 1];

return Boolean(
token1 &&
token2 &&
(token1.type === "Keyword" || token1.type === "Identifier") &&
token1.value === "declare" &&
(token2.type === "Keyword" || token2.type === "Identifier") &&
token2.value === "global"
);
}

/**
* Define the override function of `Scope#__define` for global augmentation.
* @param {Function} define The original Scope#__define method.
Expand Down Expand Up @@ -570,12 +539,10 @@ class Referencer extends OriginalReferencer {
* @returns {void}
*/
TSModuleDeclaration(node) {
const astRoot = this.scopeManager.globalScope.block;
const scope = this.currentScope();
const { id, body } = node;

// https://github.com/JamesHenry/typescript-estree/issues/27
if (isGlobalAugmentation(node, astRoot.tokens)) {
if (node.global) {
this.visitGlobalAugmentation(node);
return;
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
"dependencies": {
"eslint-scope": "^4.0.0",
"eslint-visitor-keys": "^1.0.0",
"lodash": "^4.17.11",
"typescript-estree": "5.0.0"
"typescript-estree": "5.3.0"
},
"devDependencies": {
"eslint": "^4.19.1",
Expand Down
4 changes: 2 additions & 2 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ exports.parseForESLint = function parseForESLint(code, options) {
traverser.traverse(ast, {
enter: node => {
switch (node.type) {
// Just for backword compatibility.
// Just for backward compatibility.
case "DeclareFunction":
if (!node.body) {
node.type = `TSEmptyBody${node.type}`;
Expand All @@ -58,7 +58,7 @@ exports.parseForESLint = function parseForESLint(code, options) {

// Import/Export declarations cannot appear in script.
// But if those appear only in namespace/module blocks, `ast.sourceType` was `"script"`.
// This doesn't modify `ast.sourceType` directly for backrard compatibility.
// This doesn't modify `ast.sourceType` directly for backward compatibility.
case "ImportDeclaration":
case "ExportAllDeclaration":
case "ExportDefaultDeclaration":
Expand Down

0 comments on commit 47abd82

Please sign in to comment.