Fix DotDefine

Former-commit-id: 78fa4c4f87
This commit is contained in:
Jarred Sumner
2021-05-18 14:40:37 -07:00
parent 904bf17a51
commit 7396fae4e2
4 changed files with 24 additions and 14 deletions

2
.vscode/launch.json vendored
View File

@@ -38,7 +38,7 @@
"--resolve=disable",
"--cwd",
"/Users/jarredsumner/Code/esdev/src/test/fixtures",
"label-continue-break-bug.js"
"defines.js"
],
"cwd": "${workspaceFolder}",
"console": "internalConsole"

View File

@@ -180,7 +180,7 @@ pub const Define = struct {
var list = try std.ArrayList(DotDefine).initCapacity(allocator, entry.value.len + 1);
list.appendSliceAssumeCapacity(entry.value);
list.appendAssumeCapacity(DotDefine{
.parts = global[0 .. global.len - 1],
.parts = global[0..global.len],
.data = value_define,
});
@@ -188,7 +188,7 @@ pub const Define = struct {
} else {
var list = try std.ArrayList(DotDefine).initCapacity(allocator, 1);
list.appendAssumeCapacity(DotDefine{
.parts = global[0 .. global.len - 1],
.parts = global[0..global.len],
.data = value_define,
});
@@ -223,13 +223,13 @@ pub const Define = struct {
const tail = user_define.key[last_dot + 1 .. user_define.key.len];
const remainder = user_define.key[0..last_dot];
const count = std.mem.count(u8, remainder, ".") + 1;
var parts = try allocator.alloc(string, count);
var parts = try allocator.alloc(string, count + 1);
var splitter = std.mem.split(remainder, ".");
var i: usize = 0;
while (splitter.next()) |split| : (i += 1) {
parts[i] = split;
}
parts[i] = tail;
var didFind = false;
var initial_values: []DotDefine = &([_]DotDefine{});

View File

@@ -9759,14 +9759,14 @@ pub const P = struct {
}
// // trim empty statements
// if (data.stmts.len == 0) {
// stmts.append(Stmt{ .data = Prefill.Data.SEmpty, .loc = stmt.loc }) catch unreachable;
// return;
// } else if (data.stmts.len == 1 and !statementCaresAboutScope(data.stmts[0])) {
// // Unwrap blocks containing a single statement
// stmts.append(data.stmts[0]) catch unreachable;
// return;
// }
if (data.stmts.len == 0) {
stmts.append(Stmt{ .data = Prefill.Data.SEmpty, .loc = stmt.loc }) catch unreachable;
return;
} else if (data.stmts.len == 1 and !statementCaresAboutScope(data.stmts[0])) {
// Unwrap blocks containing a single statement
stmts.append(data.stmts[0]) catch unreachable;
return;
}
stmts.append(stmt.*) catch unreachable;
return;
},
@@ -9809,6 +9809,7 @@ pub const P = struct {
data.no = p.visitSingleStmt(no, .none);
}
// Trim unnecessary "else" clauses
if (data.no != null and @as(Stmt.Tag, data.no.?.data) == .s_empty) {
data.no = null;
}
@@ -10279,9 +10280,14 @@ pub const P = struct {
switch (expr.data) {
.e_dot => |ex| {
if (parts.len > 1) {
if (ex.optional_chain != null) {
return false;
}
// Intermediates must be dot expressions
const last = parts.len - 1;
return strings.eql(parts[last], ex.name) and ex.optional_chain == null and p.isDotDefineMatch(ex.target, parts[0..last]);
const is_tail_match = strings.eql(parts[last], ex.name);
return is_tail_match and p.isDotDefineMatch(ex.target, parts[0..last]);
}
},
.e_import_meta => |ex| {

4
src/test/fixtures/defines.js vendored Normal file
View File

@@ -0,0 +1,4 @@
if (process.env.NODE_ENV === "development") {
console.log("hi");
} else {
}