Skip to content
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

Improve auto display name #1

Open
wants to merge 2 commits into
base: 7.0
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions packages/babel-generator/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function findCommonStringDelimiter(code, tokens) {
return DEFAULT_STRING_DELIMITER;
}

const occurences = {
const occurrences = {
single: 0,
double: 0,
};
Expand All @@ -111,15 +111,15 @@ function findCommonStringDelimiter(code, tokens) {

const raw = code.slice(token.start, token.end);
if (raw[0] === "'") {
occurences.single++;
occurrences.single++;
} else {
occurences.double++;
occurrences.double++;
}

checked++;
if (checked >= 3) break;
}
if (occurences.single > occurences.double) {
if (occurrences.single > occurrences.double) {
return "single";
} else {
return "double";
Expand Down
27 changes: 27 additions & 0 deletions packages/babel-traverse/test/evaluation.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ describe("evaluation", function () {
);
});

it("should evaluate template literals", function () {
assert.strictEqual(
getPath("var x = 8; var y = 1; var z = `value is ${x >>> y}`")
.get("body.2.declarations.0.init").evaluate().value,
"value is 4"
);
});

it("should evaluate member expressions", function () {
assert.strictEqual(
getPath("var x = 'foo'.length")
.get("body.0.declarations.0.init").evaluate().value,
3
);
const member_expr = getPath("var x = Math.min(2,Math.max(3,4));var y = Math.random();");
const eval_member_expr = member_expr.get("body.0.declarations.0.init").evaluate();
const eval_invalid_call = member_expr.get("body.1.declarations.0.init").evaluate();
assert.strictEqual(eval_member_expr.value, 2);
assert.strictEqual(eval_invalid_call.confident, false);
});

it("it should not deopt vars in different scope", function () {
const input = "var a = 5; function x() { var a = 5; var b = a + 1; } var b = a + 2";
assert.strictEqual(
Expand Down Expand Up @@ -88,6 +109,12 @@ describe("evaluation", function () {
getPath(constExample).get("body.1.consequent.body.1").evaluate().value,
false
);
const test_alternate = "var y = (3 < 4)? 3 + 4: 3 + 4;";
assert.strictEqual(
getPath(test_alternate)
.get("body.0.declarations.0.init.alternate").evaluate().value,
7
);
});

it("should deopt ids that are referenced before the bindings", function () {
Expand Down