Skip to content

Commit

Permalink
Base support for coalescing & logical assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Oct 28, 2023
1 parent 1253421 commit 7eaf0a2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1768,10 +1768,10 @@ var AUTO_NATIVE_LIBRARIES = true;
// versions >= MIN_FIREFOX_VERSION
// are desired to work. Pass -sMIN_FIREFOX_VERSION=majorVersion to drop support
// for Firefox versions older than < majorVersion.
// Firefox ESR 68 was released on July 9, 2019.
// Firefox 79 was released on 2020-07-28.
// MAX_INT (0x7FFFFFFF, or -1) specifies that target is not supported.
// [link]
var MIN_FIREFOX_VERSION = 68;
var MIN_FIREFOX_VERSION = 79;

// Specifies the oldest version of desktop Safari to target. Version is encoded
// in MMmmVV, e.g. 70101 denotes Safari 7.1.1.
Expand Down Expand Up @@ -1810,10 +1810,10 @@ var MIN_EDGE_VERSION = 0x7FFFFFFF;

// Specifies the oldest version of Chrome. E.g. pass -sMIN_CHROME_VERSION=58 to
// drop support for Chrome 57 and older.
// Chrome 75.0.3770 was released on 2019-06-04
// Chrome 85 was released on 2020-08-25.
// MAX_INT (0x7FFFFFFF, or -1) specifies that target is not supported.
// [link]
var MIN_CHROME_VERSION = 75;
var MIN_CHROME_VERSION = 85;

// Specifies minimum node version to target for the generated code. This is
// distinct from the minimum version required run the emscripten compiler.
Expand Down
29 changes: 28 additions & 1 deletion test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -12966,14 +12966,16 @@ def test_es5_transpile(self, args):
# - arrow funcs
# - for..of
# - object.assign
# - nullish coalescing & chaining
# - logical assignment
create_file('es6_library.js', '''\
addToLibrary({
foo: function(arg="hello") {
// Object.assign + let
let obj = Object.assign({}, {prop:1});
err('prop: ' + obj.prop);

// arror funcs + const
// arrow funcs + const
const bar = () => 2;
err('bar: ' + bar());

Expand All @@ -12990,6 +12992,22 @@ def test_es5_transpile(self, args):
};
global['foo'] = obj3;
err('value2: ' + obj3.myMethod());

// Nullish coalescing
var definitely = global['maybe'] ?? {};

// Optional chaining
global['maybe']
?.subObj
?.[key]
?.func
?.();

// Logical assignment
var obj4 = null;
obj4 ??= 0;
obj4 ||= 1;
obj4 &&= 2;
}
});
''')
Expand All @@ -13004,13 +13022,22 @@ def check_for_es6(filename, expect):
self.assertContained(['() => 2', '()=>2'], js)
self.assertContained('const ', js)
self.assertContained('let ', js)
self.assertContained('?.[', js)
self.assertContained('?.(', js)
self.assertContained('??=', js)
self.assertContained('||=', js)
self.assertContained('&&=', js)
else:
self.verify_es5(filename)
self.assertNotContained('foo(arg=', js)
self.assertNotContained('() => 2', js)
self.assertNotContained('()=>2', js)
self.assertNotContained('const ', js)
self.assertNotContained('let ', js)
self.assertNotContained('??', js)
self.assertNotContained('?.', js)
self.assertNotContained('||=', js)
self.assertNotContained('&&=', js)

# Check that under normal circumstances none of these features get
# removed / transpiled.
Expand Down
4 changes: 2 additions & 2 deletions tools/acorn-optimizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ function applyDCEGraphRemovals(ast) {

// Need a parser to pass to acorn.Node constructor.
// Create it once and reuse it.
const stubParser = new acorn.Parser({ecmaVersion: 2020});
const stubParser = new acorn.Parser({ecmaVersion: 2021});

function createNode(props) {
const node = new acorn.Node(stubParser);
Expand Down Expand Up @@ -2008,7 +2008,7 @@ let ast;
try {
ast = acorn.parse(input, {
// Keep in sync with --language_in that we pass to closure in building.py
ecmaVersion: 2020,
ecmaVersion: 2021,
preserveParens: closureFriendly,
onComment: closureFriendly ? sourceComments : undefined,
sourceType: exportES6 ? 'module' : 'script',
Expand Down
2 changes: 1 addition & 1 deletion tools/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ def closure_compiler(filename, advanced=True, extra_closure_args=None):

args = ['--compilation_level', 'ADVANCED_OPTIMIZATIONS' if advanced else 'SIMPLE_OPTIMIZATIONS']
# Keep in sync with ecmaVersion in tools/acorn-optimizer.js
args += ['--language_in', 'ECMASCRIPT_2020']
args += ['--language_in', 'ECMASCRIPT_2021']
# Tell closure not to do any transpiling or inject any polyfills.
# At some point we may want to look into using this as way to convert to ES5 but
# babel is perhaps a better tool for that.
Expand Down
2 changes: 1 addition & 1 deletion tools/unsafe_optimizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function optPassMergeVarInitializationAssignments(ast) {
}

function runOnJsText(js, pretty = false) {
const ast = acorn.parse(js, {ecmaVersion: 2020});
const ast = acorn.parse(js, {ecmaVersion: 2021});

optPassSimplifyModuleInitialization(ast);
optPassRemoveRedundantOperatorNews(ast);
Expand Down

0 comments on commit 7eaf0a2

Please sign in to comment.