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
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ if(${COVERAGE})
)
endif()
endif()
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
if(CMAKE_BUILD_TYPE STREQUAL "Release")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
endif ()
# compile
if (MSVC)
add_compile_options(/Zc:preprocessor)
Expand Down
2 changes: 1 addition & 1 deletion src/babel/babel-arena/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace BabelArena {
approximate_size = codeSize * 10.0;
break;
case static_cast<size_t>(MemoryType::String):
approximate_size = codeSize / 50.0;
approximate_size = codeSize / 10.0;
break;
case static_cast<size_t>(MemoryType::Identifier):
approximate_size = codeSize * 10.0;
Expand Down
30 changes: 19 additions & 11 deletions src/babel/babel-generator/generators/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,25 @@
// NOTE: In directives we can't change escapings,
// because they change the behavior.
// e.g. "us\x65 strict" (\x65 is e) is not a "use strict" directive.

// if (!unescapedDoubleQuoteRE.test(value)) {
// _this.token(`"${value}"`);
// } else if (!unescapedSingleQuoteRE.test(value)) {
// _this.token(`'${value}'`);
// } else {
// throw new Error(
// "Malformed AST: it is not possible to print a directive containing" +
// " both unescaped single and double quotes.",
// );
// }
size_t slash_count = 0;
bool is_double_escaped = false;
bool is_single_escaped = false;
for (auto ch: value) {
if (ch == u'\\') { slash_count++; }
else if (ch == u'\'') { is_single_escaped |= slash_count % 2 == 1; }
else if (ch == u'\"') { is_double_escaped |= slash_count % 2 == 1; }
else { slash_count = 0; }
}
if (!is_double_escaped) {
_this.token(BASE::UStringViewArray<3>{u"\"", value, u"\""});
} else if (!is_single_escaped) {
_this.token(BASE::UStringViewArray<3>{u"\'", value, u"\'"});

Check warning on line 92 in src/babel/babel-generator/generators/base.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-generator/generators/base.hpp#L92

Added line #L92 was not covered by tests
} else {
throw new BASE::JavaScript::Error(
u"Malformed AST: it is not possible to print a directive containing"
u" both unescaped single and double quotes."
);

Check warning on line 97 in src/babel/babel-generator/generators/base.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-generator/generators/base.hpp#L97

Added line #L97 was not covered by tests
}
}

DEF_GENERATOR(InterpreterDirective, node, _) {
Expand Down
4 changes: 2 additions & 2 deletions src/babel/babel-generator/generators/jsx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
auto raw = _this.getPossibleRaw(node);

if (raw != std::nullopt) {
_this.token(*raw, true);
_this.template token<true>(*raw);
} else {
_this.token(node->value, true);
_this.template token<true>(node->value);

Check warning on line 57 in src/babel/babel-generator/generators/jsx.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-generator/generators/jsx.hpp#L57

Added line #L57 was not covered by tests
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/babel/babel-generator/generators/methods.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ namespace BabelGenerator::Generators {
_this.print(key);
}

// if (
// // @ts-expect-error optional is not in ObjectMethod
// node->optional
// ) {
// // TS
// _this.token(u"?");
// }
if (
// @ts-expect-error optional is not in ObjectMethod
node->isOptional()
) {
// TS
_this.token(u"?");
}

_params(_this, node, node->computed && node->key()->type() != Node::Type::StringLiteral ? nullptr : node->key(), nullptr);
}
Expand Down
4 changes: 2 additions & 2 deletions src/babel/babel-generator/generators/template-literals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace BabelGenerator::Generators {
bool not_last = i + 1 < quasis->size();
auto value = BASE::UStringViewArray<3>{i == 0 ? u"`" : u"}", quasis->at(i)->value.raw, not_last ? u"${" : u"`"};
if (not_last) {
_this.token(value, true);
_this.template token<true>(value);
_this.print(node->expressions->at(i));

// In Babel 7 we have indivirual tokens for ${ and }, so the automatic
Expand All @@ -35,7 +35,7 @@ namespace BabelGenerator::Generators {
// if (token) _this._catchUpTo(token.loc.start);
// }
} else {
_this.token(value, true);
_this.template token<true>(value);
}
}
}
Expand Down
29 changes: 16 additions & 13 deletions src/babel/babel-generator/generators/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../printer.hpp"
#include "./index.h"
#include "./methods.hpp"
#include "../../../npm_modules/jsesc/jsesc.hpp"

namespace BabelGenerator::Generators {
using namespace BabelParser;
Expand Down Expand Up @@ -245,17 +246,20 @@ namespace BabelGenerator::Generators {

DEF_GENERATOR(NumericLiteral, node, _) {
auto raw = _this.getPossibleRaw(node);
// const opts = this.format.jsescOption;
auto const& opts = _this.format.jsescOption;
auto value = node->value;
// const str = value + "";
// if (opts.numbers) {
// this.number(jsesc(value, opts), value);
// } else
if (raw == std::nullopt) {
// const str = value + "";
if (opts.numbers) {
Jsesc::jsesc(_this.allocator, *value, opts, [&](auto&& str){
_this.number(str, value);
});
} else if (raw == std::nullopt) {
unreachable();
// _this.number(str, value); // normalize
// } else if (this.format.minified) {
// _this.number(raw->size() < str.length ? raw : str, value);
// _this.number(str, value); // normalize
} else if (_this.format.minified) {
value->withString([&](BASE::UStringView str){
_this.number(raw->size() < str.size() ? *raw : str, value);
});
} else {
_this.number(*raw, value);
}
Expand All @@ -267,10 +271,9 @@ namespace BabelGenerator::Generators {
_this.token(*raw);
return;
}
//
// const val = jsesc(node.value, this.format.jsescOption);
//
// this.token(val);

auto val = Jsesc::jsesc(_this.allocator, node->value, _this.format.jsescOption);
_this.token(val);
}

DEF_GENERATOR(BigIntLiteral, node, _) {
Expand Down
24 changes: 12 additions & 12 deletions src/babel/babel-generator/generators/typescript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ namespace BabelGenerator::Generators {
_this.print(node->indexType);
_this.token(u"]");
}

inline void tokenIfPlusMinus(Printer::Printer& self, Node::TypeScriptReadonlyOptionalFlag::Value tok);
DEF_GENERATOR(TSMappedType, node, _) {
auto nameType = node->nameType;
auto optional = node->optional;
Expand All @@ -411,7 +411,7 @@ namespace BabelGenerator::Generators {
{
_this.space();
if (readonly) {
// tokenIfPlusMinus(this, readonly);
tokenIfPlusMinus(_this, readonly);
_this.word(u"readonly");
_this.space();
}
Expand All @@ -421,7 +421,7 @@ namespace BabelGenerator::Generators {
_this.word(node->key->name->string());
} else {
// @ts-ignore(Babel 7 vs Babel 8) Babel 7 AST shape
// _this.word(node->typeParameter->name);
_this.word(node->typeParameter->name_->name->string());
}
_this.space();
_this.word(u"in");
Expand All @@ -431,7 +431,7 @@ namespace BabelGenerator::Generators {
_this.print(node->constraint);
} else {
// @ts-ignore(Babel 7 vs Babel 8) Babel 7 AST shape
// _this.print(node->typeParameter.constraint);
_this.print(node->typeParameter->constraint);
}
if (nameType) {
_this.space();
Expand All @@ -441,7 +441,7 @@ namespace BabelGenerator::Generators {
}
_this.token(u"]");
if (optional) {
// tokenIfPlusMinus(this, optional);
tokenIfPlusMinus(_this, optional);
_this.token(u"?");
}
if (typeAnnotation) {
Expand All @@ -455,12 +455,12 @@ namespace BabelGenerator::Generators {
_this.token(u"}");
}

// function tokenIfPlusMinus(self: Printer, tok: true | "+" | "-") {
// if (tok !== true) {
// self.token(tok);
// }
// }
//
inline void tokenIfPlusMinus(Printer::Printer& self, Node::TypeScriptReadonlyOptionalFlag::Value tok) {
if (tok != Node::TypeScriptReadonlyOptionalFlag::True) {
self.token(toString(tok));
}
}

DEF_GENERATOR(TSLiteralType, node, _) { _this.print(node->literal); }

template<typename T>
Expand Down Expand Up @@ -763,7 +763,7 @@ namespace BabelGenerator::Generators {
printModifiersList(
_this, node,
Node::Modifier::Value{
(isField && node->modifier.isDeclare() ? Node::Modifier::Declare : Node::Modifier::Undefined) | node->modifier.getAccessibility()
(isField && node->isDeclare() ? Node::Modifier::Declare : Node::Modifier::Undefined) | node->modifier.getAccessibility()
}
);
if (node->isStatic()) {
Expand Down
9 changes: 7 additions & 2 deletions src/babel/babel-generator/index.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#pragma once

#include "../../base/unicode.hpp"
#include "../../base/allocator.hpp"
#include "../../npm_modules/jsesc/jsesc.hpp"
#include "../babel-types/ast-types/generated/enum.hpp"
#include "../babel-types/ast-types/generated/index.hpp"

namespace BabelArena {
class Memory;
}
namespace BabelGenerator {

struct GeneratorResult {
Expand Down Expand Up @@ -107,7 +112,7 @@ namespace BabelGenerator {
/**
* Options for outputting jsesc representation.
*/
// jsescOption?: jsescOptions;
std::optional<Jsesc::PartialOption> jsescOption;

/**
* For use with the recordAndTuple token.
Expand All @@ -130,5 +135,5 @@ namespace BabelGenerator {
std::optional<BabelTypes::Enum::FormatImportAttributesKeyword::Value> importAttributesKeyword;
};
GeneratorResult
generate(PTR(BabelParser::Node::File) ast, GeneratorOptions opts = GeneratorOptions{}, std::optional<BASE::UStringView> code = std::nullopt);
generate(BabelArena::Memory& allocator, PTR(BabelParser::Node::File) ast, GeneratorOptions opts = GeneratorOptions{}, std::optional<BASE::UStringView> code = std::nullopt);
} // namespace BabelGenerator
16 changes: 8 additions & 8 deletions src/babel/babel-generator/index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,18 @@ namespace BabelGenerator {
.adjustMultilineComment = true,
.style = u" ",
},
// jsescOption: {
// quotes: "double",
// wrap: true,
// minimal: process.env.BABEL_8_BREAKING ? true : false,
// ...opts.jsescOption,
// },
.jsescOption = Jsesc::Option{
.quotes = u'"',
.wrap = true,
.minimal = BASE::process::env.BABEL_8_BREAKING ? true : false,
}.assign(opts.jsescOption.value_or(Jsesc::PartialOption{})),
.topicToken = opts.topicToken,
.importAttributesKeyword = opts.importAttributesKeyword,
};

if (!BASE::process::env.BABEL_8_BREAKING) {
format.decoratorsBeforeExport = opts.decoratorsBeforeExport;
// format.jsescOption.json = opts.jsonCompatibleStrings;
format.jsescOption.json = opts.jsonCompatibleStrings.value_or(false);
format.recordAndTupleSyntaxType = opts.recordAndTupleSyntaxType.value_or(RecordAndTupleSyntaxType::hash);
}

Expand Down Expand Up @@ -148,6 +147,7 @@ namespace BabelGenerator {
* @returns - an object containing the output code and source map.
*/
GeneratorResult generate(
BabelArena::Memory& allocator,
PTR(Node::File) ast, GeneratorOptions opts, std::optional<BASE::UStringView> code
// code?: string | { [filename: string]: string },
) {
Expand All @@ -160,7 +160,7 @@ namespace BabelGenerator {
// (ast as any).tokens,
// typeof code === "string" ? code : null,
// );
Printer::Printer printer(format, code);
Printer::Printer printer(*allocator.string(), format, code);

return printer.generate(ast);
}
Expand Down
59 changes: 30 additions & 29 deletions src/babel/babel-generator/node/index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ namespace BabelGenerator::Nodes {

// const expandedWhitespaceNodes = expandAliases(whitespace.nodes);

// function isOrHasCallExpression(node: t.Node): boolean {
// if (isCallExpression(node)) {
// return true;
// }
inline bool isOrHasCallExpression(PTR(Node::Node) node) {
if (BabelTypes::isCallExpression(node)) {
return true;
}

// return isMemberExpression(node) && isOrHasCallExpression(node.object);
// }
return BabelTypes::isMemberExpression(node) && isOrHasCallExpression(NODEAS(node, MemberExpression)->object);
}

// export function needsWhitespace(
// node: t.Node,
Expand Down Expand Up @@ -57,6 +57,7 @@ namespace BabelGenerator::Nodes {
// export function needsWhitespaceAfter(node: t.Node, parent: t.Node) {
// return needsWhitespace(node, parent, 2);
// }
inline bool isDecoratorMemberExpression(PTR(Node::Node) node);

inline bool needsParens(
PTR(Node::Node) node, PTR(Node::Node) parent, Node::Type nodeType, TokenContext::Type tokenContext = 0, bool inForInit = false
Expand All @@ -65,17 +66,17 @@ namespace BabelGenerator::Nodes {
if (!parent)
return false;

if (parent->type() == Node::Type::NewExpression && NODEAS(parent, NewExpression)->callee == node) {
// if (isOrHasCallExpression(node)) return true;
if (parent->type() == Node::Type::NewExpression && NODEAS(parent, NewExpression)->callee == node) [[unlikely]] {
if (isOrHasCallExpression(node)) return true;
}

// if (isDecorator(parent)) {
// return (
// !isDecoratorMemberExpression(node) &&
// !(isCallExpression(node) && isDecoratorMemberExpression(node.callee)) &&
// !isParenthesizedExpression(node)
// );
// }
if (BabelTypes::isDecorator(parent)) [[unlikely]] {
return (
!isDecoratorMemberExpression(node) &&
!(BabelTypes::isCallExpression(node) && isDecoratorMemberExpression(NODEAS(node, CallExpression)->callee)) &&
!BabelTypes::isParenthesizedExpression(node)
);
}

auto method = parenthesesMethods[nodeType];
if (method) {
Expand All @@ -85,20 +86,20 @@ namespace BabelGenerator::Nodes {
}
}

// function isDecoratorMemberExpression(node: t.Node): boolean {
// switch (node.type) {
// case "Identifier":
// return true;
// case "MemberExpression":
// return (
// !node.computed &&
// node.property.type === "Identifier" &&
// isDecoratorMemberExpression(node.object)
// );
// default:
// return false;
// }
// }
inline bool isDecoratorMemberExpression(PTR(Node::Node) node) {
switch (node->type()) {
case Node::Type::Identifier:
return true;
case Node::Type::MemberExpression:
return (
!NODEAS(node, MemberExpression)->computed &&
NODEAS(node, MemberExpression)->property->type() == Node::Type::Identifier &&
isDecoratorMemberExpression(NODEAS(node, MemberExpression)->object)
);
default:
return false;
}
}

// export function isLastChild(parent: t.Node, child: t.Node) {
// const visitorKeys = VISITOR_KEYS[parent.type];
Expand Down
Loading
Loading