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

refactor(fmt): prepare for noUncheckedIndexedAccess #4276

Merged
merged 4 commits into from
Mar 5, 2024
Merged
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
30 changes: 13 additions & 17 deletions fmt/printf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class Printf {
this.flags = new Flags();
const flags = this.flags;
for (; this.i < this.format.length; ++this.i) {
const c = this.format[this.i];
const c = this.format[this.i]!;
switch (this.state) {
case State.PERCENT:
switch (c) {
Expand Down Expand Up @@ -352,7 +352,7 @@ class Printf {
handleWidthAndPrecision(flags: Flags) {
const fmt = this.format;
for (; this.i !== this.format.length; ++this.i) {
const c = fmt[this.i];
const c = fmt[this.i]!;
switch (this.state) {
case State.WIDTH:
switch (c) {
Expand Down Expand Up @@ -418,7 +418,7 @@ class Printf {
break;
}
positional *= 10;
const val = parseInt(format[this.i]);
const val = parseInt(format[this.i]!, 10);
if (isNaN(val)) {
//throw new Error(
// `invalid character in positional: ${format}[${format[this.i]}]`
Expand Down Expand Up @@ -453,7 +453,7 @@ class Printf {
/** Handle verb */
handleVerb() {
const verb = this.format[this.i];
this.verb = verb;
this.verb = verb || this.verb;
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
if (this.tmpError) {
this.buf += this.tmpError;
this.tmpError = undefined;
Expand Down Expand Up @@ -691,20 +691,18 @@ class Printf {
if (!m) {
throw Error("can't happen, bug");
}
let fractional = m[F.fractional];
const precision = this.flags.precision !== -1
? this.flags.precision
: DEFAULT_PRECISION;
let rounding = false;
[fractional, rounding] = this.roundFractionToPrecision(
fractional,
const [fractional, rounding] = this.roundFractionToPrecision(
m[F.fractional] || "",
precision,
);

let e = m[F.exponent];
let esign = m[F.esign];
let e = m[F.exponent]!;
let esign = m[F.esign]!;
// scientific notation output with exponent padded to minlen 2
let mantissa = parseInt(m[F.mantissa]);
let mantissa = parseInt(m[F.mantissa]!);
if (rounding) {
mantissa += 1;
if (10 <= mantissa) {
Expand Down Expand Up @@ -737,8 +735,8 @@ class Printf {
}

const t = n.toExponential().split("e");
let m = t[0].replace(".", "");
const e = parseInt(t[1]);
let m = t[0]!.replace(".", "");
const e = parseInt(t[1]!);
if (e < 0) {
let nStr = "0.";
for (let i = 0; i !== Math.abs(e) - 1; ++i) {
Expand All @@ -755,9 +753,7 @@ class Printf {
}
// avoiding sign makes padding easier
const val = expandNumber(Math.abs(n)) as string;
const arr = val.split(".");
let dig = arr[0];
let fractional = arr[1];
let [dig, fractional] = val.split(".") as [string, string];

const precision = this.flags.precision !== -1
? this.flags.precision
Expand Down Expand Up @@ -814,7 +810,7 @@ class Printf {
throw Error("can't happen");
}

const X = parseInt(m[F.exponent]) * (m[F.esign] === "-" ? -1 : 1);
const X = parseInt(m[F.exponent]!) * (m[F.esign] === "-" ? -1 : 1);
let nStr = "";
if (P > X && X >= -4) {
this.flags.precision = P - (X + 1);
Expand Down