1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

fix: deno-types directive should have higher precedence than X-TypeScript-Types header (#6761)

This commit is contained in:
Bartek Iwańczuk 2020-07-24 14:21:36 +02:00 committed by GitHub
parent f162ced76a
commit b45f9a7357
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 6 deletions

View file

@ -1961,6 +1961,12 @@ itest!(type_directives_redirect {
http_server: true, http_server: true,
}); });
itest!(type_headers_deno_types {
args: "run --reload type_headers_deno_types.ts",
output: "type_headers_deno_types.ts.out",
http_server: true,
});
itest!(ts_type_imports { itest!(ts_type_imports {
args: "run --reload ts_type_imports.ts", args: "run --reload ts_type_imports.ts",
output: "ts_type_imports.ts.out", output: "ts_type_imports.ts.out",

View file

@ -0,0 +1,18 @@
/**
* Following import uses two distinct ways to provide types:
* - X-TypeScript-Types headers
* - @deno-types directive
*
* Because "@deno-types" directive must be placed by user explicitly it
* should have higher precedence than type header.
*
* This is verified by providing conflicting type declaration
* depending on a way. There should be no TS error, otherwise
* it means that wrong type declarations are used (from X-TypeScript-Types)
* header.
*/
// @deno-types="http://127.0.0.1:4545/type_headers_deno_types.foo.d.ts"
import { foo } from "http://127.0.0.1:4545/type_headers_deno_types.foo.js";
foo("hello");

View file

@ -0,0 +1,5 @@
Download http://[WILDCARD]:4545/type_headers_deno_types.foo.js
Download http://[WILDCARD]:4545/type_headers_deno_types.foo.d.ts
Download http://[WILDCARD]:4545/type_headers_deno_types.d.ts
Check [WILDCARD]/type_headers_deno_types.ts
hello

View file

@ -869,11 +869,13 @@ delete Object.prototype.__proto__;
importedFile.mediaType === MediaType.JSX; importedFile.mediaType === MediaType.JSX;
// If JS or JSX perform substitution for types if available // If JS or JSX perform substitution for types if available
if (isJsOrJsx) { if (isJsOrJsx) {
if (importedFile.typeHeaders.length > 0) { // @deno-types has highest precedence, followed by
// X-TypeScript-Types header
if (importDesc.resolvedTypeDirective) {
mappedUrl = importDesc.resolvedTypeDirective;
} else if (importedFile.typeHeaders.length > 0) {
const typeHeaders = importedFile.typeHeaders[0]; const typeHeaders = importedFile.typeHeaders[0];
mappedUrl = typeHeaders.resolvedSpecifier; mappedUrl = typeHeaders.resolvedSpecifier;
} else if (importDesc.resolvedTypeDirective) {
mappedUrl = importDesc.resolvedTypeDirective;
} else if (importedFile.typesDirectives.length > 0) { } else if (importedFile.typesDirectives.length > 0) {
const typeDirective = importedFile.typesDirectives[0]; const typeDirective = importedFile.typesDirectives[0];
mappedUrl = typeDirective.resolvedSpecifier; mappedUrl = typeDirective.resolvedSpecifier;
@ -928,11 +930,13 @@ delete Object.prototype.__proto__;
importedFile.mediaType === MediaType.JSX; importedFile.mediaType === MediaType.JSX;
// If JS or JSX perform substitution for types if available // If JS or JSX perform substitution for types if available
if (isJsOrJsx) { if (isJsOrJsx) {
if (importedFile.typeHeaders.length > 0) { // @deno-types has highest precedence, followed by
// X-TypeScript-Types header
if (importDesc.resolvedTypeDirective) {
mappedUrl = importDesc.resolvedTypeDirective;
} else if (importedFile.typeHeaders.length > 0) {
const typeHeaders = importedFile.typeHeaders[0]; const typeHeaders = importedFile.typeHeaders[0];
mappedUrl = typeHeaders.resolvedSpecifier; mappedUrl = typeHeaders.resolvedSpecifier;
} else if (importDesc.resolvedTypeDirective) {
mappedUrl = importDesc.resolvedTypeDirective;
} else if (importedFile.typesDirectives.length > 0) { } else if (importedFile.typesDirectives.length > 0) {
const typeDirective = importedFile.typesDirectives[0]; const typeDirective = importedFile.typesDirectives[0];
mappedUrl = typeDirective.resolvedSpecifier; mappedUrl = typeDirective.resolvedSpecifier;

View file

@ -293,6 +293,39 @@ pub async fn run_all_servers() {
); );
res res
})) }))
.or(warp::path!("type_headers_deno_types.foo.js").map(|| {
let mut res = Response::new(Body::from("export function foo(text) { console.log(text); }"));
let h = res.headers_mut();
h.insert(
"Content-type",
HeaderValue::from_static("application/javascript"),
);
h.insert(
"X-TypeScript-Types",
HeaderValue::from_static(
"http://localhost:4545/type_headers_deno_types.d.ts",
),
);
res
}))
.or(warp::path!("type_headers_deno_types.d.ts").map(|| {
let mut res = Response::new(Body::from("export function foo(text: number): void;"));
let h = res.headers_mut();
h.insert(
"Content-type",
HeaderValue::from_static("application/typescript"),
);
res
}))
.or(warp::path!("type_headers_deno_types.foo.d.ts").map(|| {
let mut res = Response::new(Body::from("export function foo(text: string): void;"));
let h = res.headers_mut();
h.insert(
"Content-type",
HeaderValue::from_static("application/typescript"),
);
res
}))
.or(warp::path!("cli"/"tests"/"subdir"/"xTypeScriptTypesRedirect.d.ts").map(|| { .or(warp::path!("cli"/"tests"/"subdir"/"xTypeScriptTypesRedirect.d.ts").map(|| {
let mut res = Response::new(Body::from( let mut res = Response::new(Body::from(
"import './xTypeScriptTypesRedirected.d.ts';", "import './xTypeScriptTypesRedirected.d.ts';",