-
Notifications
You must be signed in to change notification settings - Fork 336
add support for optional chaining #412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
michaelficarra
merged 17 commits into
estools:master
from
javascript-obfuscator:optional-chaining-support
Aug 5, 2020
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7d19ee6
Added optional chaining support WIP
7f5d20f
Updated optional chaining support to the latest spec
97262e2
Updated tests to match examples from spec
8b56419
Updated tests with correct expectations
5af1f7b
Updated optional chaining parenthesis and added more tests
6e9867a
Updated optional chaining support, added AST comparison tests
c66f83d
Updated Chai to version 4.2.0
be2d6c6
Moved expression object flags to the variable
201b451
Fixed optional CallExpression's. Added tests.
215c838
Fixed optional chaining with literal member expression object
361ffd1
Fixed mixed optional chaining precedence. Added more tests.
afb6f76
Simplified logic of ChainExpression precedence detection
339e49d
Added OptionalChaining precedence and simplified logic
2782a3e
Moved flags from variables to the calls
f672353
Added tests with optional chaining as a part of `NewExpression` `callee`
8f05fb0
Removed redundant trailing parenthesis from NewExpression with ChainE…
eb9cbb0
Reverted logic to keep parenthesis of NewExpression with ChainExpress…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,95 @@ | ||
/* | ||
Copyright (C) 2012-2013 Yusuke Suzuki <utatane.tea@gmail.com> | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the distribution. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY | ||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var fs = require('fs'), | ||
acorn = require('acorn'), | ||
escodegen = require('./loader'), | ||
chai = require('chai'), | ||
chaiExclude = require('chai-exclude'), | ||
expect = chai.expect; | ||
|
||
chai.use(chaiExclude); | ||
|
||
function test(code, expected) { | ||
var tree, actual, actualTree, options; | ||
|
||
options = { | ||
ranges: false, | ||
locations: false, | ||
ecmaVersion: 11 | ||
}; | ||
|
||
tree = acorn.parse(code, options); | ||
|
||
// for UNIX text comment | ||
actual = escodegen.generate(tree); | ||
actualTree = acorn.parse(actual, options); | ||
|
||
expect(actual).to.be.equal(expected); | ||
expect(tree).excludingEvery(['start', 'end']).to.deep.equal(actualTree); | ||
} | ||
|
||
function testMin(code, expected) { | ||
var tree, actual, actualTree, options; | ||
|
||
options = { | ||
ranges: false, | ||
locations: false, | ||
ecmaVersion: 11 | ||
}; | ||
|
||
tree = acorn.parse(code, options); | ||
|
||
// for UNIX text comment | ||
actual = escodegen.generate(tree, { | ||
format: escodegen.FORMAT_MINIFY, | ||
raw: false | ||
}).replace(/[\n\r]$/, '') + '\n'; | ||
actualTree = acorn.parse(actual, options); | ||
|
||
expect(actual).to.be.equal(expected); | ||
expect(tree).excludingEvery(['start', 'end']).to.deep.equal(actualTree); | ||
michaelficarra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
describe('compare acorn es2020 test', function () { | ||
fs.readdirSync(__dirname + '/compare-acorn-es2020').sort().forEach(function(file) { | ||
var code, expected, exp, min; | ||
if (/\.js$/.test(file) && !/expected\.js$/.test(file) && !/expected\.min\.js$/.test(file)) { | ||
it(file, function () { | ||
exp = file.replace(/\.js$/, '.expected.js'); | ||
min = file.replace(/\.js$/, '.expected.min.js'); | ||
code = fs.readFileSync(__dirname + '/compare-acorn-es2020/' + file, 'utf-8'); | ||
expected = fs.readFileSync(__dirname + '/compare-acorn-es2020/' + exp, 'utf-8'); | ||
test(code, expected); | ||
if (fs.existsSync(__dirname + '/compare-acorn-es2020/' + min)) { | ||
expected = fs.readFileSync(__dirname + '/compare-acorn-es2020/' + min, 'utf-8'); | ||
testMin(code, expected); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
/* vim: set sw=4 ts=4 et tw=80 : */ |
This file contains hidden or 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,47 @@ | ||
obj.aaa.bbb; | ||
obj.aaa?.bbb; | ||
obj?.aaa.bbb; | ||
obj?.aaa?.bbb; | ||
obj.aaa.bbb; | ||
obj.aaa?.bbb; | ||
(obj?.aaa).bbb; | ||
(obj?.aaa)?.bbb; | ||
(obj?.aaa).bbb.ccc.ddd; | ||
((obj?.aaa).bbb?.ccc).ddd; | ||
(obj?.aaa)?.bbb; | ||
obj[aaa][bbb]; | ||
obj[aaa]?.[bbb]; | ||
obj?.[aaa][bbb]; | ||
obj?.[aaa]?.[bbb]; | ||
obj[aaa][bbb]; | ||
obj[aaa]?.[bbb]; | ||
(obj?.[aaa])[bbb]; | ||
(obj?.[aaa])?.[bbb]; | ||
obj[aaa][bbb][ccc][ddd]; | ||
((obj?.[aaa])[bbb]?.[ccc])[ddd]; | ||
1?.a; | ||
obj()(); | ||
obj()?.(); | ||
obj?.()(); | ||
obj?.()?.(); | ||
obj()(); | ||
obj()?.(); | ||
(obj?.())(); | ||
(obj?.())?.(); | ||
obj()()()(); | ||
((obj?.())()?.())(); | ||
(a?.b)(); | ||
a?.b(); | ||
a?.b?.(); | ||
(a?.b)?.(); | ||
a?.().b; | ||
(a?.()).b; | ||
a?.b.c(); | ||
(a?.b.c)(); | ||
a.b?.().c; | ||
(a.b?.()).c; | ||
(a.b?.())?.c; | ||
new (a?.b().c)(); | ||
new (a?.b())(); | ||
new (a?.b().c)(); | ||
new (a?.b())(); | ||
michaelficarra marked this conversation as resolved.
Show resolved
Hide resolved
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,47 @@ | ||
obj.aaa.bbb; | ||
obj.aaa?.bbb; | ||
obj?.aaa.bbb; | ||
obj?.aaa?.bbb; | ||
(obj.aaa).bbb; | ||
(obj.aaa)?.bbb; | ||
(obj?.aaa).bbb; | ||
(obj?.aaa)?.bbb; | ||
((obj?.aaa).bbb.ccc).ddd; | ||
((obj?.aaa).bbb?.ccc).ddd; | ||
(obj?.aaa)?.bbb; | ||
obj[aaa][bbb]; | ||
obj[aaa]?.[bbb]; | ||
obj?.[aaa][bbb]; | ||
obj?.[aaa]?.[bbb]; | ||
(obj[aaa])[bbb]; | ||
(obj[aaa])?.[bbb]; | ||
(obj?.[aaa])[bbb]; | ||
(obj?.[aaa])?.[bbb]; | ||
((obj[aaa])[bbb][ccc])[ddd]; | ||
((obj?.[aaa])[bbb]?.[ccc])[ddd]; | ||
1?.a; | ||
obj()(); | ||
obj()?.(); | ||
obj?.()(); | ||
obj?.()?.(); | ||
(obj())(); | ||
(obj())?.(); | ||
(obj?.())(); | ||
(obj?.())?.(); | ||
((obj())()())(); | ||
((obj?.())()?.())(); | ||
(a?.b)(); | ||
a?.b(); | ||
a?.b?.(); | ||
(a?.b)?.(); | ||
a?.().b; | ||
(a?.()).b; | ||
a?.b.c(); | ||
(a?.b.c)(); | ||
a.b?.().c; | ||
(a.b?.()).c; | ||
(a.b?.())?.c; | ||
new (a?.b().c); | ||
new (a?.b()); | ||
new (a?.b().c)(); | ||
new (a?.b())(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.