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

fix: base64 encoding of source maps with emojis #14607

Merged
merged 13 commits into from
May 17, 2022
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ pub fn bundle(
let emit_options: deno_ast::EmitOptions = options.ts_config.into();
let source_map_config = deno_ast::SourceMapConfig {
inline_sources: emit_options.inline_sources,
maybe_base: None,
};

let cm = Rc::new(swc::common::SourceMap::new(
Expand Down
3 changes: 3 additions & 0 deletions cli/tests/testdata/coverage/complex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@ export function ƒ(): number {

// This arrow function should also show up as uncovered.
console.log("%s", () => 1);

// Make sure emojis work properly
console.log("📣❓");
5 changes: 3 additions & 2 deletions cli/tests/testdata/coverage/complex_expected.lcov
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ DA:66,0
DA:67,0
DA:68,1
DA:71,0
LH:22
LF:37
DA:74,1
LH:23
LF:38
end_of_record
2 changes: 1 addition & 1 deletion cli/tests/testdata/coverage/complex_expected.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cover [WILDCARD]/coverage/complex.ts ... 59.459% (22/37)
cover [WILDCARD]/coverage/complex.ts ... 60.526% (23/38)
46 | export function unused(
47 | foo: string,
48 | bar: string,
Expand Down
45 changes: 45 additions & 0 deletions cli/tsc/99_main_compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,43 @@ delete Object.prototype.__proto__;
}
}

// deno-fmt-ignore
const base64abc = [
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d",
"e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "+", "/",
];

/** Taken from https://deno.land/std/encoding/base64.ts */
function convertToBase64(data) {
const uint8 = core.encode(data);
let result = "",
i;
const l = uint8.length;
for (i = 2; i < l; i += 3) {
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
result += base64abc[((uint8[i - 1] & 0x0f) << 2) | (uint8[i] >> 6)];
result += base64abc[uint8[i] & 0x3f];
}
if (i === l + 1) {
// 1 octet yet to write
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[(uint8[i - 2] & 0x03) << 4];
result += "==";
}
if (i === l) {
// 2 octets yet to write
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
result += base64abc[(uint8[i - 1] & 0x0f) << 2];
result += "=";
}
return result;
}

// In the case of the LSP, this is initialized with the assets
// when snapshotting and never added to or removed after that.
/** @type {Map<string, ts.SourceFile>} */
Expand Down Expand Up @@ -485,6 +522,14 @@ delete Object.prototype.__proto__;
* @param {Request} request
*/
function exec({ config, debug: debugFlag, rootNames }) {
// https://github.com/microsoft/TypeScript/issues/49150
ts.base64encode = function (host, input) {
if (host && host.base64encode) {
return host.base64encode(input);
}
return convertToBase64(input);
};

setLogDebug(debugFlag, "TS");
performanceStart();
debug(">>> exec start", { rootNames });
Expand Down
2 changes: 1 addition & 1 deletion cli/tsc/compiler.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ declare global {
namespace ts {
var libs: string[];
var libMap: Map<string, string>;

var base64encode: (host: ts.CompilerHost, input: string) => string;
interface SourceFile {
version?: string;
}
Expand Down