testRedundantParensDontChangeTheHash

tcmts.core.test.hash.testRedundantParensDontChangeTheHash · #ax5bq5wc · term

Parentheses a formatter adds around an optional chain don't change a hash; ones that change what the chain does (it goes on after them) do.

import { hashes } from "#codebase/tcmts/core/test/hash";
import assert from "node:assert/strict";

export const testRedundantParensDontChangeTheHash = () => {
  const bare = hashes(
    `export const first = (s?: string) => s?.trim()[0]?.toUpperCase() ?? "";`,
  ).first;
  assert.equal(
    hashes(`export const first = (s?: string) => (s?.trim()[0]?.toUpperCase()) ?? "";`).first,
    bare,
  );
  assert.equal(
    hashes(`export const first = (s?: string) => ((s?.trim()[0]?.toUpperCase())) ?? "";`).first,
    bare,
  );
  // `(o?.s).length` throws when `o` is undefined; `o?.s.length` doesn't
  assert.notEqual(
    hashes(`export const len = (o?: { s: string }) => (o?.s)!.length;`).len,
    hashes(`export const len = (o?: { s: string }) => o?.s!.length;`).len,
  );
}

uses