Preventing move of "use strict"; directive during obfuscation

This commit is contained in:
sanex
2020-12-29 23:29:16 +03:00
parent d2671daad4
commit 2927fa0ab8
23 changed files with 365 additions and 38 deletions
-9
View File
@@ -422,15 +422,6 @@ export class NodeGuards {
return node.type === NodeType.UnaryExpression;
}
/**
* @param {Node} node
* @returns {boolean}
*/
public static isUseStrictOperator (node: ESTree.Node): node is ESTree.Directive {
return NodeGuards.isDirectiveNode(node)
&& node.directive === 'use strict';
}
/**
* @param {Node} node
* @returns {boolean}
+20 -4
View File
@@ -1,4 +1,5 @@
import * as ESTree from 'estree';
import { TNodeWithLexicalScope } from '../types/node/TNodeWithLexicalScope';
export class NodeMetadata {
/**
@@ -14,7 +15,10 @@ export class NodeMetadata {
* @param {keyof T} metadataKey
* @returns {T[keyof T] | undefined}
*/
public static get <T extends ESTree.BaseNodeMetadata> (node: ESTree.Node, metadataKey: keyof T): T[keyof T] | undefined {
public static get <
T extends ESTree.BaseNodeMetadata,
TMetadataKey extends keyof T
> (node: ESTree.Node, metadataKey: TMetadataKey): T[TMetadataKey] | undefined {
return node.metadata !== undefined
? (<T>node.metadata)[metadataKey]
: undefined;
@@ -25,7 +29,7 @@ export class NodeMetadata {
* @returns {boolean}
*/
public static isForceTransformNode (node: ESTree.Node): boolean {
return NodeMetadata.get(node, 'forceTransformNode') === true;
return NodeMetadata.get<ESTree.BaseNodeMetadata, 'forceTransformNode'>(node, 'forceTransformNode') === true;
}
/**
@@ -33,7 +37,7 @@ export class NodeMetadata {
* @returns {boolean}
*/
public static isIgnoredNode (node: ESTree.Node): boolean {
return NodeMetadata.get(node, 'ignoredNode') === true;
return NodeMetadata.get<ESTree.BaseNodeMetadata, 'ignoredNode'>(node, 'ignoredNode') === true;
}
/**
@@ -41,6 +45,18 @@ export class NodeMetadata {
* @returns {boolean}
*/
public static isReplacedLiteral (literalNode: ESTree.Literal): boolean {
return NodeMetadata.get<ESTree.LiteralNodeMetadata>(literalNode, 'replacedLiteral') === true;
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');
}
}