Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #792 from javascript-obfuscator/export-specifier-t…
…ransformer Added `ExportSpecifierTransformer`
- Loading branch information
Showing
16 changed files
with
294 additions
and
84 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
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
67 changes: 67 additions & 0 deletions
67
src/node-transformers/converting-transformers/ExportSpecifierTransformer.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,67 @@ | ||
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 ExportSpecifierTransformer 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.isExportSpecifierNode(node)) { | ||
return this.transformNode(node, parentNode); | ||
} | ||
} | ||
}; | ||
|
||
default: | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Replaces: | ||
* export {foo}; | ||
* | ||
* on: | ||
* export {foo as foo}; | ||
* | ||
* @param {ExportSpecifier} exportSpecifierNode | ||
* @param {Node} parentNode | ||
* @returns {Node} | ||
*/ | ||
public transformNode (exportSpecifierNode: ESTree.ExportSpecifier, parentNode: ESTree.Node): ESTree.Node { | ||
if (exportSpecifierNode.local.name === exportSpecifierNode.exported.name) { | ||
exportSpecifierNode.exported = NodeUtils.clone(exportSpecifierNode.local); | ||
} | ||
|
||
return exportSpecifierNode; | ||
} | ||
} |
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
113 changes: 113 additions & 0 deletions
113
...s/converting-transformers/export-specifier-transformer/ExportSpecifierTransformer.spec.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,113 @@ | ||
import { assert } from 'chai'; | ||
|
||
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes'; | ||
|
||
import { readFileAsString } from '../../../../helpers/readFileAsString'; | ||
|
||
import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade'; | ||
|
||
describe('ExportSpecifierTransformer', () => { | ||
describe('Variant #1: exported constant', () => { | ||
describe('Variant #1:`renameGlobals` option is enabled', () => { | ||
const regExp: RegExp = new RegExp( | ||
'const _0x([a-f0-9]){4,6} *= *0x1; *' + | ||
'export *{_0x([a-f0-9]){4,6} as foo};' | ||
); | ||
|
||
let obfuscatedCode: string; | ||
|
||
before(() => { | ||
const code: string = readFileAsString(__dirname + '/fixtures/exported-constant.js'); | ||
|
||
obfuscatedCode = JavaScriptObfuscator.obfuscate( | ||
code, | ||
{ | ||
...NO_ADDITIONAL_NODES_PRESET, | ||
renameGlobals: true | ||
} | ||
).getObfuscatedCode(); | ||
}); | ||
|
||
it('should transform export specifier node', () => { | ||
assert.match(obfuscatedCode, regExp); | ||
}); | ||
}); | ||
|
||
describe('Variant #2: `renameGlobals` option is disabled', () => { | ||
const regExp: RegExp = new RegExp( | ||
'const foo *= *0x1; *' + | ||
'export *{foo};' | ||
); | ||
|
||
let obfuscatedCode: string; | ||
|
||
before(() => { | ||
const code: string = readFileAsString(__dirname + '/fixtures/exported-constant.js'); | ||
|
||
obfuscatedCode = JavaScriptObfuscator.obfuscate( | ||
code, | ||
{ | ||
...NO_ADDITIONAL_NODES_PRESET, | ||
renameGlobals: false | ||
} | ||
).getObfuscatedCode(); | ||
}); | ||
|
||
it('should not transform export specifier node', () => { | ||
assert.match(obfuscatedCode, regExp); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Variant #2: exported import', () => { | ||
describe('Variant #1:`renameGlobals` option is enabled', () => { | ||
const regExp: RegExp = new RegExp( | ||
'import _0x([a-f0-9]){4,6} from *\'./bar\'; *' + | ||
'export *{_0x([a-f0-9]){4,6} as foo};' | ||
); | ||
|
||
let obfuscatedCode: string; | ||
|
||
before(() => { | ||
const code: string = readFileAsString(__dirname + '/fixtures/exported-import.js'); | ||
|
||
obfuscatedCode = JavaScriptObfuscator.obfuscate( | ||
code, | ||
{ | ||
...NO_ADDITIONAL_NODES_PRESET, | ||
renameGlobals: true | ||
} | ||
).getObfuscatedCode(); | ||
}); | ||
|
||
it('should transform export specifier node', () => { | ||
assert.match(obfuscatedCode, regExp); | ||
}); | ||
}); | ||
|
||
describe('Variant #2: `renameGlobals` option is disabled', () => { | ||
const regExp: RegExp = new RegExp( | ||
'import _0x([a-f0-9]){4,6} from *\'./bar\'; *' + | ||
'export *{_0x([a-f0-9]){4,6} as foo};' | ||
); | ||
|
||
let obfuscatedCode: string; | ||
|
||
before(() => { | ||
const code: string = readFileAsString(__dirname + '/fixtures/exported-import.js'); | ||
|
||
obfuscatedCode = JavaScriptObfuscator.obfuscate( | ||
code, | ||
{ | ||
...NO_ADDITIONAL_NODES_PRESET, | ||
renameGlobals: false | ||
} | ||
).getObfuscatedCode(); | ||
}); | ||
|
||
it('should transform export specifier node', () => { | ||
assert.match(obfuscatedCode, regExp); | ||
}); | ||
}); | ||
}); | ||
}); |
2 changes: 2 additions & 0 deletions
2
...ormers/converting-transformers/export-specifier-transformer/fixtures/exported-constant.js
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,2 @@ | ||
const foo = 1; | ||
export {foo}; |
2 changes: 2 additions & 0 deletions
2
...sformers/converting-transformers/export-specifier-transformer/fixtures/exported-import.js
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,2 @@ | ||
import foo from './bar'; | ||
export {foo}; |
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
Oops, something went wrong.