Improved ObjectPatternPropertiesTransformer logic. Added tests.

This commit is contained in:
sanex3339
2020-07-31 17:58:20 +03:00
parent c85f6c0092
commit 7ce5f964d6
11 changed files with 164 additions and 14 deletions
@@ -3,6 +3,8 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as ESTree from 'estree';
import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
import { IOptions } from '../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
@@ -11,6 +13,7 @@ import { NodeTransformationStage } from '../../enums/node-transformers/NodeTrans
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
import { NodeGuards } from '../../node/NodeGuards';
import { NodeLexicalScopeUtils } from '../../node/NodeLexicalScopeUtils';
import { NodeUtils } from '../../node/NodeUtils';
@injectable()
@@ -62,6 +65,15 @@ export class ObjectPatternPropertiesTransformer extends AbstractNodeTransformer
return propertyNode;
}
if (!this.options.renameGlobals) {
const lexicalScope: TNodeWithLexicalScope | undefined = NodeLexicalScopeUtils.getLexicalScope(propertyNode);
const shouldNotTransformGlobalPropertyNode: boolean = !!lexicalScope && NodeGuards.isProgramNode(lexicalScope);
if (shouldNotTransformGlobalPropertyNode) {
return propertyNode;
}
}
propertyNode.shorthand = false;
propertyNode.value = NodeUtils.clone(propertyNode.value);