Skip to content

fix(publish): print a warning when .jsx or .tsx is imported #22631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions cli/tools/registry/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use deno_ast::diagnostics::DiagnosticSnippetHighlightStyle;
use deno_ast::diagnostics::DiagnosticSourcePos;
use deno_ast::diagnostics::DiagnosticSourceRange;
use deno_ast::swc::common::util::take::Take;
use deno_ast::SourcePos;
use deno_ast::SourceRanged;
use deno_ast::SourceTextInfo;
use deno_core::anyhow::anyhow;
use deno_core::error::AnyError;
Expand All @@ -31,7 +33,10 @@ impl PublishDiagnosticsCollector {
pub fn print_and_error(&self) -> Result<(), AnyError> {
let mut errors = 0;
let mut has_slow_types_errors = false;
let diagnostics = self.diagnostics.lock().unwrap().take();
let mut diagnostics = self.diagnostics.lock().unwrap().take();

diagnostics.sort_by_cached_key(|d| d.sorting_key());

for diagnostic in diagnostics {
eprint!("{}", diagnostic.display());
if matches!(diagnostic.level(), DiagnosticLevel::Error) {
Expand Down Expand Up @@ -92,6 +97,38 @@ pub enum PublishDiagnostic {
text_info: SourceTextInfo,
referrer: deno_graph::Range,
},
UnsupportedJsxTsx {
specifier: Url,
},
}

impl PublishDiagnostic {
fn sorting_key(&self) -> (String, String, Option<SourcePos>) {
let loc = self.location();

let (specifier, source_pos) = match loc {
DiagnosticLocation::Module { specifier } => (specifier.to_string(), None),
DiagnosticLocation::Path { path } => (path.display().to_string(), None),
DiagnosticLocation::ModulePosition {
specifier,
source_pos,
text_info,
} => (
specifier.to_string(),
Some(match source_pos {
DiagnosticSourcePos::SourcePos(s) => s,
DiagnosticSourcePos::ByteIndex(index) => {
text_info.range().start() + index
}
DiagnosticSourcePos::LineAndCol { line, column } => {
text_info.line_start(line) + column
}
}),
),
};

(self.code().to_string(), specifier, source_pos)
}
}

impl Diagnostic for PublishDiagnostic {
Expand All @@ -107,6 +144,7 @@ impl Diagnostic for PublishDiagnostic {
DuplicatePath { .. } => DiagnosticLevel::Error,
UnsupportedFileType { .. } => DiagnosticLevel::Warning,
InvalidExternalImport { .. } => DiagnosticLevel::Error,
UnsupportedJsxTsx { .. } => DiagnosticLevel::Warning,
}
}

Expand All @@ -119,6 +157,7 @@ impl Diagnostic for PublishDiagnostic {
DuplicatePath { .. } => Cow::Borrowed("case-insensitive-duplicate-path"),
UnsupportedFileType { .. } => Cow::Borrowed("unsupported-file-type"),
InvalidExternalImport { .. } => Cow::Borrowed("invalid-external-import"),
UnsupportedJsxTsx { .. } => Cow::Borrowed("unsupported-jsx-tsx"),
}
}

Expand All @@ -135,6 +174,7 @@ impl Diagnostic for PublishDiagnostic {
Cow::Owned(format!("unsupported file type '{kind}'"))
}
InvalidExternalImport { kind, .. } => Cow::Owned(format!("invalid import to a {kind} specifier")),
UnsupportedJsxTsx { .. } => Cow::Borrowed("JSX and TSX files are currently not supported"),
}
}

Expand Down Expand Up @@ -174,6 +214,9 @@ impl Diagnostic for PublishDiagnostic {
column: referrer.start.character,
},
},
UnsupportedJsxTsx { specifier } => DiagnosticLocation::Module {
specifier: Cow::Borrowed(specifier),
},
}
}

Expand Down Expand Up @@ -221,6 +264,7 @@ impl Diagnostic for PublishDiagnostic {
description: Some("the specifier".into()),
},
}),
PublishDiagnostic::UnsupportedJsxTsx { .. } => None,
}
}

Expand All @@ -237,7 +281,8 @@ impl Diagnostic for PublishDiagnostic {
PublishDiagnostic::UnsupportedFileType { .. } => Some(
Cow::Borrowed("remove the file, or add it to 'publish.exclude' in the config file"),
),
PublishDiagnostic::InvalidExternalImport { .. } => Some(Cow::Borrowed("replace this import with one from jsr or npm, or vendor the dependency into your package"))
PublishDiagnostic::InvalidExternalImport { .. } => Some(Cow::Borrowed("replace this import with one from jsr or npm, or vendor the dependency into your package")),
PublishDiagnostic::UnsupportedJsxTsx { .. } => None,
}
}

Expand Down Expand Up @@ -272,6 +317,9 @@ impl Diagnostic for PublishDiagnostic {
Cow::Borrowed("this specifier is not allowed to be imported on jsr"),
Cow::Borrowed("jsr only supports importing `jsr:`, `npm:`, and `data:` specifiers"),
]),
PublishDiagnostic::UnsupportedJsxTsx { .. } => Cow::Owned(vec![
Cow::Borrowed("follow https://github.com/jsr-io/jsr/issues/24 for updates"),
])
}
}

Expand All @@ -293,6 +341,7 @@ impl Diagnostic for PublishDiagnostic {
PublishDiagnostic::InvalidExternalImport { .. } => {
Some(Cow::Borrowed("https://jsr.io/go/invalid-external-import"))
}
PublishDiagnostic::UnsupportedJsxTsx { .. } => None,
}
}
}
8 changes: 8 additions & 0 deletions cli/tools/registry/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ pub fn create_gzipped_tarball(
source_parser,
diagnostics_collector,
)?;

let media_type = MediaType::from_specifier(&specifier);
if matches!(media_type, MediaType::Jsx | MediaType::Tsx) {
diagnostics_collector.push(PublishDiagnostic::UnsupportedJsxTsx {
specifier: specifier.clone(),
});
}

files.push(PublishableTarballFile {
path_str: path_str.clone(),
specifier: specifier.clone(),
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/publish_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ itest!(jsr_jsonc {
http_server: true,
});

itest!(unsupported_jsx_tsx {
args: "publish --token 'sadfasdf'",
cwd: Some("publish/unsupported_jsx_tsx"),
output: "publish/unsupported_jsx_tsx/mod.out",
envs: env_vars_for_jsr_npm_tests(),
http_server: true,
});

#[test]
fn ignores_gitignore() {
let context = publish_context_builder().build();
Expand Down
Binary file not shown.

Large diffs are not rendered by default.

Binary file not shown.
1 change: 1 addition & 0 deletions tests/testdata/npm/registry/preact/registry.json

Large diffs are not rendered by default.

Binary file not shown.
1 change: 1 addition & 0 deletions tests/testdata/npm/registry/pretty-format/registry.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions tests/testdata/publish/unsupported_jsx_tsx/foo.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { renderToString } from "npm:preact-render-to-string";

export default function render() {
return renderToString(<div>foo.tsx</div>);
}
5 changes: 5 additions & 0 deletions tests/testdata/publish/unsupported_jsx_tsx/foo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { renderToString } from "npm:preact-render-to-string";

export default function render() {
return renderToString(<div>foo.tsx</div>);
}
11 changes: 11 additions & 0 deletions tests/testdata/publish/unsupported_jsx_tsx/jsr.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@foo/bar",
"version": "1.0.0",
"exports": {
".": "./mod.ts"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "npm:preact"
}
}
17 changes: 17 additions & 0 deletions tests/testdata/publish/unsupported_jsx_tsx/mod.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[WILDCARD]
Check file:///[WILDCARD]/publish/unsupported_jsx_tsx/mod.ts
Checking for slow types in the public API...
Check file:///[WILDCARD]/publish/unsupported_jsx_tsx/mod.ts
warning[unsupported-jsx-tsx]: JSX and TSX files are currently not supported
--> [WILDCARD]foo.jsx

info: follow https://github.com/jsr-io/jsr/issues/24 for updates

warning[unsupported-jsx-tsx]: JSX and TSX files are currently not supported
--> [WILDCARD]foo.tsx

info: follow https://github.com/jsr-io/jsr/issues/24 for updates

Publishing @foo/[email protected] ...
Successfully published @foo/[email protected]
Visit http://127.0.0.1:4250/@foo/[email protected] for details
7 changes: 7 additions & 0 deletions tests/testdata/publish/unsupported_jsx_tsx/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import fooTsx from "./foo.tsx";
import fooJsx from "./foo.jsx";

export function renderTsxJsx() {
console.log(fooTsx());
console.log(fooJsx());
}