Skip to content

chore(web): allow a locale-less Intl formatter only to read its resolvedOptions, with or without new - #1555

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:chore/locale-lint-intl-read
Oct 26, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:chore/locale-lint-intl-read

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Follow-up from the review of #1549. The no-restricted-syntax rule's zero-argument Intl case was off both ways:

  • Flagged when it shouldn't be: new Intl.DateTimeFormat().resolvedOptions().timeZone, a common way to read the browser's time zone, though it formats nothing.
  • Not flagged when it should be: Intl.DateTimeFormat().format(date), which formats in the browser's locale.

The maintainer's selector replaces the two old ones in web/eslint.config.mjs. It allows a zero-argument Intl formatter only when its resolvedOptions() is read, with or without new:

":matches(NewExpression, CallExpression)[callee.object.name='Intl'][arguments.length=0]:not(MemberExpression[property.name='resolvedOptions'] > .object)"

Checked on a probe file, since deleted:

  • Flagged: Intl.DateTimeFormat().format(…), new Intl.DateTimeFormat().format(…), new Intl.NumberFormat(), Intl.NumberFormat() and new Intl.Collator().
  • Allowed: Intl.DateTimeFormat().resolvedOptions().timeZone (lib/time-zone.ts), new Intl.DateTimeFormat().resolvedOptions().timeZone and new Intl.NumberFormat('en-US').

Related issue

Follow-up to #1549 (no issue).

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

cd web && printf "export const a = Intl.DateTimeFormat().format(new Date());\nexport const b = new Intl.DateTimeFormat().resolvedOptions().timeZone;\n" > src/lib/zz.ts && npx eslint src/lib/zz.ts; rm src/lib/zz.ts

Line 1 is flagged and line 2 isn't.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/): 0 errors; the 6 warnings were already on main
  • yarn typecheck passes (run from web/): lint config only
  • yarn format:check passes (run from web/): lint config only, outside its globs
  • Changes are focused — one concern per PR

A review agent checked the selector against yours, and that :not(Parent > .object) looks at the direct parent only: foo(Intl.DateTimeFormat()).resolvedOptions() is still flagged.

🤖 Generated with Claude Code

…vedOptions, with or without new (ansvisor#1549 review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

The #1549 review's item, the rule's zero-argument Intl case: Done. The one selector replaces the two, as suggested.

What I checked:

  • The rule. I read the six selectors from this PR's eslint.config.mjs and ran them with ESLint 9.39.4 and @typescript-eslint/parser 8.56.1 (Linter) over a probe of 41 cases. All 41 come out as intended; main's seven selectors get 33.
    • The two cases from the review:
      • Intl.DateTimeFormat().format(date) is now flagged;
      • new Intl.DateTimeFormat().resolvedOptions().timeZone is now allowed.
    • Also flagged now: a zero-argument Intl.DateTimeFormat() without new, when its .formatToParts() is called, or it's stored in a variable or passed to a function.
    • Allowed now: any resolvedOptions() read with new, such as new Intl.DateTimeFormat().resolvedOptions().locale and new Intl.NumberFormat().resolvedOptions().
    • Still allowed: a resolvedOptions() read with ?. (on the formatter or on the call), or destructured.
    • Unchanged:
      • every other zero-argument Intl formatter (NumberFormat, Collator, PluralRules, RelativeTimeFormat, ListFormat, Segmenter, DisplayNames) is still flagged, with or without new;
      • the undefined and [] selectors;
      • the toLocale*String selectors;
      • named locales, localeCompare, Intl.supportedValuesOf('timeZone') and supportedLocalesOf aren't flagged.
    • The JSX form {new Intl.DateTimeFormat().format(d)} is flagged.
  • The full config. With the real config file (main repo's packages, no cache), a probe with the PR's four lines flags the two formatting lines and not the two resolvedOptions() reads. The same probe as a .test.ts is ignored, and lib/time-zone.ts and lib/format.ts are clean.
  • No false hits.
    • The merged tree's web/src has 738 non-test files, with 0 hits and 0 parse errors. The only matches are the two in lib/format.test.ts, which the config skips on purpose.

    • Only three forms are newly flagged without formatting:

      • (Intl.DateTimeFormat() as X).resolvedOptions();
      • Intl.DateTimeFormat()!.resolvedOptions();
      • Intl.DateTimeFormat()['resolvedOptions']().

      None is in the code, and the plain form reads the same.

  • Merge.
    • One commit on main (307670b), and git merge-tree with main gives the PR's own tree.
    • It merges cleanly with every open PR. The web trees of #1553, #1554, #1556 and #1557 give 0 hits with this rule, so merging it first doesn't break their lint.
  • Before the API deploy. Lint config only, so nothing changes for users.

@gkhngyk
gkhngyk merged commit 024e3c7 into ansvisor:main Oct 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants