deno 2.7.13

Provides the deno executable
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
// Copyright 2018-2026 the Deno authors. MIT license.

use std::borrow::Cow;
use std::path::Path;
use std::sync::Arc;

use deno_ast::MediaType;
use deno_ast::ParsedSource;
use deno_ast::SourceTextInfo;
use deno_ast::TextChange;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::url::Url;
use deno_graph::ModuleGraph;
use deno_resolver::cache::LazyGraphSourceParser;
use deno_resolver::cache::ParsedSourceCache;
use deno_resolver::deno_json::CompilerOptionsResolver;
use deno_resolver::workspace::ResolutionKind;
use lazy_regex::Lazy;

use super::diagnostics::PublishDiagnostic;
use super::diagnostics::PublishDiagnosticsCollector;
use super::unfurl::PositionOrSourceRangeRef;
use super::unfurl::SpecifierUnfurler;
use super::unfurl::SpecifierUnfurlerDiagnostic;
use super::unfurl::SpecifierUnfurlerSys;
use crate::sys::CliSys;

struct JsxFolderOptions<'a> {
  jsx_runtime: &'static str,
  jsx_classic: Option<Cow<'a, deno_ast::JsxClassicOptions>>,
  jsx_import_source: Option<String>,
  jsx_import_source_types: Option<String>,
}

#[sys_traits::auto_impl]
pub trait ModuleContentProviderSys: SpecifierUnfurlerSys {}

pub struct ModuleContentProvider<TSys: ModuleContentProviderSys = CliSys> {
  specifier_unfurler: SpecifierUnfurler<TSys>,
  parsed_source_cache: Arc<ParsedSourceCache>,
  sys: TSys,
  compiler_options_resolver: Arc<CompilerOptionsResolver>,
}

impl<TSys: ModuleContentProviderSys> ModuleContentProvider<TSys> {
  pub fn new(
    parsed_source_cache: Arc<ParsedSourceCache>,
    specifier_unfurler: SpecifierUnfurler<TSys>,
    sys: TSys,
    compiler_options_resolver: Arc<CompilerOptionsResolver>,
  ) -> Self {
    Self {
      specifier_unfurler,
      parsed_source_cache,
      sys,
      compiler_options_resolver,
    }
  }

  pub fn resolve_content_maybe_unfurling(
    &self,
    graph: &ModuleGraph,
    diagnostics_collector: &PublishDiagnosticsCollector,
    path: &Path,
    specifier: &Url,
  ) -> Result<Vec<u8>, AnyError> {
    let source_parser =
      LazyGraphSourceParser::new(&self.parsed_source_cache, graph);
    let media_type = MediaType::from_specifier(specifier);
    let parsed_source = match source_parser.get_or_parse_source(specifier)? {
      Some(parsed_source) => parsed_source,
      None => {
        let data = self.sys.fs_read(path).with_context(|| {
          format!("Unable to read file '{}'", path.display())
        })?;

        match media_type {
          MediaType::JavaScript
          | MediaType::Jsx
          | MediaType::Mjs
          | MediaType::Cjs
          | MediaType::TypeScript
          | MediaType::Mts
          | MediaType::Cts
          | MediaType::Dts
          | MediaType::Dmts
          | MediaType::Dcts
          | MediaType::Tsx => {
            // continue
          }
          MediaType::SourceMap
          | MediaType::Unknown
          | MediaType::Html
          | MediaType::Markdown
          | MediaType::Sql
          | MediaType::Json
          | MediaType::Jsonc
          | MediaType::Json5
          | MediaType::Wasm
          | MediaType::Css => {
            // not unfurlable data
            return Ok(data.into_owned());
          }
        }

        let text = String::from_utf8_lossy(&data);
        deno_ast::parse_module(deno_ast::ParseParams {
          specifier: specifier.clone(),
          text: text.into(),
          media_type,
          capture_tokens: false,
          maybe_syntax: None,
          scope_analysis: false,
        })?
      }
    };

    log::debug!("Unfurling {}", specifier);
    let mut reporter = |diagnostic| {
      diagnostics_collector
        .push(PublishDiagnostic::SpecifierUnfurl(diagnostic));
    };
    let text_info = parsed_source.text_info_lazy();
    let module_info =
      deno_graph::ast::ParserModuleAnalyzer::module_info(&parsed_source);
    let mut text_changes = Vec::new();
    if media_type.is_jsx() {
      self.add_jsx_text_changes(
        specifier,
        &parsed_source,
        text_info,
        &module_info,
        &mut reporter,
        &mut text_changes,
      )?;
    }

    self.specifier_unfurler.unfurl_to_changes(
      specifier,
      &parsed_source,
      &module_info,
      &mut text_changes,
      &mut reporter,
    );
    let rewritten_text =
      deno_ast::apply_text_changes(text_info.text_str(), text_changes);

    Ok(rewritten_text.into_bytes())
  }

  fn add_jsx_text_changes(
    &self,
    specifier: &Url,
    parsed_source: &ParsedSource,
    text_info: &SourceTextInfo,
    module_info: &deno_graph::analysis::ModuleInfo,
    diagnostic_reporter: &mut dyn FnMut(SpecifierUnfurlerDiagnostic),
    text_changes: &mut Vec<TextChange>,
  ) -> Result<(), AnyError> {
    static JSX_RUNTIME_RE: Lazy<regex::Regex> =
      lazy_regex::lazy_regex!(r"(?i)^[\s*]*@jsxRuntime\s+(\S+)");
    static JSX_FACTORY_RE: Lazy<regex::Regex> =
      lazy_regex::lazy_regex!(r"(?i)^[\s*]*@jsxFactory\s+(\S+)");
    static JSX_FRAGMENT_FACTORY_RE: Lazy<regex::Regex> =
      lazy_regex::lazy_regex!(r"(?i)^[\s*]*@jsxFragmentFactory\s+(\S+)");

    let start_pos = if parsed_source.program_ref().shebang().is_some() {
      match text_info.text_str().find('\n') {
        Some(index) => index + 1,
        None => return Ok(()), // nothing in this file
      }
    } else {
      0
    };
    let mut add_text_change = |new_text: String| {
      text_changes.push(TextChange {
        range: start_pos..start_pos,
        new_text,
      })
    };
    let jsx_options =
      self.resolve_jsx_options(specifier, text_info, diagnostic_reporter)?;
    let leading_comments = parsed_source.get_leading_comments();
    let leading_comments_has_re = |regex: &regex::Regex| {
      leading_comments
        .as_ref()
        .map(|comments| {
          comments.iter().any(|c| {
            c.kind == deno_ast::swc::common::comments::CommentKind::Block
              && regex.is_match(c.text.as_str())
          })
        })
        .unwrap_or(false)
    };
    if !leading_comments_has_re(&JSX_RUNTIME_RE) {
      add_text_change(format!(
        "/** @jsxRuntime {} */",
        jsx_options.jsx_runtime,
      ));
    }
    if module_info.jsx_import_source.is_none()
      && let Some(import_source) = jsx_options.jsx_import_source
    {
      add_text_change(format!("/** @jsxImportSource {} */", import_source));
    }
    if module_info.jsx_import_source_types.is_none()
      && let Some(import_source) = jsx_options.jsx_import_source_types
    {
      add_text_change(format!(
        "/** @jsxImportSourceTypes {} */",
        import_source
      ));
    }
    if let Some(classic_options) = &jsx_options.jsx_classic {
      if !leading_comments_has_re(&JSX_FACTORY_RE) {
        add_text_change(format!(
          "/** @jsxFactory {} */",
          classic_options.factory,
        ));
      }
      if !leading_comments_has_re(&JSX_FRAGMENT_FACTORY_RE) {
        add_text_change(format!(
          "/** @jsxFragmentFactory {} */",
          classic_options.fragment_factory,
        ));
      }
    }
    Ok(())
  }

  fn resolve_jsx_options<'a>(
    &'a self,
    specifier: &Url,
    text_info: &SourceTextInfo,
    diagnostic_reporter: &mut dyn FnMut(SpecifierUnfurlerDiagnostic),
  ) -> Result<JsxFolderOptions<'a>, AnyError> {
    let compiler_options =
      self.compiler_options_resolver.for_specifier(specifier);
    let jsx_config = compiler_options.jsx_import_source_config()?;
    let transpile_options = &compiler_options.transpile_options()?.transpile;
    let jsx_runtime = match &transpile_options.jsx {
      Some(
        deno_ast::JsxRuntime::Automatic(_)
        | deno_ast::JsxRuntime::Precompile(_),
      ) => "automatic",
      None | Some(deno_ast::JsxRuntime::Classic(_)) => "classic",
    };
    let mut unfurl_import_source =
      |import_source: &str, referrer: &Url, resolution_kind: ResolutionKind| {
        let maybe_import_source = self
          .specifier_unfurler
          .unfurl_specifier_reporting_diagnostic(
            referrer,
            import_source,
            resolution_kind,
            text_info,
            PositionOrSourceRangeRef::PositionRange(
              &deno_graph::PositionRange::zeroed(),
            ),
            diagnostic_reporter,
          );
        maybe_import_source.unwrap_or_else(|| import_source.to_string())
      };
    let jsx_import_source = jsx_config
      .and_then(|c| c.import_source.as_ref())
      .map(|jsx_import_source| {
        unfurl_import_source(
          &jsx_import_source.specifier,
          &jsx_import_source.base,
          ResolutionKind::Execution,
        )
      });
    let jsx_import_source_types = jsx_config
      .and_then(|c| c.import_source_types.as_ref())
      .map(|jsx_import_source_types| {
        unfurl_import_source(
          &jsx_import_source_types.specifier,
          &jsx_import_source_types.base,
          ResolutionKind::Types,
        )
      });
    let classic_options = match &transpile_options.jsx {
      None => Some(Cow::Owned(deno_ast::JsxClassicOptions::default())),
      Some(deno_ast::JsxRuntime::Classic(classic_options)) => {
        Some(Cow::Borrowed(classic_options))
      }
      Some(
        deno_ast::JsxRuntime::Precompile(_)
        | deno_ast::JsxRuntime::Automatic(_),
      ) => None,
    };
    Ok(JsxFolderOptions {
      jsx_runtime,
      jsx_classic: classic_options,
      jsx_import_source,
      jsx_import_source_types,
    })
  }
}

#[cfg(test)]
mod test {
  use std::path::PathBuf;

  use deno_path_util::url_from_file_path;
  use deno_resolver::factory::ResolverFactory;
  use deno_resolver::factory::ResolverFactoryOptions;
  use deno_resolver::factory::WorkspaceFactory;
  use deno_resolver::factory::WorkspaceFactoryOptions;
  use pretty_assertions::assert_eq;
  use sys_traits::FsCreateDirAll;
  use sys_traits::FsWrite;
  use sys_traits::impls::InMemorySys;

  use super::*;

  #[tokio::test]
  async fn test_module_content_jsx() {
    run_test(&[
      (
        "/deno.json",
        r#"{ "nodeModulesDir": "manual", "workspace": ["package-a", "package-b", "package-c", "package-d"] }"#,
        None,
      ),
      (
        "/package-a/deno.json",
        r#"{ "compilerOptions": {
        "jsx": "react-jsx",
        "jsxImportSource": "react",
        "jsxImportSourceTypes": "@types/react",
      },
      "imports": {
        "react": "npm:react"
        "@types/react": "npm:@types/react"
      }
    }"#,
        None,
      ),
      (
        "/package-b/deno.json",
        r#"{
        "compilerOptions": { "jsx": "react-jsx" },
        "imports": {
          "react": "npm:react"
          "@types/react": "npm:@types/react"
        }
      }"#,
        None,
      ),
      (
        "/package-c/deno.json",
        r#"{
        "compilerOptions": {
          "jsx": "precompile",
          "jsxImportSource": "react",
          "jsxImportSourceTypes": "@types/react",
        },
        "imports": {
          "react": "npm:react"
          "@types/react": "npm:@types/react"
        }
      }"#,
        None,
      ),
      (
        "/package-d/deno.json",
        r#"{
        "compilerOptions": { "jsx": "react" },
        "imports": {
          "react": "npm:react"
          "@types/react": "npm:@types/react"
        }
      }"#,
        None,
      ),
      (
        "/package-a/main.tsx",
        "export const component = <div></div>;",
        Some(
          "/** @jsxRuntime automatic *//** @jsxImportSource npm:react *//** @jsxImportSourceTypes npm:@types/react */export const component = <div></div>;",
        ),
      ),
      (
        "/package-b/main.tsx",
        "export const componentB = <div></div>;",
        Some(
          "/** @jsxRuntime automatic *//** @jsxImportSource npm:react *//** @jsxImportSourceTypes npm:react */export const componentB = <div></div>;",
        ),
      ),
      (
        "/package-a/other.tsx",
        "/** @jsxImportSource npm:preact */
        /** @jsxFragmentFactory h1 */
        /** @jsxImportSourceTypes npm:@types/example */
        /** @jsxFactory h2 */
        /** @jsxRuntime automatic */
        export const component = <div></div>;",
        Some(
          "/** @jsxImportSource npm:preact */
        /** @jsxFragmentFactory h1 */
        /** @jsxImportSourceTypes npm:@types/example */
        /** @jsxFactory h2 */
        /** @jsxRuntime automatic */
        export const component = <div></div>;",
        ),
      ),
      (
        "/package-c/main.tsx",
        "export const component = <div></div>;",
        Some(
          "/** @jsxRuntime automatic *//** @jsxImportSource npm:react *//** @jsxImportSourceTypes npm:@types/react */export const component = <div></div>;",
        ),
      ),
      (
        "/package-d/main.tsx",
        "export const component = <div></div>;",
        Some(
          "/** @jsxRuntime classic *//** @jsxFactory React.createElement *//** @jsxFragmentFactory React.Fragment */export const component = <div></div>;",
        ),
      ),
    ]).await;
  }

  fn get_path(path: &str) -> PathBuf {
    PathBuf::from(if cfg!(windows) {
      format!("C:{}", path.replace('/', "\\"))
    } else {
      path.to_string()
    })
  }

  async fn run_test(
    files: &[(&'static str, &'static str, Option<&'static str>)],
  ) {
    let in_memory_sys = InMemorySys::default();
    for (path, text, _) in files {
      let path = get_path(path);
      in_memory_sys
        .fs_create_dir_all(path.parent().unwrap())
        .unwrap();
      in_memory_sys.fs_write(path, text).unwrap();
    }
    let provider = module_content_provider(in_memory_sys).await;
    for (path, _, expected) in files {
      let Some(expected) = expected else {
        continue;
      };
      let path = get_path(path);
      let bytes = provider
        .resolve_content_maybe_unfurling(
          &ModuleGraph::new(deno_graph::GraphKind::All),
          &Default::default(),
          &path,
          &url_from_file_path(&path).unwrap(),
        )
        .unwrap();
      assert_eq!(String::from_utf8_lossy(&bytes), *expected);
    }
  }

  async fn module_content_provider(
    sys: InMemorySys,
  ) -> ModuleContentProvider<InMemorySys> {
    let cwd = get_path("/");

    let workspace_factory = Arc::new(WorkspaceFactory::new(
      sys.clone(),
      cwd.to_path_buf(),
      WorkspaceFactoryOptions::default(),
    ));
    let resolver_factory = ResolverFactory::new(
      workspace_factory,
      ResolverFactoryOptions {
        package_json_dep_resolution: Some(
          deno_resolver::workspace::PackageJsonDepResolution::Enabled,
        ),
        unstable_sloppy_imports: true,
        ..Default::default()
      },
    );

    let specifier_unfurler = SpecifierUnfurler::new(
      resolver_factory.node_resolver().unwrap().clone(),
      resolver_factory.npm_req_resolver().unwrap().clone(),
      resolver_factory.pkg_json_resolver().clone(),
      resolver_factory
        .workspace_factory()
        .workspace_directory()
        .unwrap()
        .clone(),
      resolver_factory.workspace_resolver().await.unwrap().clone(),
      true,
    );
    ModuleContentProvider::new(
      Arc::new(ParsedSourceCache::default()),
      specifier_unfurler,
      sys,
      resolver_factory
        .compiler_options_resolver()
        .unwrap()
        .clone(),
    )
  }
}