diff options
author | 2023-05-24 18:33:28 -0700 | |
---|---|---|
committer | 2023-05-24 18:33:28 -0700 | |
commit | 49729341895752f8817f444e9c87eaf4affa68bf (patch) | |
tree | 9c1fc60a6379e4bff8ab62ac27f5f2df1953a427 /src/js_parser.zig | |
parent | d73bce058ab35b39838334f47e56fcea63db8e59 (diff) | |
download | bun-49729341895752f8817f444e9c87eaf4affa68bf.tar.gz bun-49729341895752f8817f444e9c87eaf4affa68bf.tar.zst bun-49729341895752f8817f444e9c87eaf4affa68bf.zip |
fix setting `jsxImportSource`, `jsxFactory`, and `jsxFragmentFactory` (#3057)
* default automatic, merge jsx flags from multiple tsconfigs
* use entire package name
* some tests
* more tests
Diffstat (limited to 'src/js_parser.zig')
-rw-r--r-- | src/js_parser.zig | 94 |
1 files changed, 24 insertions, 70 deletions
diff --git a/src/js_parser.zig b/src/js_parser.zig index 91631d1f6..a0f4defae 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -111,7 +111,7 @@ const JSXImport = enum { jsxs: ?LocRef = null, Fragment: ?LocRef = null, createElement: ?LocRef = null, - factory_name: []const u8 = "createElement", + factory_name: []const u8 = "React.createElement", fragment_name: []const u8 = "Fragment", pub fn get(this: *const Symbols, name: []const u8) ?Ref { @@ -133,67 +133,31 @@ const JSXImport = enum { }; } - const Runtime = struct { - pub const full: []const string = &[_]string{ "jsx", "jsxs" }; - pub const jsxs_: []const string = &[_]string{"jsxs"}; - pub const jsx_: []const string = &[_]string{"jsx"}; - }; - - const DevRuntime = struct { - pub const full: []const string = &[_]string{ "jsxDEV", "jsxs" }; - pub const jsxs_: []const string = &[_]string{"jsxs"}; - pub const jsx_: []const string = &[_]string{"jsxDEV"}; - }; - pub fn runtimeImportNames(this: *const Symbols) []const string { + pub fn runtimeImportNames(this: *const Symbols, buf: *[3]string) []const string { + var i: usize = 0; if (this.jsxDEV != null) { std.debug.assert(this.jsx == null); // we should never end up with this in the same file - - if (this.jsxs != null) - return DevRuntime.full; - - return DevRuntime.jsx_; + buf[0] = "jsxDEV"; + i += 1; } - if (this.jsx != null and this.jsxs != null) - return Runtime.full; - - if (this.jsxs != null) - return Runtime.jsxs_; - - if (this.jsx != null) - return Runtime.jsx_; - - return &[_]string{}; - } - - const Legacy = struct { - pub const full: []const string = &[_]string{ "createElement", "Fragment" }; - pub const createElement_: []const string = &[_]string{"createElement"}; - pub const Fragment_: []const string = &[_]string{"Fragment"}; - }; - - pub fn legacyImportNames(this: *const Symbols, jsx: *const options.JSX.Pragma, buf: *[2]string) []const string { - _ = jsx; - if (this.Fragment != null and this.createElement != null) { - buf[0..2].* = .{ - this.factory_name, - this.fragment_name, - }; - return buf[0..2]; + if (this.jsx != null) { + std.debug.assert(this.jsxDEV == null); // we should never end up with this in the same file + buf[0] = "jsx"; + i += 1; } - if (this.createElement != null) { - buf[0] = - this.factory_name; - return buf[0..1]; + if (this.jsxs != null) { + buf[i] = "jsxs"; + i += 1; } if (this.Fragment != null) { - buf[0] = this.fragment_name; - return buf[0..1]; + buf[i] = this.fragment_name; + i += 1; } - return &[_]string{}; + return buf[0..i]; } }; }; @@ -4199,10 +4163,9 @@ pub const Parser = struct { } // handle new way to do automatic JSX imports which fixes symbol collision issues - if (p.options.jsx.parse and p.options.features.auto_import_jsx) { - var legacy_import_names_buf = [2]string{ "", "" }; - const runtime_import_names = p.jsx_imports.runtimeImportNames(); - const legacy_import_names = p.jsx_imports.legacyImportNames(&p.options.jsx, &legacy_import_names_buf); + if (p.options.jsx.parse and p.options.features.auto_import_jsx and p.options.jsx.runtime == .automatic) { + var buf = [3]string{ "", "", "" }; + const runtime_import_names = p.jsx_imports.runtimeImportNames(&buf); if (runtime_import_names.len > 0) { p.generateImportStmt( @@ -4215,18 +4178,6 @@ pub const Parser = struct { false, ) catch unreachable; } - - if (legacy_import_names.len > 0) { - p.generateImportStmt( - p.options.jsx.classic_import_source, - legacy_import_names, - &before, - &p.jsx_imports, - null, - "", - false, - ) catch unreachable; - } } var parts_slice: []js_ast.Part = &([_]js_ast.Part{}); @@ -6466,7 +6417,7 @@ fn NewParser_( } if (p.lexer.jsx_pragma.jsxImportSource()) |import_source| { - p.options.jsx.classic_import_source = options.JSX.Pragma.parsePackageName(import_source.text); + p.options.jsx.classic_import_source = import_source.text; p.options.jsx.package_name = p.options.jsx.classic_import_source; p.options.jsx.setImportSource(p.allocator); } @@ -6557,8 +6508,11 @@ fn NewParser_( // "Foo.Bar.createElement" becomes: // import { Bar } from 'foo'; // Usages become Bar.createElement - if (p.options.jsx.fragment.len > 0) - p.jsx_imports.fragment_name = p.options.jsx.fragment[if (p.options.jsx.fragment.len > 1) 1 else 0]; + + if (p.options.jsx.runtime == .classic) { + if (p.options.jsx.fragment.len > 0) + p.jsx_imports.fragment_name = p.options.jsx.fragment[if (p.options.jsx.fragment.len > 1) 1 else 0]; + } if (p.options.jsx.factory.len > 0) p.jsx_imports.factory_name = p.options.jsx.factory[if (p.options.jsx.factory.len > 1) 1 else 0]; |