Fixed wrong generation of code that has statements after ReturnStatment when simplify option is enabled

This commit is contained in:
Timofey Kachalov
2022-02-06 21:10:16 +03:00
committed by GitHub
parent 40eec6185c
commit 77f64bf5df
17 changed files with 311 additions and 49 deletions
-4
View File
@@ -54,8 +54,4 @@ declare module 'estree' {
metadata?: LiteralNodeMetadata;
'x-verbatim-property'?: escodegen.XVerbatimProperty;
}
interface StaticBlock extends Omit<BlockStatement, 'type'> {
type: 'StaticBlock'
}
}
@@ -15,4 +15,9 @@ export interface IIteratedStatementsSimplifyData {
* @type {boolean}
*/
hasReturnStatement: boolean;
/**
* @type {boolean}
*/
hasStatementsAfterReturnStatement: boolean;
}
@@ -63,9 +63,19 @@ export abstract class AbstractStatementSimplifyTransformer extends AbstractNodeT
const {
startIndex,
unwrappedExpressions,
hasReturnStatement
hasReturnStatement,
hasStatementsAfterReturnStatement
} = this.collectIteratedStatementsSimplifyData(statementNode);
if (hasStatementsAfterReturnStatement) {
return {
leadingStatements: statementNode.body,
trailingStatement: null,
hasReturnStatement: false,
hasSingleExpression: false
};
}
const leadingStatements: ESTree.Statement[] = this.getLeadingStatements(statementNode, startIndex);
if (!unwrappedExpressions.length) {
@@ -111,6 +121,7 @@ export abstract class AbstractStatementSimplifyTransformer extends AbstractNodeT
const unwrappedExpressions: ESTree.Expression[] = [];
let hasReturnStatement: boolean = false;
let hasStatementsAfterReturnStatement: boolean = false;
let startIndex: number | null = null;
for (let i = statementNodeBodyLength - 1; i >= 0; i--) {
@@ -133,6 +144,7 @@ export abstract class AbstractStatementSimplifyTransformer extends AbstractNodeT
) {
unwrappedExpressions.unshift(statementBodyStatementNode.argument);
hasReturnStatement = true;
hasStatementsAfterReturnStatement = i !== statementNodeBodyLength - 1;
startIndex = i;
continue;
}
@@ -143,7 +155,8 @@ export abstract class AbstractStatementSimplifyTransformer extends AbstractNodeT
return {
startIndex,
unwrappedExpressions,
hasReturnStatement
hasReturnStatement,
hasStatementsAfterReturnStatement
};
}
+2 -4
View File
@@ -507,10 +507,8 @@ export class NodeGuards {
* @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;
public static isStaticBlockNode (node: ESTree.Node): node is ESTree.StaticBlock {
return node.type === NodeType.StaticBlock;
}
/**