1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

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

This commit adds a warning when .jsx or .tsx is encountered during
publishing.

This is a stop-gap solution before we fix it proper.
This commit is contained in:
Bartek Iwańczuk 2024-02-29 11:54:57 +00:00 committed by GitHub
parent 1dad7bec17
commit 211b3ff244
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 115 additions and 2 deletions

View file

@ -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;
@ -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) {
@ -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 {
@ -107,6 +144,7 @@ impl Diagnostic for PublishDiagnostic {
DuplicatePath { .. } => DiagnosticLevel::Error,
UnsupportedFileType { .. } => DiagnosticLevel::Warning,
InvalidExternalImport { .. } => DiagnosticLevel::Error,
UnsupportedJsxTsx { .. } => DiagnosticLevel::Warning,
}
}
@ -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"),
}
}
@ -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"),
}
}
@ -174,6 +214,9 @@ impl Diagnostic for PublishDiagnostic {
column: referrer.start.character,
},
},
UnsupportedJsxTsx { specifier } => DiagnosticLocation::Module {
specifier: Cow::Borrowed(specifier),
},
}
}
@ -221,6 +264,7 @@ impl Diagnostic for PublishDiagnostic {
description: Some("the specifier".into()),
},
}),
PublishDiagnostic::UnsupportedJsxTsx { .. } => None,
}
}
@ -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,
}
}
@ -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"),
])
}
}
@ -293,6 +341,7 @@ impl Diagnostic for PublishDiagnostic {
PublishDiagnostic::InvalidExternalImport { .. } => {
Some(Cow::Borrowed("https://jsr.io/go/invalid-external-import"))
}
PublishDiagnostic::UnsupportedJsxTsx { .. } => None,
}
}
}

View file

@ -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(),

View file

@ -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();

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,5 @@
import { renderToString } from "npm:preact-render-to-string";
export default function render() {
return renderToString(<div>foo.tsx</div>);
}

View file

@ -0,0 +1,5 @@
import { renderToString } from "npm:preact-render-to-string";
export default function render() {
return renderToString(<div>foo.tsx</div>);
}

View file

@ -0,0 +1,11 @@
{
"name": "@foo/bar",
"version": "1.0.0",
"exports": {
".": "./mod.ts"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "npm:preact"
}
}

View file

@ -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/bar@1.0.0 ...
Successfully published @foo/bar@1.0.0
Visit http://127.0.0.1:4250/@foo/bar@1.0.0 for details

View file

@ -0,0 +1,7 @@
import fooTsx from "./foo.tsx";
import fooJsx from "./foo.jsx";
export function renderTsxJsx() {
console.log(fooTsx());
console.log(fooJsx());
}