Support for class static blocks

This commit is contained in:
sanex
2022-01-29 14:57:21 +03:00
parent ac3c34fe77
commit 6b8ead0b2b
11 changed files with 115 additions and 15 deletions
+5
View File
@@ -3,6 +3,7 @@
import * as acorn from 'acorn';
import * as escodegen from '@javascript-obfuscator/escodegen';
import * as eslintScope from 'eslint-scope';
import { BlockStatement } from 'estree';
declare module 'estree' {
/**
@@ -53,4 +54,8 @@ declare module 'estree' {
metadata?: LiteralNodeMetadata;
'x-verbatim-property'?: escodegen.XVerbatimProperty;
}
interface StaticBlock extends Omit<BlockStatement, 'type'> {
type: 'StaticBlock'
}
}
+1
View File
@@ -47,6 +47,7 @@ export enum NodeType {
ReturnStatement = 'ReturnStatement',
SequenceExpression = 'SequenceExpression',
SpreadElement = 'SpreadElement',
StaticBlock = 'StaticBlock',
Super = 'Super',
SwitchCase = 'SwitchCase',
SwitchStatement = 'SwitchStatement',
+12
View File
@@ -544,6 +544,18 @@ export class NodeFactory {
};
}
/**
* @param {Statement[]} body
* @returns {StaticBlock}
*/
public static staticBlockNode (body: ESTree.Statement[] = []): ESTree.StaticBlock {
return {
type: NodeType.StaticBlock,
body,
metadata: { ignoredNode: false }
};
}
/**
* @param {Expression} discriminant
* @param {SwitchCase[]} cases
+14 -1
View File
@@ -365,7 +365,9 @@ export class NodeGuards {
* @returns {boolean}
*/
public static isNodeWithBlockLexicalScope (node: ESTree.Node): node is TNodeWithLexicalScope {
return NodeGuards.isNodeWithLexicalScope(node) || NodeGuards.isBlockStatementNode(node);
return NodeGuards.isNodeWithLexicalScope(node)
|| NodeGuards.isBlockStatementNode(node)
|| NodeGuards.isStaticBlockNode(node);
}
/**
@@ -417,6 +419,7 @@ export class NodeGuards {
public static isNodeWithStatements (node: ESTree.Node): node is TNodeWithStatements {
return NodeGuards.isProgramNode(node)
|| NodeGuards.isBlockStatementNode(node)
|| NodeGuards.isStaticBlockNode(node)
|| NodeGuards.isSwitchCaseNode(node);
}
@@ -500,6 +503,16 @@ export class NodeGuards {
return node.type === NodeType.SpreadElement;
}
/**
* @param {Node} node
* @returns {boolean}
*/
// TODO: add type guard after @types/estree update
public static isStaticBlockNode (node: ESTree.Node): boolean {
// TODO: Update @types/estree
return (<any>node).type === NodeType.StaticBlock;
}
/**
* @param {Node} node
* @returns {boolean}