Skip to content

refactor(eslint-parser): add definition for eslint-scope #33

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

Merged
merged 4 commits into from
Jan 17, 2019
Merged
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
3 changes: 3 additions & 0 deletions packages/typescript-eslint-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
"typescript-estree": "18.0.0"
},
"devDependencies": {
"@types/eslint": "^4.16.5",
"@types/estree": "^0.0.39",
"@types/eslint-visitor-keys": "^1.0.0",
"@typescript-eslint/shared-fixtures": "*"
}
}
46 changes: 34 additions & 12 deletions packages/typescript-eslint-parser/src/analyze-scope.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import escope from 'eslint-scope';
import { ScopeManager } from 'eslint-scope';
import { Definition, ParameterDefinition } from 'eslint-scope/lib/definition';
import OriginalPatternVisitor from 'eslint-scope/lib/pattern-visitor';
import Reference from 'eslint-scope/lib/reference';
Expand All @@ -7,6 +7,11 @@ import { Scope } from 'eslint-scope/lib/scope';
import { getKeys as fallback } from 'eslint-visitor-keys';
import { ParserOptions } from './parser-options';
import { visitorKeys as childVisitorKeys } from './visitor-keys';
import {
PatternVisitorCallback,
PatternVisitorOptions
} from 'eslint-scope/lib/options';
import { Node } from 'estree';

/**
* Define the override function of `Scope#__define` for global augmentation.
Expand All @@ -27,14 +32,23 @@ function overrideDefine(define: any) {

/** The scope class for enum. */
class EnumScope extends Scope {
constructor(scopeManager: any, upperScope: any, block: any) {
constructor(
scopeManager: ScopeManager,
upperScope: Scope,
block: Node | null
) {
// @ts-ignore
super(scopeManager, 'enum', upperScope, block, false);
}
}

class PatternVisitor extends OriginalPatternVisitor {
constructor(...args: any[]) {
super(...args);
constructor(
options: PatternVisitorOptions,
rootPattern: any,
callback: PatternVisitorCallback
) {
super(options, rootPattern, callback);
}

Identifier(node: any) {
Expand Down Expand Up @@ -76,8 +90,10 @@ class PatternVisitor extends OriginalPatternVisitor {
}

class Referencer extends OriginalReferencer {
constructor(...args: any[]) {
super(...args);
protected typeMode: boolean;

constructor(options: any, scopeManager: ScopeManager) {
super(options, scopeManager);
this.typeMode = false;
}

Expand All @@ -88,7 +104,11 @@ class Referencer extends OriginalReferencer {
* @param {Function} callback The callback function for left-hand side nodes.
* @returns {void}
*/
visitPattern(node: any, options: any, callback: any) {
visitPattern(
node: any,
options: PatternVisitorOptions,
callback: PatternVisitorCallback
) {
if (!node) {
return;
}
Expand All @@ -102,6 +122,7 @@ class Referencer extends OriginalReferencer {
visitor.visit(node);

if (options.processRightHandNodes) {
// @ts-ignore
visitor.rightHandNodes.forEach(this.visit, this);
}
}
Expand All @@ -125,11 +146,12 @@ class Referencer extends OriginalReferencer {
);

// Remove overload definition to avoid confusion of no-redeclare rule.
const { defs, identifiers } = upperScope.set.get(id.name);
const { defs, identifiers } = upperScope.set.get(id.name)!;
for (let i = 0; i < defs.length; ++i) {
const def = defs[i];
if (
def.type === 'FunctionName' &&
// @ts-ignore
def.node.type === 'TSDeclareFunction'
) {
defs.splice(i, 1);
Expand All @@ -153,7 +175,7 @@ class Referencer extends OriginalReferencer {
this.visitPattern(
params[i],
{ processRightHandNodes: true },
(pattern: any, info: any) => {
(pattern, info) => {
innerScope.__define(
pattern,
new ParameterDefinition(pattern, node, i, info.rest)
Expand Down Expand Up @@ -305,7 +327,7 @@ class Referencer extends OriginalReferencer {
if (id) {
const variable = scope.set.get(id.name);
const defs = variable && variable.defs;
const existed = defs && defs.some((d: any) => d.type === 'FunctionName');
const existed = defs && defs.some(d => d.type === 'FunctionName');
if (!existed) {
scope.__define(
id,
Expand Down Expand Up @@ -743,7 +765,7 @@ class Referencer extends OriginalReferencer {
* @param {Decorator[]|undefined} decorators The decorator nodes to visit.
* @returns {void}
*/
visitDecorators(decorators: any[] | undefined) {
visitDecorators(decorators?: any[]) {
if (decorators) {
decorators.forEach(this.visit, this);
}
Expand Down Expand Up @@ -781,7 +803,7 @@ export function analyzeScope(ast: any, parserOptions: ParserOptions) {
fallback
};

const scopeManager = new escope.ScopeManager(options);
const scopeManager = new ScopeManager(options);
const referencer = new Referencer(options, scopeManager);

referencer.visit(ast);
Expand Down
Loading