Skip to content

Enhancement: [restrict-template-expressions] allowURLSearchParams option #5323

Closed as not planned
@mmkal

Description

@mmkal

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

https://github.com/typescript-eslint/typescript-eslint/blob/v4.28.2/packages/eslint-plugin/docs/rules/restrict-template-expressions.md

Description

Having to do this is a little annoying:

const href = `https://example.com/foo/bar/baz?${new URLSearchParams({ abc: 'xyz' }).toString()}`

When the version of the above without .toString() is always safe and less verbose. URLSearchParams is supported in pretty much every browser, nodejs, and deno.

I read the rationale for closing the broader issue #3538: #3538 (comment) and I don't think it applies here. This is specific to URLSearchParams not any-class-which-implements-toString which is less safe and harder to implement.

Fail

const href = 'https://example.com/foo/bar/baz?${{ abc: 'xyz' }}`

Pass

const href = 'https://example.com/foo/bar/baz?${new URLSearchParams({ abc: 'xyz' })}`

Additional Info

The option should work just like allowNumber, allowBoolean, etc.

I'd be happy to contribute a PR if it'd be accepted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already existsenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions