Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`convert-esmodule can convert + + 1`] = `
"\\"use strict\\";
c = (10, + +15);
"
`;

exports[`convert-esmodule can convert class default exports 1`] = `
"\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,39 @@ import * as astring from 'astring';
import * as meriyah from 'meriyah';
import { Identifier } from 'meriyah/dist/estree';

// Enables parenthesis regardless of precedence
const NEEDS_PARENTHESES = 17;
const EXPRESSIONS_PRECEDENCE = {
// Definitions
ArrayExpression: 20,
TaggedTemplateExpression: 20,
ThisExpression: 20,
Identifier: 20,
Literal: 18,
TemplateLiteral: 20,
Super: 20,
SequenceExpression: 20,
// Operations
MemberExpression: 19,
CallExpression: 19,
NewExpression: 19,
// Other definitions
ArrowFunctionExpression: NEEDS_PARENTHESES,
ClassExpression: NEEDS_PARENTHESES,
FunctionExpression: NEEDS_PARENTHESES,
ObjectExpression: NEEDS_PARENTHESES,
// Other operations
UpdateExpression: 16,
UnaryExpression: 15,
BinaryExpression: 14,
LogicalExpression: 13,
ConditionalExpression: 4,
AssignmentExpression: 3,
AwaitExpression: 2,
YieldExpression: 2,
RestElement: 1,
};

/**
* Add support for next syntax
*/
Expand Down Expand Up @@ -30,4 +63,32 @@ export const customGenerator = {
this[node.source.type](node.source, state);
state.write(')');
},
UnaryExpression(
node: meriyah.ESTree.UnaryExpression,
state: { write(s: string): void }
) {
if (node.prefix) {
state.write(node.operator);
if (
node.operator.length > 1 ||
node.argument.type === 'UnaryExpression'
) {
state.write(' ');
}
if (
EXPRESSIONS_PRECEDENCE[node.argument.type] <
EXPRESSIONS_PRECEDENCE.UnaryExpression
) {
state.write('(');
this[node.argument.type](node.argument, state);
state.write(')');
} else {
this[node.argument.type](node.argument, state);
}
} else {
// FIXME: This case never occurs
this[node.argument.type](node.argument, state);
state.write(node.operator);
}
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,12 @@ describe('convert-esmodule', () => {

expect(convertEsModule(code)).toMatchSnapshot();
});

it('can convert + +', () => {
const code = `
c = (10.0, + +(15))
`;

expect(convertEsModule(code)).toMatchSnapshot();
});
});