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

Update to Prettier 2 and use ES Private Fields #4498

Merged
merged 13 commits into from
Mar 28, 2020
  •  
  •  
  •  
88 changes: 44 additions & 44 deletions cli/js/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,37 @@ function copyBytes(dst: Uint8Array, src: Uint8Array, off = 0): number {
}

export class Buffer implements Reader, SyncReader, Writer, SyncWriter {
private buf: Uint8Array; // contents are the bytes buf[off : len(buf)]
private off = 0; // read at buf[off], write at buf[buf.byteLength]
#buf: Uint8Array; // contents are the bytes buf[off : len(buf)]
#off = 0; // read at buf[off], write at buf[buf.byteLength]

constructor(ab?: ArrayBuffer) {
if (ab == null) {
this.buf = new Uint8Array(0);
this.#buf = new Uint8Array(0);
return;
}

this.buf = new Uint8Array(ab);
this.#buf = new Uint8Array(ab);
}

bytes(): Uint8Array {
return this.buf.subarray(this.off);
return this.#buf.subarray(this.#off);
}

toString(): string {
const decoder = new TextDecoder();
return decoder.decode(this.buf.subarray(this.off));
return decoder.decode(this.#buf.subarray(this.#off));
}

empty(): boolean {
return this.buf.byteLength <= this.off;
return this.#buf.byteLength <= this.#off;
}

get length(): number {
return this.buf.byteLength - this.off;
return this.#buf.byteLength - this.#off;
}

get capacity(): number {
return this.buf.buffer.byteLength;
return this.#buf.buffer.byteLength;
}

truncate(n: number): void {
Expand All @@ -69,27 +69,27 @@ export class Buffer implements Reader, SyncReader, Writer, SyncWriter {
if (n < 0 || n > this.length) {
throw Error("bytes.Buffer: truncation out of range");
}
this._reslice(this.off + n);
this.#reslice(this.#off + n);
}

reset(): void {
this._reslice(0);
this.off = 0;
this.#reslice(0);
this.#off = 0;
}

private _tryGrowByReslice(n: number): number {
const l = this.buf.byteLength;
#tryGrowByReslice = (n: number): number => {
const l = this.#buf.byteLength;
if (n <= this.capacity - l) {
this._reslice(l + n);
this.#reslice(l + n);
return l;
}
return -1;
}
};

private _reslice(len: number): void {
assert(len <= this.buf.buffer.byteLength);
this.buf = new Uint8Array(this.buf.buffer, 0, len);
}
#reslice = (len: number): void => {
assert(len <= this.#buf.buffer.byteLength);
this.#buf = new Uint8Array(this.#buf.buffer, 0, len);
};

readSync(p: Uint8Array): number | EOF {
if (this.empty()) {
Expand All @@ -101,8 +101,8 @@ export class Buffer implements Reader, SyncReader, Writer, SyncWriter {
}
return EOF;
}
const nread = copyBytes(p, this.buf.subarray(this.off));
this.off += nread;
const nread = copyBytes(p, this.#buf.subarray(this.#off));
this.#off += nread;
return nread;
}

Expand All @@ -112,23 +112,23 @@ export class Buffer implements Reader, SyncReader, Writer, SyncWriter {
}

writeSync(p: Uint8Array): number {
const m = this._grow(p.byteLength);
return copyBytes(this.buf, p, m);
const m = this.#grow(p.byteLength);
return copyBytes(this.#buf, p, m);
}

write(p: Uint8Array): Promise<number> {
const n = this.writeSync(p);
return Promise.resolve(n);
}

private _grow(n: number): number {
#grow = (n: number): number => {
const m = this.length;
// If buffer is empty, reset to recover space.
if (m === 0 && this.off !== 0) {
if (m === 0 && this.#off !== 0) {
this.reset();
}
// Fast: Try to grow by means of a reslice.
const i = this._tryGrowByReslice(n);
const i = this.#tryGrowByReslice(n);
if (i >= 0) {
return i;
}
Expand All @@ -138,41 +138,41 @@ export class Buffer implements Reader, SyncReader, Writer, SyncWriter {
// ArrayBuffer. We only need m+n <= c to slide, but
// we instead let capacity get twice as large so we
// don't spend all our time copying.
copyBytes(this.buf, this.buf.subarray(this.off));
copyBytes(this.#buf, this.#buf.subarray(this.#off));
} else if (c > MAX_SIZE - c - n) {
throw new Error("The buffer cannot be grown beyond the maximum size.");
} else {
// Not enough space anywhere, we need to allocate.
const buf = new Uint8Array(2 * c + n);
copyBytes(buf, this.buf.subarray(this.off));
this.buf = buf;
copyBytes(buf, this.#buf.subarray(this.#off));
this.#buf = buf;
}
// Restore this.off and len(this.buf).
this.off = 0;
this._reslice(m + n);
// Restore this.#off and len(this.#buf).
this.#off = 0;
this.#reslice(m + n);
return m;
}
};

grow(n: number): void {
if (n < 0) {
throw Error("Buffer.grow: negative count");
}
const m = this._grow(n);
this._reslice(m);
const m = this.#grow(n);
this.#reslice(m);
}

async readFrom(r: Reader): Promise<number> {
let n = 0;
while (true) {
try {
const i = this._grow(MIN_READ);
this._reslice(i);
const fub = new Uint8Array(this.buf.buffer, i);
const i = this.#grow(MIN_READ);
this.#reslice(i);
const fub = new Uint8Array(this.#buf.buffer, i);
const nread = await r.read(fub);
if (nread === EOF) {
return n;
}
this._reslice(i + nread);
this.#reslice(i + nread);
n += nread;
} catch (e) {
return n;
Expand All @@ -184,14 +184,14 @@ export class Buffer implements Reader, SyncReader, Writer, SyncWriter {
let n = 0;
while (true) {
try {
const i = this._grow(MIN_READ);
this._reslice(i);
const fub = new Uint8Array(this.buf.buffer, i);
const i = this.#grow(MIN_READ);
this.#reslice(i);
const fub = new Uint8Array(this.#buf.buffer, i);
const nread = r.readSync(fub);
if (nread === EOF) {
return n;
}
this._reslice(i + nread);
this.#reslice(i + nread);
n += nread;
} catch (e) {
return n;
Expand Down
2 changes: 1 addition & 1 deletion cli/js/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface BuildInfo {

export const build: BuildInfo = {
arch: "" as Arch,
os: "" as OperatingSystem
os: "" as OperatingSystem,
};

export function setBuildInfo(os: OperatingSystem, arch: Arch): void {
Expand Down
2 changes: 1 addition & 1 deletion cli/js/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function code(open: number, close: number): Code {
return {
open: `\x1b[${open}m`,
close: `\x1b[${close}m`,
regexp: new RegExp(`\\x1b\\[${close}m`, "g")
regexp: new RegExp(`\\x1b\\[${close}m`, "g"),
};
}

Expand Down
44 changes: 22 additions & 22 deletions cli/js/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import {
defaultBundlerOptions,
defaultRuntimeCompileOptions,
defaultTranspileOptions,
Host
Host,
} from "./compiler/host.ts";
import {
processImports,
processLocalImports,
resolveModules
resolveModules,
} from "./compiler/imports.ts";
import {
createWriteFile,
Expand All @@ -35,7 +35,7 @@ import {
ignoredDiagnostics,
WriteFileState,
processConfigureResponse,
base64ToUint8Array
base64ToUint8Array,
} from "./compiler/util.ts";
import { Diagnostic, DiagnosticItem } from "./diagnostics.ts";
import { fromTypeScriptDiagnostic } from "./diagnostics_util.ts";
Expand Down Expand Up @@ -93,7 +93,7 @@ async function compile(
const { bundle, config, configPath, outFile, rootNames, target } = request;
util.log(">>> compile start", {
rootNames,
type: CompilerRequestType[request.type]
type: CompilerRequestType[request.type],
});

// When a programme is emitted, TypeScript will call `writeFile` with
Expand All @@ -108,14 +108,14 @@ async function compile(
bundle,
host: undefined,
outFile,
rootNames
rootNames,
};
const writeFile = createWriteFile(state);

const host = (state.host = new Host({
bundle,
target,
writeFile
writeFile,
}));
let diagnostics: readonly ts.Diagnostic[] | undefined;

Expand All @@ -129,7 +129,7 @@ async function compile(
// requesting those from the privileged side, populating the in memory
// cache which will be used by the host, before resolving.
const resolvedRootModules = await processImports(
rootNames.map(rootName => [rootName, rootName]),
rootNames.map((rootName) => [rootName, rootName]),
undefined,
bundle || host.getCompilationSettings().checkJs
);
Expand All @@ -143,7 +143,7 @@ async function compile(
rootNames,
options,
host,
oldProgram: TS_SNAPSHOT_PROGRAM
oldProgram: TS_SNAPSHOT_PROGRAM,
});

diagnostics = ts
Expand Down Expand Up @@ -171,12 +171,12 @@ async function compile(
emitSkipped,
diagnostics: diagnostics.length
? fromTypeScriptDiagnostic(diagnostics)
: undefined
: undefined,
};

util.log("<<< compile end", {
rootNames,
type: CompilerRequestType[request.type]
type: CompilerRequestType[request.type],
});

return result;
Expand All @@ -190,7 +190,7 @@ async function runtimeCompile(
util.log(">>> runtime compile start", {
rootName,
bundle,
sources: sources ? Object.keys(sources) : undefined
sources: sources ? Object.keys(sources) : undefined,
});

// resolve the root name, if there are sources, the root name does not
Expand Down Expand Up @@ -232,7 +232,7 @@ async function runtimeCompile(
const resolvedNames = resolveModules(additionalFiles);
rootNames.push(
...(await processImports(
resolvedNames.map(rn => [rn, rn]),
resolvedNames.map((rn) => [rn, rn]),
undefined,
checkJsImports
))
Expand All @@ -246,14 +246,14 @@ async function runtimeCompile(
rootNames,
sources,
emitMap: {},
emitBundle: undefined
emitBundle: undefined,
};
const writeFile = createWriteFile(state);

const host = (state.host = new Host({
bundle,
target,
writeFile
writeFile,
}));
const compilerOptions = [defaultRuntimeCompileOptions];
if (convertedOptions) {
Expand All @@ -268,7 +268,7 @@ async function runtimeCompile(
rootNames,
options: host.getCompilationSettings(),
host,
oldProgram: TS_SNAPSHOT_PROGRAM
oldProgram: TS_SNAPSHOT_PROGRAM,
});

if (bundle) {
Expand All @@ -288,7 +288,7 @@ async function runtimeCompile(
rootName,
sources: sources ? Object.keys(sources) : undefined,
bundle,
emitMap: Object.keys(state.emitMap)
emitMap: Object.keys(state.emitMap),
});

const maybeDiagnostics = diagnostics.length
Expand Down Expand Up @@ -320,7 +320,7 @@ function runtimeTranspile(
inputText,
{
fileName,
compilerOptions
compilerOptions,
}
);
result[fileName] = { source, map };
Expand All @@ -329,7 +329,7 @@ function runtimeTranspile(
}

async function tsCompilerOnMessage({
data: request
data: request,
}: {
data: CompilerRequest;
}): Promise<void> {
Expand Down Expand Up @@ -364,7 +364,7 @@ async function tsCompilerOnMessage({
}

async function wasmCompilerOnMessage({
data: binary
data: binary,
}: {
data: string;
}): Promise<void> {
Expand Down Expand Up @@ -411,12 +411,12 @@ Object.defineProperties(globalThis, {
value: bootstrapWasmCompilerRuntime,
enumerable: false,
writable: false,
configurable: false
configurable: false,
},
bootstrapTsCompilerRuntime: {
value: bootstrapTsCompilerRuntime,
enumerable: false,
writable: false,
configurable: false
}
configurable: false,
},
});
Loading