summaryrefslogtreecommitdiff
path: root/cli/tools/registry/unfurl.rs
blob: 5ec726a64015904c2f33d486526a6cf74dd09888 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use deno_ast::ParsedSource;
use deno_ast::SourceRange;
use deno_ast::SourceTextInfo;
use deno_config::workspace::MappedResolution;
use deno_config::workspace::PackageJsonDepResolution;
use deno_config::workspace::WorkspaceResolver;
use deno_core::ModuleSpecifier;
use deno_graph::DependencyDescriptor;
use deno_graph::DynamicTemplatePart;
use deno_graph::ParserModuleAnalyzer;
use deno_graph::TypeScriptReference;
use deno_package_json::PackageJsonDepValue;
use deno_resolver::sloppy_imports::SloppyImportsResolutionMode;
use deno_runtime::deno_node::is_builtin_node_module;

use crate::resolver::CliSloppyImportsResolver;

#[derive(Debug, Clone)]
pub enum SpecifierUnfurlerDiagnostic {
  UnanalyzableDynamicImport {
    specifier: ModuleSpecifier,
    text_info: SourceTextInfo,
    range: SourceRange,
  },
}

impl SpecifierUnfurlerDiagnostic {
  pub fn code(&self) -> &'static str {
    match self {
      Self::UnanalyzableDynamicImport { .. } => "unanalyzable-dynamic-import",
    }
  }

  pub fn message(&self) -> &'static str {
    match self {
      Self::UnanalyzableDynamicImport { .. } => {
        "unable to analyze dynamic import"
      }
    }
  }
}

pub struct SpecifierUnfurler {
  sloppy_imports_resolver: Option<CliSloppyImportsResolver>,
  workspace_resolver: WorkspaceResolver,
  bare_node_builtins: bool,
}

impl SpecifierUnfurler {
  pub fn new(
    sloppy_imports_resolver: Option<CliSloppyImportsResolver>,
    workspace_resolver: WorkspaceResolver,
    bare_node_builtins: bool,
  ) -> Self {
    debug_assert_eq!(
      workspace_resolver.pkg_json_dep_resolution(),
      PackageJsonDepResolution::Enabled
    );
    Self {
      sloppy_imports_resolver,
      workspace_resolver,
      bare_node_builtins,
    }
  }

  fn unfurl_specifier(
    &self,
    referrer: &ModuleSpecifier,
    specifier: &str,
  ) -> Option<String> {
    let resolved = if let Ok(resolved) =
      self.workspace_resolver.resolve(specifier, referrer)
    {
      match resolved {
        MappedResolution::Normal { specifier, .. }
        | MappedResolution::ImportMap { specifier, .. } => Some(specifier),
        MappedResolution::WorkspaceJsrPackage { pkg_req_ref, .. } => {
          Some(ModuleSpecifier::parse(&pkg_req_ref.to_string()).unwrap())
        }
        MappedResolution::WorkspaceNpmPackage {
          target_pkg_json: pkg_json,
          pkg_name,
          sub_path,
        } => {
          // todo(#24612): consider warning or error when this is also a jsr package?
          ModuleSpecifier::parse(&format!(
            "npm:{}{}{}",
            pkg_name,
            pkg_json
              .version
              .as_ref()
              .map(|v| format!("@^{}", v))
              .unwrap_or_default(),
            sub_path
              .as_ref()
              .map(|s| format!("/{}", s))
              .unwrap_or_default()
          ))
          .ok()
        }
        MappedResolution::PackageJson {
          alias,
          sub_path,
          dep_result,
          ..
        } => match dep_result {
          Ok(dep) => match dep {
            PackageJsonDepValue::Req(pkg_req) => {
              // todo(#24612): consider warning or error when this is an npm workspace
              // member that's also a jsr package?
              ModuleSpecifier::parse(&format!(
                "npm:{}{}",
                pkg_req,
                sub_path
                  .as_ref()
                  .map(|s| format!("/{}", s))
                  .unwrap_or_default()
              ))
              .ok()
            }
            PackageJsonDepValue::Workspace(version_req) => {
              // todo(#24612): consider warning or error when this is also a jsr package?
              ModuleSpecifier::parse(&format!(
                "npm:{}@{}{}",
                alias,
                version_req,
                sub_path
                  .as_ref()
                  .map(|s| format!("/{}", s))
                  .unwrap_or_default()
              ))
              .ok()
            }
          },
          Err(err) => {
            log::warn!(
              "Ignoring failed to resolve package.json dependency. {:#}",
              err
            );
            None
          }
        },
      }
    } else {
      None
    };
    let resolved = match resolved {
      Some(resolved) => resolved,
      None if self.bare_node_builtins && is_builtin_node_module(specifier) => {
        format!("node:{specifier}").parse().unwrap()
      }
      None => ModuleSpecifier::options()
        .base_url(Some(referrer))
        .parse(specifier)
        .ok()?,
    };
    // TODO(lucacasonato): this requires integration in deno_graph first
    // let resolved = if let Ok(specifier) =
    //   NpmPackageReqReference::from_specifier(&resolved)
    // {
    //   if let Some(scope_name) = specifier.req().name.strip_prefix("@jsr/") {
    //     let (scope, name) = scope_name.split_once("__")?;
    //     let new_specifier = JsrPackageReqReference::new(PackageReqReference {
    //       req: PackageReq {
    //         name: format!("@{scope}/{name}"),
    //         version_req: specifier.req().version_req.clone(),
    //       },
    //       sub_path: specifier.sub_path().map(ToOwned::to_owned),
    //     })
    //     .to_string();
    //     ModuleSpecifier::parse(&new_specifier).unwrap()
    //   } else {
    //     resolved
    //   }
    // } else {
    //   resolved
    // };
    let resolved =
      if let Some(sloppy_imports_resolver) = &self.sloppy_imports_resolver {
        sloppy_imports_resolver
          .resolve(&resolved, SloppyImportsResolutionMode::Execution)
          .map(|res| res.into_specifier())
          .unwrap_or(resolved)
      } else {
        resolved
      };
    let relative_resolved = relative_url(&resolved, referrer);
    if relative_resolved == specifier {
      None // nothing to unfurl
    } else {
      log::debug!(
        "Unfurled specifier: {} from {} -> {}",
        specifier,
        referrer,
        relative_resolved
      );
      Some(relative_resolved)
    }
  }

  /// Attempts to unfurl the dynamic dependency returning `true` on success
  /// or `false` when the import was not analyzable.
  fn try_unfurl_dynamic_dep(
    &self,
    module_url: &ModuleSpecifier,
    text_info: &SourceTextInfo,
    dep: &deno_graph::DynamicDependencyDescriptor,
    text_changes: &mut Vec<deno_ast::TextChange>,
  ) -> bool {
    match &dep.argument {
      deno_graph::DynamicArgument::String(specifier) => {
        let range = to_range(text_info, &dep.argument_range);
        let maybe_relative_index =
          text_info.text_str()[range.start..range.end].find(specifier);
        let Some(relative_index) = maybe_relative_index else {
          return true; // always say it's analyzable for a string
        };
        let unfurled = self.unfurl_specifier(module_url, specifier);
        if let Some(unfurled) = unfurled {
          let start = range.start + relative_index;
          text_changes.push(deno_ast::TextChange {
            range: start..start + specifier.len(),
            new_text: unfurled,
          });
        }
        true
      }
      deno_graph::DynamicArgument::Template(parts) => match parts.first() {
        Some(DynamicTemplatePart::String { value: specifier }) => {
          // relative doesn't need to be modified
          let is_relative =
            specifier.starts_with("./") || specifier.starts_with("../");
          if is_relative {
            return true;
          }
          if !specifier.ends_with('/') {
            return false;
          }
          let unfurled = self.unfurl_specifier(module_url, specifier);
          let Some(unfurled) = unfurled else {
            return true; // nothing to unfurl
          };
          let range = to_range(text_info, &dep.argument_range);
          let maybe_relative_index =
            text_info.text_str()[range.start..].find(specifier);
          let Some(relative_index) = maybe_relative_index else {
            return false;
          };
          let start = range.start + relative_index;
          text_changes.push(deno_ast::TextChange {
            range: start..start + specifier.len(),
            new_text: unfurled,
          });
          true
        }
        Some(DynamicTemplatePart::Expr) => {
          false // failed analyzing
        }
        None => {
          true // ignore
        }
      },
      deno_graph::DynamicArgument::Expr => {
        false // failed analyzing
      }
    }
  }

  pub fn unfurl(
    &self,
    url: &ModuleSpecifier,
    parsed_source: &ParsedSource,
    diagnostic_reporter: &mut dyn FnMut(SpecifierUnfurlerDiagnostic),
  ) -> String {
    let mut text_changes = Vec::new();
    let text_info = parsed_source.text_info_lazy();
    let module_info = ParserModuleAnalyzer::module_info(parsed_source);
    let analyze_specifier =
      |specifier: &str,
       range: &deno_graph::PositionRange,
       text_changes: &mut Vec<deno_ast::TextChange>| {
        if let Some(unfurled) = self.unfurl_specifier(url, specifier) {
          text_changes.push(deno_ast::TextChange {
            range: to_range(text_info, range),
            new_text: unfurled,
          });
        }
      };
    for dep in &module_info.dependencies {
      match dep {
        DependencyDescriptor::Static(dep) => {
          analyze_specifier(
            &dep.specifier,
            &dep.specifier_range,
            &mut text_changes,
          );
        }
        DependencyDescriptor::Dynamic(dep) => {
          let success =
            self.try_unfurl_dynamic_dep(url, text_info, dep, &mut text_changes);

          if !success {
            let start_pos = text_info.line_start(dep.argument_range.start.line)
              + dep.argument_range.start.character;
            let end_pos = text_info.line_start(dep.argument_range.end.line)
              + dep.argument_range.end.character;
            diagnostic_reporter(
              SpecifierUnfurlerDiagnostic::UnanalyzableDynamicImport {
                specifier: url.to_owned(),
                range: SourceRange::new(start_pos, end_pos),
                text_info: text_info.clone(),
              },
            );
          }
        }
      }
    }
    for ts_ref in &module_info.ts_references {
      let specifier_with_range = match ts_ref {
        TypeScriptReference::Path(range) => range,
        TypeScriptReference::Types(range) => range,
      };
      analyze_specifier(
        &specifier_with_range.text,
        &specifier_with_range.range,
        &mut text_changes,
      );
    }
    for specifier_with_range in &module_info.jsdoc_imports {
      analyze_specifier(
        &specifier_with_range.text,
        &specifier_with_range.range,
        &mut text_changes,
      );
    }
    if let Some(specifier_with_range) = &module_info.jsx_import_source {
      analyze_specifier(
        &specifier_with_range.text,
        &specifier_with_range.range,
        &mut text_changes,
      );
    }

    let rewritten_text =
      deno_ast::apply_text_changes(text_info.text_str(), text_changes);
    rewritten_text
  }
}

fn relative_url(
  resolved: &ModuleSpecifier,
  referrer: &ModuleSpecifier,
) -> String {
  if resolved.scheme() == "file" {
    let relative = referrer.make_relative(resolved).unwrap();
    if relative.is_empty() {
      let last = resolved.path_segments().unwrap().last().unwrap();
      format!("./{last}")
    } else if relative.starts_with("../") {
      relative
    } else {
      format!("./{relative}")
    }
  } else {
    resolved.to_string()
  }
}

fn to_range(
  text_info: &SourceTextInfo,
  range: &deno_graph::PositionRange,
) -> std::ops::Range<usize> {
  let mut range = range
    .as_source_range(text_info)
    .as_byte_range(text_info.range().start);
  let text = &text_info.text_str()[range.clone()];
  if text.starts_with('"') || text.starts_with('\'') {
    range.start += 1;
  }
  if text.ends_with('"') || text.ends_with('\'') {
    range.end -= 1;
  }
  range
}

#[cfg(test)]
mod tests {
  use std::sync::Arc;

  use crate::resolver::SloppyImportsCachedFs;

  use super::*;
  use deno_ast::MediaType;
  use deno_ast::ModuleSpecifier;
  use deno_config::workspace::ResolverWorkspaceJsrPackage;
  use deno_core::serde_json::json;
  use deno_core::url::Url;
  use deno_runtime::deno_fs::RealFs;
  use deno_runtime::deno_node::PackageJson;
  use deno_semver::Version;
  use import_map::ImportMapWithDiagnostics;
  use indexmap::IndexMap;
  use pretty_assertions::assert_eq;
  use test_util::testdata_path;

  fn parse_ast(specifier: &Url, source_code: &str) -> ParsedSource {
    let media_type = MediaType::from_specifier(specifier);
    deno_ast::parse_module(deno_ast::ParseParams {
      specifier: specifier.clone(),
      media_type,
      capture_tokens: false,
      maybe_syntax: None,
      scope_analysis: false,
      text: source_code.into(),
    })
    .unwrap()
  }

  #[test]
  fn test_unfurling() {
    let cwd = testdata_path().join("unfurl").to_path_buf();

    let deno_json_url =
      ModuleSpecifier::from_file_path(cwd.join("deno.json")).unwrap();
    let value = json!({
      "imports": {
        "express": "npm:express@5",
        "lib/": "./lib/",
        "fizz": "./fizz/mod.ts",
        "@std/fs": "npm:@jsr/std__fs@1",
      }
    });
    let ImportMapWithDiagnostics { import_map, .. } =
      import_map::parse_from_value(deno_json_url, value).unwrap();
    let package_json = PackageJson::load_from_value(
      cwd.join("package.json"),
      json!({
        "dependencies": {
          "chalk": 5
        }
      }),
    );
    let workspace_resolver = WorkspaceResolver::new_raw(
      Arc::new(ModuleSpecifier::from_directory_path(&cwd).unwrap()),
      Some(import_map),
      vec![ResolverWorkspaceJsrPackage {
        is_patch: false,
        base: ModuleSpecifier::from_directory_path(cwd.join("jsr-package"))
          .unwrap(),
        name: "@denotest/example".to_string(),
        version: Some(Version::parse_standard("1.0.0").unwrap()),
        exports: IndexMap::from([(".".to_string(), "mod.ts".to_string())]),
      }],
      vec![Arc::new(package_json)],
      deno_config::workspace::PackageJsonDepResolution::Enabled,
    );
    let fs = Arc::new(RealFs);
    let unfurler = SpecifierUnfurler::new(
      Some(CliSloppyImportsResolver::new(SloppyImportsCachedFs::new(
        fs,
      ))),
      workspace_resolver,
      true,
    );

    // Unfurling TS file should apply changes.
    {
      let source_code = r#"import express from "express";"
import foo from "lib/foo.ts";
import bar from "lib/bar.ts";
import fizz from "fizz";
import chalk from "chalk";
import baz from "./baz";
import b from "./b.js";
import b2 from "./b";
import "./mod.ts";
import url from "url";
import "@denotest/example";
// TODO: unfurl these to jsr
// import "npm:@jsr/std__fs@1/file";
// import "npm:@jsr/std__fs@1";
// import "npm:@jsr/std__fs";
// import "@std/fs";

const test1 = await import("lib/foo.ts");
const test2 = await import(`lib/foo.ts`);
const test3 = await import(`lib/${expr}`);
const test4 = await import(`./lib/${expr}`);
const test5 = await import("./lib/something.ts");
const test6 = await import(`./lib/something.ts`);
// will warn
const warn1 = await import(`lib${expr}`);
const warn2 = await import(`${expr}`);
"#;
      let specifier =
        ModuleSpecifier::from_file_path(cwd.join("mod.ts")).unwrap();
      let source = parse_ast(&specifier, source_code);
      let mut d = Vec::new();
      let mut reporter = |diagnostic| d.push(diagnostic);
      let unfurled_source = unfurler.unfurl(&specifier, &source, &mut reporter);
      assert_eq!(d.len(), 2);
      assert!(
        matches!(
          d[0],
          SpecifierUnfurlerDiagnostic::UnanalyzableDynamicImport { .. }
        ),
        "{:?}",
        d[0]
      );
      assert!(
        matches!(
          d[1],
          SpecifierUnfurlerDiagnostic::UnanalyzableDynamicImport { .. }
        ),
        "{:?}",
        d[1]
      );
      let expected_source = r#"import express from "npm:express@5";"
import foo from "./lib/foo.ts";
import bar from "./lib/bar.ts";
import fizz from "./fizz/mod.ts";
import chalk from "npm:chalk@5";
import baz from "./baz/index.js";
import b from "./b.ts";
import b2 from "./b.ts";
import "./mod.ts";
import url from "node:url";
import "jsr:@denotest/example@^1.0.0";
// TODO: unfurl these to jsr
// import "npm:@jsr/std__fs@1/file";
// import "npm:@jsr/std__fs@1";
// import "npm:@jsr/std__fs";
// import "@std/fs";

const test1 = await import("./lib/foo.ts");
const test2 = await import(`./lib/foo.ts`);
const test3 = await import(`./lib/${expr}`);
const test4 = await import(`./lib/${expr}`);
const test5 = await import("./lib/something.ts");
const test6 = await import(`./lib/something.ts`);
// will warn
const warn1 = await import(`lib${expr}`);
const warn2 = await import(`${expr}`);
"#;
      assert_eq!(unfurled_source, expected_source);
    }
  }
}