forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ObjectPatternPropertiesTransformer. Added helper for stubbing s…
…elected node transformers.
- Loading branch information
sanex3339
committed
Jul 31, 2020
1 parent
f360d0e
commit c85f6c0
Showing
18 changed files
with
227 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/node-transformers/converting-transformers/ObjectPatternPropertiesTransformer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { inject, injectable, } from 'inversify'; | ||
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers'; | ||
|
||
import * as ESTree from 'estree'; | ||
|
||
import { IOptions } from '../../interfaces/options/IOptions'; | ||
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator'; | ||
import { IVisitor } from '../../interfaces/node-transformers/IVisitor'; | ||
|
||
import { NodeTransformationStage } from '../../enums/node-transformers/NodeTransformationStage'; | ||
|
||
import { AbstractNodeTransformer } from '../AbstractNodeTransformer'; | ||
import { NodeGuards } from '../../node/NodeGuards'; | ||
import { NodeUtils } from '../../node/NodeUtils'; | ||
|
||
@injectable() | ||
export class ObjectPatternPropertiesTransformer extends AbstractNodeTransformer { | ||
/** | ||
* @param {IRandomGenerator} randomGenerator | ||
* @param {IOptions} options | ||
*/ | ||
public constructor ( | ||
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator, | ||
@inject(ServiceIdentifiers.IOptions) options: IOptions | ||
) { | ||
super(randomGenerator, options); | ||
} | ||
|
||
/** | ||
* @param {NodeTransformationStage} nodeTransformationStage | ||
* @returns {IVisitor | null} | ||
*/ | ||
public getVisitor (nodeTransformationStage: NodeTransformationStage): IVisitor | null { | ||
switch (nodeTransformationStage) { | ||
case NodeTransformationStage.Converting: | ||
return { | ||
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): ESTree.Node | undefined => { | ||
if (parentNode && NodeGuards.isPropertyNode(node)) { | ||
return this.transformNode(node, parentNode); | ||
} | ||
} | ||
}; | ||
|
||
default: | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* replaces: | ||
* const {foo} = bar; | ||
* | ||
* on: | ||
* const {foo: foo} = bar; | ||
* | ||
* @param {Property} propertyNode | ||
* @param {NodeGuards} parentNode | ||
* @returns {NodeGuards} | ||
*/ | ||
public transformNode (propertyNode: ESTree.Property, parentNode: ESTree.Node): ESTree.Node { | ||
if (!NodeGuards.isObjectPatternNode(parentNode) || !propertyNode.shorthand) { | ||
return propertyNode; | ||
} | ||
|
||
propertyNode.shorthand = false; | ||
propertyNode.value = NodeUtils.clone(propertyNode.value); | ||
|
||
NodeUtils.parentizeNode(propertyNode.value, parentNode); | ||
|
||
return propertyNode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as mocha from 'mocha'; | ||
import * as sinon from 'sinon'; | ||
|
||
import { INodeTransformer } from '../../src/interfaces/node-transformers/INodeTransformer'; | ||
|
||
export function stubNodeTransformers (nodeTransformers: (new (...args: any[]) => INodeTransformer)[]): void { | ||
const transformerStubs: sinon.SinonStub[] = []; | ||
|
||
mocha.before(() => { | ||
for (const nodeTransformer of nodeTransformers) { | ||
const stub: sinon.SinonStub = sinon | ||
.stub(nodeTransformer.prototype, 'getVisitor') | ||
.callsFake(() => null); | ||
|
||
transformerStubs.push(stub); | ||
} | ||
}); | ||
|
||
mocha.after(() => { | ||
for (const stub of transformerStubs) { | ||
stub.restore(); | ||
} | ||
}); | ||
} |
Oops, something went wrong.