From 97cb54de2e2990f4ba41f7b68c2b5113e40f2c2e Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 2 May 2021 13:04:55 -0700 Subject: [PATCH] classes work, excluding name and constructor/super Former-commit-id: 818d0149312b07191340427e5b2990ae6d0cec94 --- .vscode/tasks.json | 6 +- build.zig | 12 +- src/defines.zig | 2 +- src/js_ast.zig | 4 + src/js_lexer.zig | 1 + src/js_lexer_tables.zig | 1 + src/js_parser.zig | 10331 +---------------------------- src/js_parser/imports.zig | 40 + src/js_parser/js_parser.zig | 10245 ++++++++++++++++++++++++++++ src/js_parser/js_parser_test.zig | 499 ++ src/js_printer.zig | 16 +- src/main.zig | 1 - 12 files changed, 10816 insertions(+), 10342 deletions(-) create mode 100644 src/js_parser/imports.zig create mode 100644 src/js_parser/js_parser.zig create mode 100644 src/js_parser/js_parser_test.zig diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 6091db4f03..93ad44cdcd 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -25,7 +25,7 @@ "label": "test", "type": "process", "command": "zig", - "args": ["test", "${file}"], + "args": ["test", "${file}", "--main-pkg-path", "${workspaceFolder}"], "group": { "kind": "test", @@ -33,7 +33,9 @@ }, "presentation": { "showReuseMessage": false, - "clear": true + "clear": true, + "panel": "new", + "reveal": "always" } } ] diff --git a/build.zig b/build.zig index 6e6e0cfc7e..68981fb504 100644 --- a/build.zig +++ b/build.zig @@ -17,10 +17,20 @@ pub fn build(b: *std.build.Builder) void { } else { exe = b.addExecutable("esdev", "src/main.zig"); } + var cwd_buf = [_]u8{0} ** 4096; + var cwd = std.os.getcwd(&cwd_buf) catch unreachable; + var walker = std.fs.walkPath(std.heap.page_allocator, cwd) catch unreachable; + if (std.builtin.is_test) { + while (walker.next() catch unreachable) |entry| { + if (std.mem.endsWith(u8, entry.basename, "_test.zig")) { + std.debug.print("[test] Added {s}", .{entry.basename}); + _ = b.addTest(entry.path); + } + } + } exe.setTarget(target); exe.setBuildMode(mode); - exe.addLibPath("/usr/local/lib"); exe.install(); diff --git a/src/defines.zig b/src/defines.zig index 740d0d80e7..1a90233548 100644 --- a/src/defines.zig +++ b/src/defines.zig @@ -231,7 +231,7 @@ pub const Define = struct { // "NODE_ENV" if (define.dots.getEntry(tail)) |entry| { for (entry.value) |*part| { - // ["process", "env"] == ["process", "env"] + // ["process", "env"] === ["process", "env"] (if that actually worked) if (arePartsEqual(part.parts, parts)) { part.data = part.data.merge(user_define.value); didFind = true; diff --git a/src/js_ast.zig b/src/js_ast.zig index 42138301b7..321c69c33c 100644 --- a/src/js_ast.zig +++ b/src/js_ast.zig @@ -592,6 +592,10 @@ pub const Symbol = struct { symbols_for_source: [][]Symbol, pub fn get(self: *Map, ref: Ref) ?Symbol { + if (Ref.isSourceIndexNull(ref.source_index)) { + return null; + } + return self.symbols_for_source[ref.source_index][ref.inner_index]; } diff --git a/src/js_lexer.zig b/src/js_lexer.zig index d618500572..9108285aa2 100644 --- a/src/js_lexer.zig +++ b/src/js_lexer.zig @@ -29,6 +29,7 @@ pub const JSONOptions = struct { }; pub const Lexer = struct { + // pub const Error = error{ // UnexpectedToken, // EndOfFile, diff --git a/src/js_lexer_tables.zig b/src/js_lexer_tables.zig index 4b99776995..7a1bf705eb 100644 --- a/src/js_lexer_tables.zig +++ b/src/js_lexer_tables.zig @@ -537,6 +537,7 @@ pub const JSXEntityMap = std.StringHashMap(CodePoint); pub var jsxEntity: JSXEntityMap = undefined; +// There's probably a way to move this to comptime pub fn initJSXEntityMap() !void { jsxEntity = JSXEntityMap.init(alloc.dynamic); // return jsxEntity; diff --git a/src/js_parser.zig b/src/js_parser.zig index f8c787f3bd..d202fcc547 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -1,10330 +1 @@ -const std = @import("std"); -const logger = @import("logger.zig"); -const js_lexer = @import("js_lexer.zig"); -const importRecord = @import("import_record.zig"); -const js_ast = @import("js_ast.zig"); -const options = @import("options.zig"); -const alloc = @import("alloc.zig"); - -const js_printer = @import("js_printer.zig"); -const renamer = @import("renamer.zig"); - -const fs = @import("fs.zig"); -usingnamespace @import("strings.zig"); -usingnamespace @import("ast/base.zig"); -usingnamespace js_ast.G; -usingnamespace @import("defines.zig"); - -const ImportKind = importRecord.ImportKind; -const BindingNodeIndex = js_ast.BindingNodeIndex; - -const StmtNodeIndex = js_ast.StmtNodeIndex; -const ExprNodeIndex = js_ast.ExprNodeIndex; -const ExprNodeList = js_ast.ExprNodeList; -const StmtNodeList = js_ast.StmtNodeList; -const BindingNodeList = js_ast.BindingNodeList; -const assert = std.debug.assert; - -const LocRef = js_ast.LocRef; -const S = js_ast.S; -const B = js_ast.B; -const G = js_ast.G; -const T = js_lexer.T; -const E = js_ast.E; -const Stmt = js_ast.Stmt; -const Expr = js_ast.Expr; -const Binding = js_ast.Binding; -const Symbol = js_ast.Symbol; -const Level = js_ast.Op.Level; -const Op = js_ast.Op; -const Scope = js_ast.Scope; -const locModuleScope = logger.Loc.Empty; - -pub fn ExpressionTransposer(comptime ctx: type, visitor: fn (ptr: *ctx, arg: Expr, state: anytype) Expr) type { - return struct { - context: *Context, - - pub fn init(c: *Context) @This() { - return @This(){ - .context = c, - }; - } - - pub fn maybeTransposeIf(self: *@This(), arg: Expr, state: anytype) Expr { - switch (arg.data) { - .e_if => |ex| { - ex.yes = self.maybeTransposeIf(ex.yes, state); - ex.no = self.maybeTransposeIf(ex.no, state); - return arg; - }, - else => { - return visitor(self.context, arg, state); - }, - } - } - pub const Context = ctx; - }; -} - -pub fn locAfterOp(e: E.Binary) logger.Loc { - if (e.left.loc.start < e.right.loc.start) { - return e.right.loc; - } else { - // handle the case when we have transposed the operands - return e.left.loc; - } -} - -pub const ImportScanner = struct { - stmts: []Stmt = &([_]Stmt{}), - kept_import_equals: bool = false, - removed_import_equals: bool = false, - pub fn scan(p: *P, stmts: []Stmt) !ImportScanner { - var scanner = ImportScanner{}; - var stmts_end: usize = 0; - - for (stmts) |_stmt| { - // zls needs the hint, it seems. - const stmt: Stmt = _stmt; - switch (stmt.data) { - .s_import => |st| { - var record: ImportRecord = p.import_records.items[st.import_record_index]; - - // The official TypeScript compiler always removes unused imported - // symbols. However, we deliberately deviate from the official - // TypeScript compiler's behavior doing this in a specific scenario: - // we are not bundling, symbol renaming is off, and the tsconfig.json - // "importsNotUsedAsValues" setting is present and is not set to - // "remove". - // - // This exists to support the use case of compiling partial modules for - // compile-to-JavaScript languages such as Svelte. These languages try - // to reference imports in ways that are impossible for esbuild to know - // about when esbuild is only given a partial module to compile. Here - // is an example of some Svelte code that might use esbuild to convert - // TypeScript to JavaScript: - // - // - //
- //

Hello {name}!

- // - //
- // - // Tools that use esbuild to compile TypeScript code inside a Svelte - // file like this only give esbuild the contents of the + //
+ //

Hello {name}!

+ // + //
+ // + // Tools that use esbuild to compile TypeScript code inside a Svelte + // file like this only give esbuild the contents of the