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

[WIP] refactor(unstable): change return types in runtime compiler APIs #7496

Closed
wants to merge 2 commits into from
Closed
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
32 changes: 29 additions & 3 deletions cli/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::colors;
use regex::Regex;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;
use std::error::Error;
use std::fmt;

Expand Down Expand Up @@ -160,6 +162,21 @@ impl<'de> Deserialize<'de> for DiagnosticCategory {
}
}

impl Serialize for DiagnosticCategory {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let value = match self {
DiagnosticCategory::Warning => 0,
DiagnosticCategory::Error => 1,
DiagnosticCategory::Suggestion => 2,
DiagnosticCategory::Message => 3,
};
Serialize::serialize(&value, serializer)
}
}

impl From<i64> for DiagnosticCategory {
fn from(value: i64) -> Self {
match value {
Expand All @@ -172,7 +189,7 @@ impl From<i64> for DiagnosticCategory {
}
}

#[derive(Debug, Deserialize, Clone)]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DiagnosticMessageChain {
message_text: String,
Expand All @@ -199,14 +216,14 @@ impl DiagnosticMessageChain {
}
}

#[derive(Debug, Deserialize, Clone)]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Position {
pub line: u64,
pub character: u64,
}

#[derive(Debug, Deserialize, Clone)]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Diagnostic {
category: DiagnosticCategory,
Expand Down Expand Up @@ -359,6 +376,15 @@ impl<'de> Deserialize<'de> for Diagnostics {
}
}

impl Serialize for Diagnostics {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
Serialize::serialize(&self.0, serializer)
}
}

impl fmt::Display for Diagnostics {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut i = 0;
Expand Down
24 changes: 18 additions & 6 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ declare namespace Deno {
* user friendly format.
*
* ```ts
* const [diagnostics, result] = Deno.compile("file_with_compile_issues.ts");
* const {diagnostics, emitMap} = Deno.compile("file_with_compile_issues.ts");
* console.table(diagnostics); // Prints raw diagnostic data
* console.log(Deno.formatDiagnostics(diagnostics)); // User friendly output of diagnostics
* ```
Expand Down Expand Up @@ -500,9 +500,15 @@ declare namespace Deno {
* the module name will be used to determine the media type of the module.
*
* ```ts
* const [ maybeDiagnostics1, output1 ] = await Deno.compile("foo.ts");
*
* const [ maybeDiagnostics2, output2 ] = await Deno.compile("/foo.ts", {
* const {
* diagnostics: maybeDiagnostics1,
* emitMap: output1
* } = await Deno.compile("foo.ts");
*
* const {
* diagnostics: maybeDiagnostics2,
* emitMap: output2
* } = await Deno.compile("/foo.ts", {
* "/foo.ts": `export * from "./bar.ts";`,
* "/bar.ts": `export const bar = "bar";`
* });
Expand All @@ -524,7 +530,10 @@ declare namespace Deno {
rootName: string,
sources?: Record<string, string>,
options?: CompilerOptions,
Copy link
Member Author

Choose a reason for hiding this comment

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

Removing Deno.transpileOnly() in favor of noCheck option means we need to change options?: CompilerOptions into:

options?: {
  noCheck?: boolean,
  compilerOptions?: CompilerOptions
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect there are other non-tsconfig related options that we will want to make in here as well, like import map, so yeah, let's change it.

): Promise<[Diagnostic[] | undefined, Record<string, string>]>;
): Promise<{
diagnostics: Diagnostic[] | undefined,
Copy link
Member Author

Choose a reason for hiding this comment

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

add ignoredCompilerOptions field

emitMap: Record<string, string>
Copy link
Member Author

Choose a reason for hiding this comment

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

rename to emittedFiles

}>;

/** **UNSTABLE**: new API, yet to be vetted.
*
Expand Down Expand Up @@ -567,7 +576,10 @@ declare namespace Deno {
rootName: string,
sources?: Record<string, string>,
options?: CompilerOptions,
): Promise<[Diagnostic[] | undefined, string]>;
): Promise<{
diagnostics: Diagnostic[] | undefined,
output: string
}>;

/** **UNSTABLE**: Should not have same name as `window.location` type. */
interface Location {
Expand Down
18 changes: 10 additions & 8 deletions cli/ops/runtime_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::tsc::runtime_transpile;
use deno_core::error::AnyError;
use deno_core::BufVec;
use deno_core::OpState;
use futures::FutureExt;
use serde::Deserialize;
use serde_json::Value;
use std::cell::RefCell;
Expand Down Expand Up @@ -41,27 +40,30 @@ async fn op_compile(
let state = state.borrow();
state.borrow::<Permissions>().clone()
};
let fut = if args.bundle {
runtime_bundle(

let response = if args.bundle {
let r = runtime_bundle(
&global_state,
permissions,
&args.root_name,
&args.sources,
&args.options,
)
.boxed_local()
.await?;
serde_json::to_value(r)?
} else {
runtime_compile(
let r = runtime_compile(
&global_state,
permissions,
&args.root_name,
&args.sources,
&args.options,
)
.boxed_local()
.await?;
serde_json::to_value(r)?
};
let result = fut.await?;
Ok(result)

Ok(response)
}

#[derive(Deserialize, Debug)]
Expand Down
33 changes: 15 additions & 18 deletions cli/rt/40_compiler_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
return opTranspile(payload);
}

// TODO(bartlomieju): change return type to interface?
async function compile(
rootName,
sources,
Expand All @@ -52,22 +51,20 @@
sources: !!sources,
options,
});
const result = await opCompile(payload);
util.assert(result.emitMap);
const maybeDiagnostics = result.diagnostics.length === 0
? undefined
: result.diagnostics;

const emitMap = {};
const { diagnostics, emitMap } = await opCompile(payload);
util.assert(emitMap);

for (const [key, emittedSource] of Object.entries(result.emitMap)) {
emitMap[key] = emittedSource.contents;
const processedEmitMap = {};
for (const [key, emittedSource] of Object.entries(emitMap)) {
processedEmitMap[key] = emittedSource.contents;
}

return [maybeDiagnostics, emitMap];
return {
diagnostics: diagnostics.length === 0 ? undefined : diagnostics,
emitMap: processedEmitMap,
};
}

// TODO(bartlomieju): change return type to interface?
async function bundle(
rootName,
sources,
Expand All @@ -84,12 +81,12 @@
sources: !!sources,
options,
});
const result = await opCompile(payload);
util.assert(result.output);
const maybeDiagnostics = result.diagnostics.length === 0
? undefined
: result.diagnostics;
return [maybeDiagnostics, result.output];
const { diagnostics, output } = await opCompile(payload);
util.assert(output);
return {
diagnostics: diagnostics.length === 0 ? undefined : diagnostics,
emitMap: processedEmitMap,
};
}

window.__bootstrap.compilerApi = {
Expand Down
Loading