Просмотр исходного кода

Using of internal storage to store lexical scope directives

sanex 4 лет назад
Родитель
Сommit
adcf2691aa

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
dist/index.browser.js


Разница между файлами не показана из-за своего большого размера
+ 0 - 0
dist/index.cli.js


Разница между файлами не показана из-за своего большого размера
+ 0 - 0
dist/index.js


+ 0 - 21
src/declarations/ESTree.d.ts

@@ -13,25 +13,13 @@ declare module 'estree' {
         ignoredNode?: boolean;
     }
 
-    export interface FunctionNodeMetadata extends BaseNodeMetadata {
-        directiveNode?: Directive | null;
-    }
-
     export interface LiteralNodeMetadata extends BaseNodeMetadata {
         replacedLiteral?: boolean;
     }
 
-    export interface ProgramNodeMetadata extends BaseNodeMetadata {
-        directiveNode?: Directive | null;
-    }
-
     /**
      * Nodes
      */
-    interface ArrowFunctionExpression extends BaseNode {
-        metadata?: FunctionNodeMetadata;
-    }
-
     interface BaseNode {
         metadata?: BaseNodeMetadata;
         parentNode?: Node;
@@ -47,16 +35,7 @@ declare module 'estree' {
         loc?: acorn.SourceLocation;
     }
 
-    interface FunctionExpression extends BaseNode {
-        metadata?: FunctionNodeMetadata;
-    }
-
-    interface FunctionDeclaration extends BaseNode {
-        metadata?: FunctionNodeMetadata;
-    }
-
     interface Program extends BaseNode {
-        metadata?: ProgramNodeMetadata;
         scope?: eslintScope.Scope | null;
     }
 

+ 11 - 3
src/node-transformers/finalizing-transformers/DirectivePlacementTransformer.ts

@@ -4,6 +4,7 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
 import * as estraverse from 'estraverse';
 import * as ESTree from 'estree';
 
+import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
 import { TNodeWithLexicalScopeStatements } from '../../types/node/TNodeWithLexicalScopeStatements';
 
 import { IOptions } from '../../interfaces/options/IOptions';
@@ -16,7 +17,6 @@ import { NodeTransformer } from '../../enums/node-transformers/NodeTransformer';
 import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
 import { NodeAppender } from '../../node/NodeAppender';
 import { NodeGuards } from '../../node/NodeGuards';
-import { NodeMetadata } from '../../node/NodeMetadata';
 import { NodeUtils } from '../../node/NodeUtils';
 
 /**
@@ -32,6 +32,14 @@ export class DirectivePlacementTransformer extends AbstractNodeTransformer {
         NodeTransformer.CustomCodeHelpersTransformer
     ];
 
+    /**
+     * @type {WeakMap<TNodeWithLexicalScope, Directive>}
+     */
+    private readonly lexicalScopeDirectives: WeakMap<
+        TNodeWithLexicalScope,
+        ESTree.Directive
+    > = new WeakMap<TNodeWithLexicalScope, ESTree.Directive>();
+
     /**
      * @param {IRandomGenerator} randomGenerator
      * @param {IOptions} options
@@ -94,7 +102,7 @@ export class DirectivePlacementTransformer extends AbstractNodeTransformer {
         const firstStatementNode = nodeWithLexicalScopeStatements.body[0] ?? null;
 
         if (firstStatementNode && NodeGuards.isDirectiveNode(firstStatementNode)) {
-            NodeMetadata.set(parentNode, {directiveNode: firstStatementNode});
+            this.lexicalScopeDirectives.set(parentNode, firstStatementNode);
         }
 
         return nodeWithLexicalScopeStatements;
@@ -113,7 +121,7 @@ export class DirectivePlacementTransformer extends AbstractNodeTransformer {
             return nodeWithLexicalScopeStatements;
         }
 
-        const directiveNode: ESTree.Directive | null | undefined = NodeMetadata.getDirectiveNode(parentNode);
+        const directiveNode: ESTree.Directive | undefined = this.lexicalScopeDirectives.get(parentNode);
 
         if (directiveNode) {
             const newDirectiveNode: ESTree.Directive = NodeUtils.clone(directiveNode);

+ 0 - 13
src/node/NodeMetadata.ts

@@ -1,5 +1,4 @@
 import * as ESTree from 'estree';
-import { TNodeWithLexicalScope } from '../types/node/TNodeWithLexicalScope';
 
 export class NodeMetadata {
     /**
@@ -47,16 +46,4 @@ export class NodeMetadata {
     public static isReplacedLiteral (literalNode: ESTree.Literal): boolean {
         return NodeMetadata.get<ESTree.LiteralNodeMetadata, 'replacedLiteral'>(literalNode, 'replacedLiteral') === true;
     }
-
-    /**
-     * @param {TNodeWithLexicalScope} nodeWithLexicalScope
-     * @returns {Directive | null | undefined}
-     */
-    public static getDirectiveNode (nodeWithLexicalScope: TNodeWithLexicalScope): ESTree.Directive | null | undefined {
-        return NodeMetadata.get<
-            ESTree.ProgramNodeMetadata
-            | ESTree.FunctionNodeMetadata,
-            'directiveNode'
-        >(nodeWithLexicalScope, 'directiveNode');
-    }
 }

Некоторые файлы не были показаны из-за большого количества измененных файлов