123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- import { injectable, } from 'inversify';
- import * as eslintScope from 'eslint-scope';
- import * as estraverse from 'estraverse';
- import * as ESTree from 'estree';
- import { IScopeAnalyzer } from '../../interfaces/analyzers/scope-analyzer/IScopeAnalyzer';
- import { ecmaVersion } from '../../constants/EcmaVersion';
- import { NodeGuards } from '../../node/NodeGuards';
- @injectable()
- export class ScopeAnalyzer implements IScopeAnalyzer {
- /**
- * @type {eslintScope.AnalysisOptions}
- */
- private static readonly eslintScopeOptions: eslintScope.AnalysisOptions = {
- ecmaVersion,
- optimistic: true
- };
- /**
- * @type {acorn.Options['sourceType'][]}
- */
- private static readonly sourceTypes: acorn.Options['sourceType'][] = [
- 'script',
- 'module'
- ];
- /**
- * @type {number}
- */
- private static readonly emptyRangeValue: number = 0;
- /**
- * @type {eslintScope.ScopeManager | null}
- */
- private scopeManager: eslintScope.ScopeManager | null = null;
- /**
- * `eslint-scope` reads `ranges` property of a nodes
- * Should attach that property to the some custom nodes
- *
- * @param {Node} astTree
- */
- private static attachMissingRanges (astTree: ESTree.Node): void {
- estraverse.replace(astTree, {
- enter: (node: ESTree.Node): ESTree.Node => {
- if (!node.range) {
- node.range = [
- node.parentNode?.range?.[0] ?? ScopeAnalyzer.emptyRangeValue,
- node.parentNode?.range?.[1] ?? ScopeAnalyzer.emptyRangeValue
- ];
- }
- return node;
- }
- });
- }
- /**
- * @param {Node} node
- * @returns {boolean}
- */
- private static isRootNode (node: ESTree.Node): boolean {
- return NodeGuards.isProgramNode(node) || node.parentNode === node;
- }
- /**
- * @param {Program} astTree
- */
- public analyze (astTree: ESTree.Node): void {
- const sourceTypeLength: number = ScopeAnalyzer.sourceTypes.length;
- ScopeAnalyzer.attachMissingRanges(astTree);
- for (let i: number = 0; i < sourceTypeLength; i++) {
- try {
- this.scopeManager = eslintScope.analyze(astTree, {
- ...ScopeAnalyzer.eslintScopeOptions,
- sourceType: ScopeAnalyzer.sourceTypes[i]
- });
- return;
- } catch (error) {
- if (i < sourceTypeLength - 1) {
- continue;
- }
- throw new Error(error);
- }
- }
- throw new Error('Scope analyzing error');
- }
- /**
- * @param {Node} node
- * @returns {Scope}
- */
- public acquireScope (node: ESTree.Node): eslintScope.Scope {
- if (!this.scopeManager) {
- throw new Error('Scope manager is not defined');
- }
- const scope: eslintScope.Scope | null = this.scopeManager.acquire(
- node,
- ScopeAnalyzer.isRootNode(node)
- );
- if (!scope) {
- throw new Error('Cannot acquire scope for node');
- }
- this.sanitizeScopes(scope);
- return scope;
- }
- /**
- * @param {Scope} scope
- */
- private sanitizeScopes (scope: eslintScope.Scope): void {
- scope.childScopes.forEach((childScope: eslintScope.Scope) => {
- // fix of class scopes
- // trying to move class scope references to the parent scope
- if (childScope.type === 'class' && childScope.upper) {
- if (!childScope.variables.length) {
- return;
- }
- // class name variable is always first
- const classNameVariable: eslintScope.Variable = childScope.variables[0];
- const upperVariable: eslintScope.Variable | undefined = childScope.upper.variables
- .find((variable: eslintScope.Variable) => {
- const isValidClassNameVariable: boolean = classNameVariable.defs
- .some((definition: eslintScope.Definition) => definition.type === 'ClassName');
- return isValidClassNameVariable && variable.name === classNameVariable.name;
- });
- upperVariable?.references.push(...childScope.variables[0].references);
- }
- });
- for (const childScope of scope.childScopes) {
- this.sanitizeScopes(childScope);
- }
- }
- }
|