aboutsummaryrefslogtreecommitdiff
path: root/src/resolver
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-05-12 13:00:25 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-05-12 13:00:25 -0700
commitc09d7cf83905111ef7af9ee5bbd523f56664395c (patch)
tree84fed91f8f6522594b109c09140f69d9c39ff598 /src/resolver
parentf8131f42bcd039964586cbf3bd019dc9a449c438 (diff)
downloadbun-c09d7cf83905111ef7af9ee5bbd523f56664395c.tar.gz
bun-c09d7cf83905111ef7af9ee5bbd523f56664395c.tar.zst
bun-c09d7cf83905111ef7af9ee5bbd523f56664395c.zip
That's all the errors??
Former-commit-id: f9a74df73d2831bfdd8e6f1c0e5c999ea300fc0d
Diffstat (limited to 'src/resolver')
-rw-r--r--src/resolver/package_json.zig30
-rw-r--r--src/resolver/resolve_path.zig2
-rw-r--r--src/resolver/resolver.zig89
-rw-r--r--src/resolver/tsconfig_json.zig86
4 files changed, 109 insertions, 98 deletions
diff --git a/src/resolver/package_json.zig b/src/resolver/package_json.zig
index 3bab16ef2..68d6bacb1 100644
--- a/src/resolver/package_json.zig
+++ b/src/resolver/package_json.zig
@@ -44,28 +44,23 @@ pub const PackageJSON = struct {
//
browser_map: BrowserMap,
- pub fn parse(r: *resolver.Resolver, input_path: string) ?*PackageJSON {
- if (!has_set_default_main_fields) {
- has_set_default_main_fields = true;
- }
-
+ pub fn parse(r: *resolver.Resolver, input_path: string) ?PackageJSON {
const parts = [_]string{ input_path, "package.json" };
const package_json_path = std.fs.path.join(r.allocator, &parts) catch unreachable;
errdefer r.allocator.free(package_json_path);
- const entry: *r.caches.fs.Entry = try r.caches.fs.readFile(r.fs, input_path) catch |err| {
- r.log.addErrorFmt(null, .empty, r.allocator, "Cannot read file \"{s}\": {s}", .{ r.prettyPath(fs.Path.init(input_path)), @errorName(err) }) catch unreachable;
+ const entry = r.caches.fs.readFile(r.fs, input_path) catch |err| {
+ r.log.addErrorFmt(null, logger.Loc.Empty, r.allocator, "Cannot read file \"{s}\": {s}", .{ r.prettyPath(fs.Path.init(input_path)), @errorName(err) }) catch unreachable;
return null;
};
- if (r.debug_logs) |debug| {
+ if (r.debug_logs) |*debug| {
debug.addNoteFmt("The file \"{s}\" exists", .{package_json_path}) catch unreachable;
}
- const key_path = fs.Path.init(allocator.dupe(package_json_path) catch unreachable);
+ const key_path = fs.Path.init(r.allocator.dupe(u8, package_json_path) catch unreachable);
- var json_source = logger.Source.initPathString(key_path);
- json_source.contents = entry.contents;
+ var json_source = logger.Source.initPathString(key_path.text, entry.contents);
json_source.path.pretty = r.prettyPath(json_source.path);
const json: js_ast.Expr = (r.caches.json.parseJSON(r.log, json_source, r.allocator) catch |err| {
@@ -77,8 +72,9 @@ pub const PackageJSON = struct {
var package_json = PackageJSON{
.source = json_source,
+ .module_type = .unknown,
.browser_map = BrowserMap.init(r.allocator),
- .main_fields_map = MainFieldMap.init(r.allocator),
+ .main_fields = MainFieldMap.init(r.allocator),
};
if (json.getProperty("type")) |type_json| {
@@ -110,7 +106,7 @@ pub const PackageJSON = struct {
if (json.getProperty(main)) |main_json| {
const expr: js_ast.Expr = main_json.expr;
- if ((main_json.getString(r.allocator) catch null)) |str| {
+ if ((expr.getString(r.allocator))) |str| {
if (str.len > 0) {
package_json.main_fields.put(main, str) catch unreachable;
}
@@ -132,13 +128,13 @@ pub const PackageJSON = struct {
// },
//
if (json.getProperty("browser")) |browser_prop| {
- switch (browser_prop.data) {
+ switch (browser_prop.expr.data) {
.e_object => |obj| {
// The value is an object
// Remap all files in the browser field
for (obj.properties) |prop| {
- var _key_str = (prop.key orelse continue).getString(r.allocator) catch unreachable;
+ var _key_str = (prop.key orelse continue).getString(r.allocator) orelse continue;
const value: js_ast.Expr = prop.value orelse continue;
// Normalize the path so we can compare against it without getting
@@ -155,7 +151,7 @@ pub const PackageJSON = struct {
switch (value.data) {
.e_string => |str| {
// If this is a string, it's a replacement package
- package_json.browser_map.put(key, str) catch unreachable;
+ package_json.browser_map.put(key, str.string(r.allocator) catch unreachable) catch unreachable;
},
.e_boolean => |boolean| {
if (!boolean.value) {
@@ -163,7 +159,7 @@ pub const PackageJSON = struct {
}
},
else => {
- r.log.addWarning("Each \"browser\" mapping must be a string or boolean", value.loc) catch unreachable;
+ r.log.addWarning(&json_source, value.loc, "Each \"browser\" mapping must be a string or boolean") catch unreachable;
},
}
}
diff --git a/src/resolver/resolve_path.zig b/src/resolver/resolve_path.zig
index 78f00cf96..f639bff1b 100644
--- a/src/resolver/resolve_path.zig
+++ b/src/resolver/resolve_path.zig
@@ -25,7 +25,7 @@ pub fn resolvePath(buffer: []u8, src_path: []const u8) ?[]u8 {
}
} else {
if (end + segment.len + 1 > buffer.len)
- return error.BufferTooSmall;
+ return null;
const start = end;
buffer[end] = '/';
diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig
index 340648399..88bc13cd5 100644
--- a/src/resolver/resolver.zig
+++ b/src/resolver/resolver.zig
@@ -34,7 +34,7 @@ pub const DirInfo = struct {
entries: fs.FileSystem.DirEntry,
has_node_modules: bool = false, // Is there a "node_modules" subdirectory?
package_json: ?*PackageJSON = null, // Is there a "package.json" file?
- ts_config_json: ?*TSConfigJSON = null, // Is there a "tsconfig.json" file in this directory or a parent directory?
+ tsconfig_json: ?*TSConfigJSON = null, // Is there a "tsconfig.json" file in this directory or a parent directory?
abs_real_path: string = "", // If non-empty, this is the real absolute path resolving any symlinks
};
@@ -331,7 +331,7 @@ pub const Resolver = struct {
// First, check path overrides from the nearest enclosing TypeScript "tsconfig.json" file
if ((r.dirInfoCached(source_dir) catch null)) |_dir_info| {
const dir_info: *DirInfo = _dir_info;
- if (dir_info.ts_config_json) |tsconfig| {
+ if (dir_info.tsconfig_json) |tsconfig| {
if (tsconfig.paths.count() > 0) {
const res = r.matchTSConfigPaths(tsconfig, import_path, kind);
return Result{ .path_pair = res.path_pair, .diff_case = res.diff_case };
@@ -384,27 +384,23 @@ pub const Resolver = struct {
const entry = try r.caches.fs.readFile(r.fs, file);
const key_path = Path.init(file);
- const source = logger.Source{
- .key_path = key_path,
- .pretty_path = r.prettyPath(key_path),
- .contents = entry.contents,
- };
- const file_dir = std.fs.path.dirname(file);
+ const source = logger.Source.initPathString(key_path.text, entry.contents);
+ const file_dir = std.fs.path.dirname(file) orelse return null;
- var result = try TSConfigJSON.parse(r.allocator, r.log, r.opts, r.caches.json) orelse return null;
+ var result = (try TSConfigJSON.parse(r.allocator, r.log, source, &r.caches.json)) orelse return null;
if (result.base_url) |base| {
// this might leak
if (!std.fs.path.isAbsolute(base)) {
- var paths = [_]string{ file_dir, base };
- result.base_url = std.fs.path.join(r.allocator, paths) catch unreachable;
+ const paths = [_]string{ file_dir, base };
+ result.base_url = std.fs.path.join(r.allocator, &paths) catch unreachable;
}
}
if (result.paths.count() > 0 and (result.base_url_for_paths.len == 0 or !std.fs.path.isAbsolute(result.base_url_for_paths))) {
// this might leak
- var paths = [_]string{ file_dir, base };
- result.base_url_for_paths = std.fs.path.join(r.allocator, paths) catch unreachable;
+ const paths = [_]string{ file_dir, result.base_url.? };
+ result.base_url_for_paths = std.fs.path.join(r.allocator, &paths) catch unreachable;
}
return result;
@@ -416,7 +412,10 @@ pub const Resolver = struct {
}
pub fn parsePackageJSON(r: *Resolver, file: string) !?*PackageJSON {
- return try PackageJSON.parse(r, file);
+ const pkg = PackageJSON.parse(r, file) orelse return null;
+ var _pkg = try r.allocator.create(PackageJSON);
+ _pkg.* = pkg;
+ return _pkg;
}
pub fn isPackagePath(path: string) bool {
@@ -504,7 +503,7 @@ pub const Resolver = struct {
if (!strings.eqlComptime(base, "node_modules")) {
if (entries.get("node_modules")) |entry| {
// the catch might be wrong!
- info.has_node_modules = (entry.entry.kind(rfs) catch .file) == .dir;
+ info.has_node_modules = (entry.entry.kind(rfs)) == .dir;
}
}
@@ -514,19 +513,21 @@ pub const Resolver = struct {
// Make sure "absRealPath" is the real path of the directory (resolving any symlinks)
if (!r.opts.preserve_symlinks) {
- if (parent_info.entries.get(base)) |entry| {
+ if (parent_info.entries.get(base)) |lookup| {
+ const entry = lookup.entry;
+
var symlink = entry.symlink(rfs);
if (symlink.len > 0) {
- if (r.debug_logs) |logs| {
- try logs.addNote(std.fmt.allocPrint(r.allocator, "Resolved symlink \"{s}\" to \"{s}\"", .{ path, symlink }));
+ if (r.debug_logs) |*logs| {
+ try logs.addNote(std.fmt.allocPrint(r.allocator, "Resolved symlink \"{s}\" to \"{s}\"", .{ path, symlink }) catch unreachable);
}
info.abs_real_path = symlink;
} else if (parent_info.abs_real_path.len > 0) {
// this might leak a little i'm not sure
const parts = [_]string{ parent_info.abs_real_path, base };
- symlink = std.fs.path.join(r.allocator, &parts);
- if (r.debug_logs) |logs| {
- try logs.addNote(std.fmt.allocPrint(r.allocator, "Resolved symlink \"{s}\" to \"{s}\"", .{ path, symlink }));
+ symlink = std.fs.path.join(r.allocator, &parts) catch unreachable;
+ if (r.debug_logs) |*logs| {
+ try logs.addNote(std.fmt.allocPrint(r.allocator, "Resolved symlink \"{s}\" to \"{s}\"", .{ path, symlink }) catch unreachable);
}
info.abs_real_path = symlink;
}
@@ -535,19 +536,20 @@ pub const Resolver = struct {
}
// Record if this directory has a package.json file
- if (entries.get("package.json")) |entry| {
+ if (entries.get("package.json")) |lookup| {
+ const entry = lookup.entry;
if (entry.kind(rfs) == .file) {
- info.package_json = r.parsePackageJSON(path);
+ info.package_json = r.parsePackageJSON(path) catch null;
if (info.package_json) |pkg| {
- if (pkg.browser_map != null) {
+ if (pkg.browser_map.count() > 0) {
info.enclosing_browser_scope = info;
}
- if (r.debug_logs) |logs| {
- try logs.addNote(std.fmt.allocPrint(r.allocator, "Resolved package.json in \"{s}\"", .{
+ if (r.debug_logs) |*logs| {
+ logs.addNoteFmt("Resolved package.json in \"{s}\"", .{
path,
- }));
+ }) catch unreachable;
}
}
}
@@ -557,14 +559,20 @@ pub const Resolver = struct {
{
var tsconfig_path: ?string = null;
if (r.opts.tsconfig_override == null) {
- var entry = entries.get("tsconfig.json");
- if (entry.kind(rfs) == .file) {
- const parts = [_]string{ path, "tsconfig.json" };
- tsconfig_path = try std.fs.path.join(r.allocator, parts);
- } else if (entries.get("jsconfig.json")) |jsconfig| {
- if (jsconfig.kind(rfs) == .file) {
- const parts = [_]string{ path, "jsconfig.json" };
- tsconfig_path = try std.fs.path.join(r.allocator, parts);
+ if (entries.get("tsconfig.json")) |lookup| {
+ const entry = lookup.entry;
+ if (entry.kind(rfs) == .file) {
+ const parts = [_]string{ path, "tsconfig.json" };
+ tsconfig_path = try std.fs.path.join(r.allocator, &parts);
+ }
+ }
+ if (tsconfig_path == null) {
+ if (entries.get("jsconfig.json")) |lookup| {
+ const entry = lookup.entry;
+ if (entry.kind(rfs) == .file) {
+ const parts = [_]string{ path, "jsconfig.json" };
+ tsconfig_path = try std.fs.path.join(r.allocator, &parts);
+ }
}
}
} else if (parent == null) {
@@ -574,20 +582,21 @@ pub const Resolver = struct {
if (tsconfig_path) |tsconfigpath| {
var visited = std.StringHashMap(bool).init(r.allocator);
defer visited.deinit();
- info.ts_config_json = r.parseTSConfig(tsconfigpath, visited) catch |err| {
+ info.tsconfig_json = r.parseTSConfig(tsconfigpath, &visited) catch |err| brk: {
const pretty = r.prettyPath(Path.init(tsconfigpath));
if (err == error.ENOENT) {
- r.log.addErrorFmt(null, logger.Loc.Empty, r.allocator, "Cannot find tsconfig file \"{s}\"", .{pretty});
+ r.log.addErrorFmt(null, logger.Loc.Empty, r.allocator, "Cannot find tsconfig file \"{s}\"", .{pretty}) catch unreachable;
} else if (err != error.ParseErrorAlreadyLogged) {
- r.log.addErrorFmt(null, logger.Loc.Empty, r.allocator, "Cannot read file \"{s}\": {s}", .{ pretty, @errorName(err) });
+ r.log.addErrorFmt(null, logger.Loc.Empty, r.allocator, "Cannot read file \"{s}\": {s}", .{ pretty, @errorName(err) }) catch unreachable;
}
+ break :brk null;
};
}
}
- if (info.ts_config_json == null and parent != null) {
- info.ts_config_json = parent.?.tsconfig_json;
+ if (info.tsconfig_json == null and parent != null) {
+ info.tsconfig_json = parent.?.tsconfig_json;
}
return info;
diff --git a/src/resolver/tsconfig_json.zig b/src/resolver/tsconfig_json.zig
index b4e027413..6c7992833 100644
--- a/src/resolver/tsconfig_json.zig
+++ b/src/resolver/tsconfig_json.zig
@@ -1,10 +1,10 @@
usingnamespace @import("../global.zig");
const std = @import("std");
const options = @import("../options.zig");
-const log = @import("../logger.zig");
-const cache = @import("../cache.zig");
const logger = @import("../logger.zig");
+const cache = @import("../cache.zig");
const js_ast = @import("../js_ast.zig");
+const js_lexer = @import("../js_lexer.zig");
const alloc = @import("../alloc.zig");
const PathsMap = std.StringHashMap([]string);
@@ -53,7 +53,6 @@ pub const TSConfigJSON = struct {
allocator: *std.mem.Allocator,
log: *logger.Log,
source: logger.Source,
- opts: options.TransformOptions,
json_cache: *cache.Cache.Json,
) anyerror!?*TSConfigJSON {
// Unfortunately "tsconfig.json" isn't actually JSON. It's some other
@@ -64,52 +63,52 @@ pub const TSConfigJSON = struct {
// these particular files. This is likely not a completely accurate
// emulation of what the TypeScript compiler does (e.g. string escape
// behavior may also be different).
- const json: js_ast.Expr = (json_cache.parseTSConfig(log, opts, source, allocator) catch null) orelse return null;
+ const json: js_ast.Expr = (json_cache.parseTSConfig(log, source, allocator) catch null) orelse return null;
var result: TSConfigJSON = TSConfigJSON{ .abs_path = source.key_path.text, .paths = PathsMap.init(allocator) };
errdefer allocator.free(result.paths);
- if (extends != null) {
- if (json.getProperty("extends")) |extends_value| {
- log.addWarning(&source, extends_value.loc, "\"extends\" is not implemented yet") catch unreachable;
- // if ((extends_value.expr.getString(allocator) catch null)) |str| {
- // if (extends(str, source.rangeOfString(extends_value.loc))) |base| {
- // result.jsx = base.jsx;
- // result.base_url_for_paths = base.base_url_for_paths;
- // result.use_define_for_class_fields = base.use_define_for_class_fields;
- // result.preserve_imports_not_used_as_values = base.preserve_imports_not_used_as_values;
- // // https://github.com/microsoft/TypeScript/issues/14527#issuecomment-284948808
- // result.paths = base.paths;
- // }
- // }
- }
+ if (json.getProperty("extends")) |extends_value| {
+ log.addWarning(&source, extends_value.loc, "\"extends\" is not implemented yet") catch unreachable;
+ // if ((extends_value.expr.getString(allocator) catch null)) |str| {
+ // if (extends(str, source.rangeOfString(extends_value.loc))) |base| {
+ // result.jsx = base.jsx;
+ // result.base_url_for_paths = base.base_url_for_paths;
+ // result.use_define_for_class_fields = base.use_define_for_class_fields;
+ // result.preserve_imports_not_used_as_values = base.preserve_imports_not_used_as_values;
+ // // https://github.com/microsoft/TypeScript/issues/14527#issuecomment-284948808
+ // result.paths = base.paths;
+ // }
+ // }
}
// Parse "compilerOptions"
if (json.getProperty("compilerOptions")) |compiler_opts| {
+ var has_base_url = false;
// Parse "baseUrl"
if (compiler_opts.expr.getProperty("baseUrl")) |base_url_prop| {
// maybe we should add a warning when it exists but the value is an array or osmething invalid?
- if ((base_url_prop.expr.getString(allocator) catch null)) |base_url| {
+ if ((base_url_prop.expr.getString(allocator))) |base_url| {
result.base_url = base_url;
+ has_base_url = true;
}
}
// Parse "jsxFactory"
if (compiler_opts.expr.getProperty("jsxFactory")) |jsx_prop| {
if (jsx_prop.expr.getString(allocator)) |str| {
- result.jsx.factory = try parseMemberExpressionForJSX(log, source, jsx_prop.loc, str, allocator);
+ result.jsx.factory = try parseMemberExpressionForJSX(log, &source, jsx_prop.loc, str, allocator);
}
}
// Parse "jsxFragmentFactory"
if (compiler_opts.expr.getProperty("jsxFactory")) |jsx_prop| {
if (jsx_prop.expr.getString(allocator)) |str| {
- result.jsx.fragment = try parseMemberExpressionForJSX(log, source, jsx_prop.loc, str, allocator);
+ result.jsx.fragment = try parseMemberExpressionForJSX(log, &source, jsx_prop.loc, str, allocator);
}
}
// Parse "jsxImportSource"
- if (compiler_opts.expr.getProperty("jsxImportSource")) |jsx_factory_prop| {
+ if (compiler_opts.expr.getProperty("jsxImportSource")) |jsx_prop| {
if (jsx_prop.expr.getString(allocator)) |str| {
result.jsx.import_source = str;
}
@@ -123,16 +122,16 @@ pub const TSConfigJSON = struct {
}
// Parse "importsNotUsedAsValues"
- if (compiler_opts.expr.getProperty("importsNotUsedAsValues")) |imports_not_used_as_values_prop| {
+ if (compiler_opts.expr.getProperty("importsNotUsedAsValues")) |jsx_prop| {
// This should never allocate since it will be utf8
- if ((jsx_prop.expr.getString(allocator) catch null)) |str| {
+ if ((jsx_prop.expr.getString(allocator))) |str| {
switch (ImportsNotUsedAsValue.List.get(str) orelse ImportsNotUsedAsValue.invalid) {
.preserve, .err => {
result.preserve_imports_not_used_as_values = true;
},
.remove => {},
else => {
- log.addRangeWarningFmt(source, source.rangeOfString(imports_not_used_as_values_prop.loc), allocator, "Invalid value \"{s}\" for \"importsNotUsedAsValues\"", .{str}) catch {};
+ log.addRangeWarningFmt(&source, source.rangeOfString(jsx_prop.loc), allocator, "Invalid value \"{s}\" for \"importsNotUsedAsValues\"", .{str}) catch {};
},
}
}
@@ -146,9 +145,9 @@ pub const TSConfigJSON = struct {
result.paths = PathsMap.init(allocator);
for (paths.properties) |property| {
const key_prop = property.key orelse continue;
- const key = (key_prop.getString(allocator) catch null) orelse continue;
+ const key = (key_prop.getString(allocator)) orelse continue;
- if (!TSConfigJSON.isValidTSConfigPathNoBaseURLPattern(key, log, source, key_prop.loc)) {
+ if (!TSConfigJSON.isValidTSConfigPathNoBaseURLPattern(key, log, &source, allocator, key_prop.loc)) {
continue;
}
@@ -178,20 +177,27 @@ pub const TSConfigJSON = struct {
switch (value_prop.data) {
.e_array => |array| {
if (array.items.len > 0) {
- var paths = allocator.alloc(string, array.items.len) catch unreachable;
- errdefer allocator.free(paths);
+ var values = allocator.alloc(string, array.items.len) catch unreachable;
+ errdefer allocator.free(values);
var count: usize = 0;
for (array.items) |expr| {
- if ((expr.getString(allocator) catch null)) |str| {
- if (TSConfigJSON.isValidTSConfigPathPattern(str, log, source, loc, allocator) and
+ if ((expr.getString(allocator))) |str| {
+ if (TSConfigJSON.isValidTSConfigPathPattern(
+ str,
+ log,
+ &source,
+ expr.loc,
+ allocator,
+ ) and
(has_base_url or
TSConfigJSON.isValidTSConfigPathNoBaseURLPattern(
str,
log,
- source,
- loc,
+ &source,
+ allocator,
+ expr.loc,
))) {
- paths[count] = str;
+ values[count] = str;
count += 1;
}
}
@@ -199,15 +205,15 @@ pub const TSConfigJSON = struct {
if (count > 0) {
result.paths.put(
key,
- paths[0..count],
+ values[0..count],
) catch unreachable;
}
}
},
else => {
log.addRangeWarningFmt(
- source,
- log,
+ &source,
+ source.rangeOfString(key_prop.loc),
allocator,
"Substitutions for pattern \"{s}\" should be an array",
.{key},
@@ -226,7 +232,7 @@ pub const TSConfigJSON = struct {
return _result;
}
- pub fn isValidTSConfigPathPattern(text: string, log: *logger.Log, source: *logger.Source, loc: logger.Loc, allocator: *std.mem.Allocator) bool {
+ pub fn isValidTSConfigPathPattern(text: string, log: *logger.Log, source: *const logger.Source, loc: logger.Loc, allocator: *std.mem.Allocator) bool {
var found_asterisk = false;
for (text) |c, i| {
if (c == '*') {
@@ -242,7 +248,7 @@ pub const TSConfigJSON = struct {
return true;
}
- pub fn parseMemberExpressionForJSX(log: *logger.Log, source: *logger.Source, loc: logger.Loc, text: string, allocator: *std.mem.Allocator) ![]string {
+ pub fn parseMemberExpressionForJSX(log: *logger.Log, source: *const logger.Source, loc: logger.Loc, text: string, allocator: *std.mem.Allocator) ![]string {
if (text.len == 0) {
return &([_]string{});
}
@@ -267,7 +273,7 @@ pub const TSConfigJSON = struct {
return c == '/' or c == '\\';
}
- pub fn isValidTSConfigPathNoBaseURLPattern(text: string, log: logger.Log, source: *logger.Source, loc: logger.Loc) bool {
+ pub fn isValidTSConfigPathNoBaseURLPattern(text: string, log: *logger.Log, source: *const logger.Source, allocator: *std.mem.Allocator, loc: logger.Loc) bool {
var c0: u8 = 0;
var c1: u8 = 0;
var c2: u8 = 0;