Added tests for prohibited single if statement

This commit is contained in:
sanex3339
2020-07-04 12:53:31 +03:00
parent 2e198d1190
commit 3a3d350d96
8 changed files with 89 additions and 32 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+4 -4
View File
@@ -63,14 +63,14 @@
"@typescript-eslint/parser": "3.5.0",
"chai": "4.2.0",
"coveralls": "3.1.0",
"eslint": "7.3.1",
"eslint": "7.4.0",
"eslint-plugin-import": "2.22.0",
"eslint-plugin-jsdoc": "28.5.1",
"eslint-plugin-jsdoc": "28.6.1",
"eslint-plugin-no-null": "1.0.2",
"eslint-plugin-prefer-arrow": "1.2.1",
"eslint-plugin-unicorn": "20.1.0",
"fork-ts-checker-notifier-webpack-plugin": "3.0.0",
"fork-ts-checker-webpack-plugin": "5.0.5",
"fork-ts-checker-webpack-plugin": "5.0.6",
"mocha": "8.0.1",
"nyc": "15.1.0",
"pjson": "1.0.9",
@@ -80,7 +80,7 @@
"threads": "1.6.3",
"ts-loader": "7.0.5",
"ts-node": "8.10.2",
"typescript": "3.9.5",
"typescript": "3.9.6",
"webpack": "4.43.0",
"webpack-cli": "3.3.12",
"webpack-node-externals": "1.7.2"
@@ -425,8 +425,24 @@ export class IfStatementSimplifyTransformer extends AbstractNodeTransformer {
// TODO: write tests
// function declaration is not allowed outside of block in `strict` mode
return NodeGuards.isFunctionDeclarationNode(statement)
// `IfStatement` may break the code
// TODO: catch this and write tests
/**
* Without ignore it can break following code:
* Input:
* if (condition1) {
* if (condition2) {
* var foo = bar();
* }
* } else {
* var baz = bark();
* }
*
* Invalid output:
* if (condition1)
* if (condition2)
* var foo = bar();
* else
* var baz = bark();
*/
|| NodeGuards.isIfStatementNode(statement);
}
}
+9 -8
View File
@@ -7,14 +7,15 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
`
function foo () {
if (true) {
if (foo === 1) {
}
} else {
function foo () {}
}
}
function foo() {
if (true) {
if (false) {
var bar = baz();
}
} else {
var bark = hawk();
}
}
`,
{
...NO_ADDITIONAL_NODES_PRESET,
@@ -750,5 +750,36 @@ describe('IfStatementSimplifyTransformer', () => {
});
});
});
describe('Prohibited single statement', () => {
describe('Variant #1: `IfStatement` as prohibited single statement', () => {
const regExp: RegExp = new RegExp(
'if *\\(!!\\[]\\) *{ *' +
'if *\\(!\\[]\\) *' +
'var _0x([a-f0-9]){4,6} *= *baz\\(\\); *' +
'} *else *' +
'var _0x([a-f0-9]){4,6} *= *hawk\\(\\);'
);
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/if-statement-as-prohibited-single-statement.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
minify: true
}
).getObfuscatedCode();
});
it('should not simplify if statement', () => {
assert.match(obfuscatedCode, regExp);
});
});
});
});
});
@@ -0,0 +1,9 @@
function foo() {
if (true) {
if (false) {
var bar = baz();
}
} else {
var bark = hawk();
}
}
+16 -16
View File
@@ -1923,10 +1923,10 @@ eslint-plugin-import@2.22.0:
resolve "^1.17.0"
tsconfig-paths "^3.9.0"
eslint-plugin-jsdoc@28.5.1:
version "28.5.1"
resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-28.5.1.tgz#5a95ee8705af389ba2062a1036be6077b5e62f3f"
integrity sha512-1XSWu8UnGwqO8mX3XKGofffL83VRt00ptq0m5OrTLFDN3At4x+/pJ8YHJONKhGC35TtjskcS9/RR6F9pjQ8c+w==
eslint-plugin-jsdoc@28.6.1:
version "28.6.1"
resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-28.6.1.tgz#c9e9da59d0d3cef4fb45ffb91c0acde43af4e418"
integrity sha512-Z3y7hcNPDuhL339D1KOf9SY8pMAxYxhaG4QLtu3KVn20k/hNF1u6WQv44wvuSCb6OfPJ4say37RUlSNqIjR+mw==
dependencies:
comment-parser "^0.7.5"
debug "^4.1.1"
@@ -2015,10 +2015,10 @@ eslint-visitor-keys@^1.2.0:
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.2.0.tgz#74415ac884874495f78ec2a97349525344c981fa"
integrity sha512-WFb4ihckKil6hu3Dp798xdzSfddwKKU3+nGniKF6HfeW6OLd2OUDEPP7TcHtB5+QXOKg2s6B2DaMPE1Nn/kxKQ==
eslint@7.3.1:
version "7.3.1"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.3.1.tgz#76392bd7e44468d046149ba128d1566c59acbe19"
integrity sha512-cQC/xj9bhWUcyi/RuMbRtC3I0eW8MH0jhRELSvpKYkWep3C6YZ2OkvcvJVUeO6gcunABmzptbXBuDoXsjHmfTA==
eslint@7.4.0:
version "7.4.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.4.0.tgz#4e35a2697e6c1972f9d6ef2b690ad319f80f206f"
integrity sha512-gU+lxhlPHu45H3JkEGgYhWhkR9wLHHEXC9FbWFnTlEkbKyZKWgWRLgf61E8zWmBuI6g5xKBph9ltg3NtZMVF8g==
dependencies:
"@babel/code-frame" "^7.0.0"
ajv "^6.10.0"
@@ -2354,10 +2354,10 @@ fork-ts-checker-notifier-webpack-plugin@3.0.0:
dependencies:
node-notifier "^6.0.0"
fork-ts-checker-webpack-plugin@5.0.5:
version "5.0.5"
resolved "https://registry.yarnpkg.com/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-5.0.5.tgz#4cf77f925008464db4670750a6e9266a4d25a988"
integrity sha512-2vpP+irspLOpZ1pcH3K3yR76MWXVSOYKTGwhwFQGnWjOs5MxzPN+dyDh1tNoaD5DUFP5kr4lGQURE9Qk+DpiUQ==
fork-ts-checker-webpack-plugin@5.0.6:
version "5.0.6"
resolved "https://registry.yarnpkg.com/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-5.0.6.tgz#02af713af02e47b338a3992279209bc65d34c773"
integrity sha512-8h4S7WANr69Resw+tMd5U23xdbVPsT+VOeMKQhUaKhkGeMm0fNf7ObJPP81WG+tsmy4LK4ayIf0JXBY+hy6vMw==
dependencies:
"@babel/code-frame" "^7.8.3"
chalk "^2.4.1"
@@ -5273,10 +5273,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
typescript@3.9.5:
version "3.9.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.5.tgz#586f0dba300cde8be52dd1ac4f7e1009c1b13f36"
integrity sha512-hSAifV3k+i6lEoCJ2k6R2Z/rp/H3+8sdmcn5NrS3/3kE7+RyZXm9aqvxWqjEXHAd8b0pShatpcdMTvEdvAJltQ==
typescript@3.9.6:
version "3.9.6"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.6.tgz#8f3e0198a34c3ae17091b35571d3afd31999365a"
integrity sha512-Pspx3oKAPJtjNwE92YS05HQoY7z2SFyOpHo9MqJor3BXAGNaPUs83CuVp9VISFkSjyRfiTpmKuAYGJB7S7hOxw==
union-value@^1.0.0:
version "1.0.1"