Skip to content
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

feat: JSX Support #3038

Merged
merged 5 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 18 additions & 0 deletions cli/file_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,9 @@ fn map_file_extension(path: &Path) -> msg::MediaType {
None => msg::MediaType::Unknown,
Some(os_str) => match os_str.to_str() {
Some("ts") => msg::MediaType::TypeScript,
Some("tsx") => msg::MediaType::TSX,
Some("js") => msg::MediaType::JavaScript,
Some("jsx") => msg::MediaType::JSX,
Some("mjs") => msg::MediaType::JavaScript,
Some("json") => msg::MediaType::Json,
_ => msg::MediaType::Unknown,
Expand Down Expand Up @@ -1342,6 +1344,10 @@ mod tests {
map_file_extension(Path::new("foo/bar.ts")),
msg::MediaType::TypeScript
);
assert_eq!(
map_file_extension(Path::new("foo/bar.tsx")),
msg::MediaType::TSX
);
assert_eq!(
map_file_extension(Path::new("foo/bar.d.ts")),
msg::MediaType::TypeScript
Expand All @@ -1350,6 +1356,10 @@ mod tests {
map_file_extension(Path::new("foo/bar.js")),
msg::MediaType::JavaScript
);
assert_eq!(
map_file_extension(Path::new("foo/bar.jsx")),
msg::MediaType::JSX
);
assert_eq!(
map_file_extension(Path::new("foo/bar.json")),
msg::MediaType::Json
Expand All @@ -1371,6 +1381,10 @@ mod tests {
map_content_type(Path::new("foo/bar.ts"), None),
msg::MediaType::TypeScript
);
assert_eq!(
map_content_type(Path::new("foo/bar.tsx"), None),
msg::MediaType::TSX
);
assert_eq!(
map_content_type(Path::new("foo/bar.d.ts"), None),
msg::MediaType::TypeScript
Expand All @@ -1379,6 +1393,10 @@ mod tests {
map_content_type(Path::new("foo/bar.js"), None),
msg::MediaType::JavaScript
);
assert_eq!(
map_content_type(Path::new("foo/bar.jsx"), None),
msg::MediaType::JSX
);
assert_eq!(
map_content_type(Path::new("foo/bar.json"), None),
msg::MediaType::Json
Expand Down
10 changes: 7 additions & 3 deletions cli/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,19 @@ pub enum ErrorKind {
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum MediaType {
JavaScript = 0,
TypeScript = 1,
Json = 2,
Unknown = 3,
JSX = 1,
TypeScript = 2,
TSX = 3,
Json = 4,
Unknown = 5,
}

pub fn enum_name_media_type(mt: MediaType) -> &'static str {
match mt {
MediaType::JavaScript => "JavaScript",
MediaType::JSX => "JSX",
MediaType::TypeScript => "TypeScript",
MediaType::TSX => "TSX",
MediaType::Json => "Json",
MediaType::Unknown => "Unknown",
}
Expand Down
4 changes: 2 additions & 2 deletions cli/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,10 @@ impl ThreadSafeState {
msg::MediaType::Json => {
state_.json_compiler.compile_async(state_.clone(), &out)
}
msg::MediaType::TypeScript => {
msg::MediaType::TypeScript | msg::MediaType::TSX => {
state_.ts_compiler.compile_async(state_.clone(), &out)
}
msg::MediaType::JavaScript => {
msg::MediaType::JavaScript | msg::MediaType::JSX => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about this. This will leave the .jsx untouched if the checkJs compiler flag is not set. I can't see that being desirable.

We should have tests around .jsx and we should have an expected behaviour of how they are handled. We also need it consistent with how the compiler behaves, where by default we have allow JS but not check JS, so how does that effect the emit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I absolutely forgot it. Hmm... should we compile .jsx files as TSX?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, not compile as TSX, more that we will always attempt to transpile them, move it to the previous match clause, but I would check that that works as expected, when the checkJs compiler flag is not enabled (which is the default), which the integration test should prove.

if state_.ts_compiler.compile_js {
state_.ts_compiler.compile_async(state_.clone(), &out)
} else {
Expand Down
7 changes: 7 additions & 0 deletions cli/tests/046_jsx_test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function h(factory, props, ...children) {
return {factory, props, children}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not linted properly. We should ensure the linting covers these files as well.

}
const View = () => (
<div class="deno">land</div>
)
console.log(<View />)
1 change: 1 addition & 0 deletions cli/tests/046_jsx_test.tsx.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ factory: [Function: View], props: null, children: [] }
5 changes: 5 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ itest!(_045_proxy {
output: "045_proxy_test.ts.out",
});

itest!(_046_jsx {
args: "run --allow-net --allow-env --allow-run --reload 046_jsx_test.tsx",
output: "046_jsx_test.tsx.out",
});

itest!(async_error {
exit_code: 1,
args: "run --reload async_error.ts",
Expand Down
17 changes: 12 additions & 5 deletions js/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import { writeFileSync } from "./write_file.ts";
// Update carefully!
enum MediaType {
JavaScript = 0,
TypeScript = 1,
Json = 2,
Unknown = 3
JSX = 1,
TypeScript = 2,
TSX = 3,
Json = 4,
Unknown = 5
}

// Startup boilerplate. This is necessary because the compiler has its own
Expand Down Expand Up @@ -198,8 +200,12 @@ function getExtension(fileName: string, mediaType: MediaType): ts.Extension {
switch (mediaType) {
case MediaType.JavaScript:
return ts.Extension.Js;
case MediaType.JSX:
return ts.Extension.Jsx;
case MediaType.TypeScript:
return fileName.endsWith(".d.ts") ? ts.Extension.Dts : ts.Extension.Ts;
case MediaType.TSX:
return ts.Extension.Tsx;
case MediaType.Json:
return ts.Extension.Json;
case MediaType.Unknown:
Expand All @@ -221,7 +227,9 @@ class Host implements ts.CompilerHost {
resolveJsonModule: true,
sourceMap: true,
stripComments: true,
target: ts.ScriptTarget.ESNext
target: ts.ScriptTarget.ESNext,
jsx: ts.JsxEmit.React,
jsxFactory: "h"
};

private _sourceFileCache: Record<string, SourceFile> = {};
Expand Down Expand Up @@ -511,7 +519,6 @@ window.compilerMain = function compilerMain(): void {
window.onmessage = ({ data }: { data: CompilerReq }): void => {
const { rootNames, configPath, config, bundle } = data;
const host = new Host(bundle);

let emitSkipped = true;
let diagnostics: ts.Diagnostic[] | undefined;

Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"sourceMap": true,
"strict": true,
"target": "esnext",
"jsx": "react",
"jsxFactory": "h",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am concerned we are not using the default here of React.createElement. I can understand why, but it will be potentially surprising to users that we aren't using the default. At the very least I would like to see updates to the manual along with this PR before we merge it explaining how it is anticipated to be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think either is OK. React.creaetElement may rather be useful for real use. react and react-dom/server can be imported in Deno via dev.jspm.io.

"types": []
},
"files": [
Expand Down