Skip to content

Commit

Permalink
feat: provide referrer when requesting jsx config (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn authored Jan 21, 2025
1 parent 867b61c commit 9d26d04
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 21 deletions.
12 changes: 9 additions & 3 deletions lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,21 @@ struct JsResolveTypesResponse {
}

impl Resolver for JsResolver {
fn default_jsx_import_source(&self) -> Option<String> {
fn default_jsx_import_source(
&self,
_referrer: &ModuleSpecifier,
) -> Option<String> {
self.maybe_default_jsx_import_source.clone()
}

fn default_jsx_import_source_types(&self) -> Option<String> {
fn default_jsx_import_source_types(
&self,
_referrer: &ModuleSpecifier,
) -> Option<String> {
self.maybe_default_jsx_import_source_types.clone()
}

fn jsx_import_source_module(&self) -> &str {
fn jsx_import_source_module(&self, _referrer: &ModuleSpecifier) -> &str {
self
.maybe_jsx_import_source_module
.as_deref()
Expand Down
12 changes: 6 additions & 6 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2694,7 +2694,7 @@ pub(crate) fn parse_js_module_from_module_info(
let has_jsx_import_source_pragma = module_info.jsx_import_source.is_some();
let res = module_info.jsx_import_source.or_else(|| {
maybe_resolver.and_then(|r| {
r.default_jsx_import_source()
r.default_jsx_import_source(&module.specifier)
.map(|import_source| SpecifierWithRange {
text: import_source,
range: PositionRange {
Expand All @@ -2706,7 +2706,7 @@ pub(crate) fn parse_js_module_from_module_info(
});
if let Some(import_source) = res {
let jsx_import_source_module = maybe_resolver
.map(|r| r.jsx_import_source_module())
.map(|r| r.jsx_import_source_module(&module.specifier))
.unwrap_or(DEFAULT_JSX_IMPORT_SOURCE_MODULE);
let specifier_text =
format!("{}/{}", import_source.text, jsx_import_source_module);
Expand All @@ -2733,15 +2733,15 @@ pub(crate) fn parse_js_module_from_module_info(
let mut types_res = module_info.jsx_import_source_types;
if types_res.is_none() && !has_jsx_import_source_pragma {
types_res = maybe_resolver.and_then(|r| {
r.default_jsx_import_source_types().map(|import_source| {
SpecifierWithRange {
r.default_jsx_import_source_types(&module.specifier).map(
|import_source| SpecifierWithRange {
text: import_source,
range: PositionRange {
start: Position::zeroed(),
end: Position::zeroed(),
},
}
})
},
)
});
}
if let Some(import_source_types) = types_res {
Expand Down
53 changes: 46 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ mod tests {
use source::NpmResolvePkgReqsResult;
use source::ResolutionMode;
use source::Source;
use source::DEFAULT_JSX_IMPORT_SOURCE_MODULE;
use std::cell::RefCell;
use std::collections::BTreeMap;

Expand Down Expand Up @@ -1313,11 +1314,14 @@ console.log(a);
struct MockImportMapResolver {}

impl Resolver for MockImportMapResolver {
fn default_jsx_import_source(&self) -> Option<String> {
fn default_jsx_import_source(
&self,
_referrer: &ModuleSpecifier,
) -> Option<String> {
None
}

fn jsx_import_source_module(&self) -> &str {
fn jsx_import_source_module(&self, _referrer: &ModuleSpecifier) -> &str {
source::DEFAULT_JSX_IMPORT_SOURCE_MODULE
}

Expand Down Expand Up @@ -3536,7 +3540,10 @@ export const foo = 'bar';"#,
#[derive(Debug)]
struct R;
impl Resolver for R {
fn default_jsx_import_source(&self) -> Option<String> {
fn default_jsx_import_source(
&self,
_referrer: &ModuleSpecifier,
) -> Option<String> {
Some("https://example.com/preact".into())
}
}
Expand Down Expand Up @@ -3587,7 +3594,10 @@ export const foo = 'bar';"#,
#[derive(Debug)]
struct R;
impl Resolver for R {
fn default_jsx_import_source_types(&self) -> Option<String> {
fn default_jsx_import_source_types(
&self,
_referrer: &ModuleSpecifier,
) -> Option<String> {
Some("https://example.com/preact-types".into())
}
}
Expand Down Expand Up @@ -3634,7 +3644,14 @@ export const foo = 'bar';"#,
#[derive(Debug)]
struct R;
impl Resolver for R {
fn default_jsx_import_source(&self) -> Option<String> {
fn default_jsx_import_source(
&self,
referrer: &ModuleSpecifier,
) -> Option<String> {
assert_eq!(
referrer,
&ModuleSpecifier::parse("file:///a/test01.tsx").unwrap()
);
Some("https://example.com/preact".into())
}
}
Expand Down Expand Up @@ -3679,13 +3696,35 @@ export const foo = 'bar';"#,
#[derive(Debug)]
struct R;
impl Resolver for R {
fn default_jsx_import_source(&self) -> Option<String> {
fn default_jsx_import_source(
&self,
referrer: &ModuleSpecifier,
) -> Option<String> {
assert_eq!(
referrer,
&ModuleSpecifier::parse("file:///a/test01.tsx").unwrap()
);
Some("https://example.com/preact".into())
}

fn default_jsx_import_source_types(&self) -> Option<String> {
fn default_jsx_import_source_types(
&self,
referrer: &ModuleSpecifier,
) -> Option<String> {
assert_eq!(
referrer,
&ModuleSpecifier::parse("file:///a/test01.tsx").unwrap()
);
Some("https://example.com/preact-types".into())
}

fn jsx_import_source_module(&self, referrer: &ModuleSpecifier) -> &str {
assert_eq!(
referrer,
&ModuleSpecifier::parse("file:///a/test01.tsx").unwrap()
);
DEFAULT_JSX_IMPORT_SOURCE_MODULE
}
}

let specifier = ModuleSpecifier::parse("file:///a/test01.tsx").unwrap();
Expand Down
12 changes: 9 additions & 3 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,21 +441,27 @@ pub trait Resolver: fmt::Debug {
/// configured. If this method returns `Some` and a JSX file is encountered
/// that does not have an import source specified as a pragma, this import
/// source will be used instead.
fn default_jsx_import_source(&self) -> Option<String> {
fn default_jsx_import_source(
&self,
_referrer: &ModuleSpecifier,
) -> Option<String> {
None
}

/// An optional method that returns the default JSX types import source if one
/// is configured. If this method returns `Some` and a JSX file is encountered
/// that does not have an types import source specified as a pragma, this
/// types import source will be used instead.
fn default_jsx_import_source_types(&self) -> Option<String> {
fn default_jsx_import_source_types(
&self,
_referrer: &ModuleSpecifier,
) -> Option<String> {
None
}

/// An optional method which returns the JSX import source module which will
/// be appended to any JSX import source pragmas identified.
fn jsx_import_source_module(&self) -> &str {
fn jsx_import_source_module(&self, _referrer: &ModuleSpecifier) -> &str {
DEFAULT_JSX_IMPORT_SOURCE_MODULE
}

Expand Down
4 changes: 2 additions & 2 deletions tests/specs/ecosystem/mrii/rocket_io/0_1_3.test
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ mrii/rocket-io/0.1.3
-- stdout --

-- stderr --
error: Uncaught Error: [ERR_PACKAGE_PATH_NOT_EXPORTED] Package subpath './build/esm/socket' is not defined for types by "exports" in '<global_npm_dir>/socket.io-client/4.7.5/package.json' imported from 'file://<tmpdir>/src/types/socket-reserved-events.ts'
at Object.resolveModuleNameLiterals (ext:deno_tsc/99_main_compiler.js:789:28)
error: Error: [ERR_PACKAGE_PATH_NOT_EXPORTED] Package subpath './build/esm/socket' is not defined for types by "exports" in '<global_npm_dir>/socket.io-client/4.7.5/package.json' imported from 'file://<tmpdir>/src/types/socket-reserved-events.ts'
at Object.resolveModuleNameLiterals (ext:deno_tsc/99_main_compiler.js:794:28)
at resolveModuleNamesWorker (ext:deno_tsc/00_typescript.js:125466:20)
at resolveNamesReusingOldState (ext:deno_tsc/00_typescript.js:125608:14)
at resolveModuleNamesReusingOldState (ext:deno_tsc/00_typescript.js:125564:12)
Expand Down

0 comments on commit 9d26d04

Please sign in to comment.