Skip to content

Commit

Permalink
refactor(fmt): prepare for noUncheckedIndexedAccess (#4276)
Browse files Browse the repository at this point in the history
* refactor: fmt-noUncheckedIndexedAccess

* fix: cr
  • Loading branch information
eryue0220 authored Mar 5, 2024
1 parent 0c6f5e8 commit e1561a9
Showing 1 changed file with 13 additions and 17 deletions.
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;
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

0 comments on commit e1561a9

Please sign in to comment.