Skip to content

Commit

Permalink
fix(compiler): JSX compilation and provide better error message (deno…
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Jun 15, 2020
1 parent 0ffc99a commit b1893e6
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
61 changes: 56 additions & 5 deletions cli/global_state.rs
Expand Up @@ -9,6 +9,7 @@ use crate::module_graph::ModuleGraphFile;
use crate::module_graph::ModuleGraphLoader;
use crate::msg;
use crate::msg::MediaType;
use crate::op_error::OpError;
use crate::permissions::Permissions;
use crate::state::exit_unstable;
use crate::tsc::CompiledModule;
Expand Down Expand Up @@ -203,7 +204,16 @@ impl GlobalState {
};

let compiled_module = if was_compiled {
state1.ts_compiler.get_compiled_module(&out.url)?
state1
.ts_compiler
.get_compiled_module(&out.url)
.map_err(|e| {
let msg = e.to_string();
OpError::other(format!(
"Failed to get compiled source code of {}.\nReason: {}",
out.url, msg
))
})?
} else {
CompiledModule {
code: String::from_utf8(out.source_code.clone())?,
Expand Down Expand Up @@ -245,12 +255,14 @@ impl GlobalState {
}

/// Determine if TS compiler should be run with `allowJs` setting on. This
/// is the case when there's a JavaScript file with non-JavaScript import.
/// is the case when there's either:
/// - a JavaScript file with non-JavaScript import
/// - JSX import
fn should_allow_js(module_graph_files: &[&ModuleGraphFile]) -> bool {
module_graph_files.iter().any(|module_file| {
if module_file.media_type != (MediaType::JavaScript as i32) {
false
} else {
if module_file.media_type == (MediaType::JSX as i32) {
true
} else if module_file.media_type == (MediaType::JavaScript as i32) {
module_file.imports.iter().any(|import_desc| {
let import_file = module_graph_files
.iter()
Expand All @@ -263,6 +275,8 @@ fn should_allow_js(module_graph_files: &[&ModuleGraphFile]) -> bool {
|| media_type == (MediaType::TSX as i32)
|| media_type == (MediaType::JSX as i32)
})
} else {
false
}
})
}
Expand Down Expand Up @@ -342,6 +356,43 @@ fn test_should_allow_js() {
},
],));

assert!(should_allow_js(&[
&ModuleGraphFile {
specifier: "file:///some/file.jsx".to_string(),
url: "file:///some/file.jsx".to_string(),
redirect: None,
filename: "some/file.jsx".to_string(),
imports: vec![],
referenced_files: vec![],
lib_directives: vec![],
types_directives: vec![],
type_headers: vec![],
media_type: MediaType::JSX as i32,
source_code: "function foo() {}".to_string(),
},
&ModuleGraphFile {
specifier: "file:///some/file.ts".to_string(),
url: "file:///some/file.ts".to_string(),
redirect: None,
filename: "some/file.ts".to_string(),
imports: vec![ImportDescriptor {
specifier: "./file.jsx".to_string(),
resolved_specifier: ModuleSpecifier::resolve_url(
"file:///some/file.jsx",
)
.unwrap(),
type_directive: None,
resolved_type_directive: None,
}],
referenced_files: vec![],
lib_directives: vec![],
types_directives: vec![],
type_headers: vec![],
media_type: MediaType::TypeScript as i32,
source_code: "function foo() {}".to_string(),
},
]));

assert!(!should_allow_js(&[
&ModuleGraphFile {
specifier: "file:///some/file.js".to_string(),
Expand Down
5 changes: 5 additions & 0 deletions cli/tests/integration_tests.rs
Expand Up @@ -2015,6 +2015,11 @@ itest!(ts_import_from_js {
http_server: true,
});

itest!(jsx_import_from_ts {
args: "run --quiet --reload jsx_import_from_ts.ts",
output: "jsx_import_from_ts.ts.out",
});

itest!(single_compile_with_reload {
args: "run --reload --allow-read single_compile_with_reload.ts",
output: "single_compile_with_reload.ts.out",
Expand Down
11 changes: 11 additions & 0 deletions cli/tests/jsx_import_from_ts.App.jsx
@@ -0,0 +1,11 @@
const React = {
createElement() {}
}

export default function app() {
return (
<div>
<h2>asdf</h2>
</div>
);
}
3 changes: 3 additions & 0 deletions cli/tests/jsx_import_from_ts.ts
@@ -0,0 +1,3 @@
import app from "./jsx_import_from_ts.App.jsx";

console.log(app);
1 change: 1 addition & 0 deletions cli/tests/jsx_import_from_ts.ts.out
@@ -0,0 +1 @@
[Function: app]

0 comments on commit b1893e6

Please sign in to comment.