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.
Merge pull request javascript-obfuscator#537 from javascript-obfuscat…
…or/issue-535 Fixed runtime error with class expressions
- Loading branch information
Showing
10 changed files
with
98 additions
and
8 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
64 changes: 64 additions & 0 deletions
64
...ating-transformers/scope-identifiers-transformer/class-expression/ClassExpression.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,64 @@ | ||
import { assert } from 'chai'; | ||
|
||
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes'; | ||
|
||
import { getRegExpMatch } from '../../../../../helpers/getRegExpMatch'; | ||
import { readFileAsString } from '../../../../../helpers/readFileAsString'; | ||
|
||
import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade'; | ||
|
||
describe('ScopeIdentifiersTransformer ClassExpression identifiers', () => { | ||
describe('transformation of ClassExpression identifiers', () => { | ||
describe('Variant #1: `ClassExpression` parent block scope is not a `ProgramNode`', () => { | ||
const classNameIdentifierRegExp: RegExp = /var (_0x[a-f0-9]{4,6}) *= *class *\{/; | ||
const classCallIdentifierRegExp: RegExp = /new *(_0x[a-f0-9]{4,6}) *\( *\);/; | ||
|
||
let obfuscatedCode: string, | ||
classNameIdentifier: string, | ||
classCallIdentifier: string; | ||
|
||
before(() => { | ||
const code: string = readFileAsString(__dirname + '/fixtures/base.js'); | ||
|
||
obfuscatedCode = JavaScriptObfuscator.obfuscate( | ||
code, | ||
{ | ||
...NO_ADDITIONAL_NODES_PRESET | ||
} | ||
).getObfuscatedCode(); | ||
classNameIdentifier = getRegExpMatch(obfuscatedCode, classNameIdentifierRegExp); | ||
classCallIdentifier = getRegExpMatch(obfuscatedCode, classCallIdentifierRegExp); | ||
}); | ||
|
||
it('should transform class variable name', () => { | ||
assert.equal(classNameIdentifier, classCallIdentifier); | ||
}); | ||
}); | ||
|
||
describe('Variant #2: `ClassExpression` parent block scope is a `ProgramNode`', () => { | ||
const classNameIdentifierRegExp: RegExp = /var Foo *= *class *\{/; | ||
const classCallIdentifierRegExp: RegExp = /new *Foo *\( *\);/; | ||
|
||
let obfuscatedCode: string; | ||
|
||
before(() => { | ||
const code: string = readFileAsString(__dirname + '/fixtures/parent-block-scope-is-program-node.js'); | ||
|
||
obfuscatedCode = JavaScriptObfuscator.obfuscate( | ||
code, | ||
{ | ||
...NO_ADDITIONAL_NODES_PRESET | ||
} | ||
).getObfuscatedCode(); | ||
}); | ||
|
||
it('match #1: shouldn\'t transform class name', () => { | ||
assert.match(obfuscatedCode, classNameIdentifierRegExp); | ||
}); | ||
|
||
it('match #2: shouldn\'t transform class name', () => { | ||
assert.match(obfuscatedCode, classCallIdentifierRegExp); | ||
}); | ||
}); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
.../obfuscating-transformers/scope-identifiers-transformer/class-expression/fixtures/base.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,9 @@ | ||
(function () { | ||
var Foo = class { | ||
getInstance () { | ||
return new Foo(); | ||
} | ||
}; | ||
|
||
new Foo(); | ||
})(); |
7 changes: 7 additions & 0 deletions
7
...e-identifiers-transformer/class-expression/fixtures/parent-block-scope-is-program-node.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,7 @@ | ||
var Foo = class { | ||
getInstance () { | ||
return new Foo(); | ||
} | ||
}; | ||
|
||
new 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