eslint-plugin-total-functions icon indicating copy to clipboard operation
eslint-plugin-total-functions copied to clipboard

New rule: Ban functions that throw?

Open danielnixon opened this issue 4 years ago • 9 comments

  • new URL("asdf") (see https://github.com/danielnixon/readonly-types/blob/master/src/index.ts#L75)
  • decodeURIComponent('%')
  • "".normalize("asdf")
  • others? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects

danielnixon avatar Jun 01 '20 00:06 danielnixon

> decodeURI('%')
Uncaught URIError: URI malformed
> global.decodeURIComponent("%")
Uncaught URIError: URI malformed
    at decodeURIComponent (<anonymous>)
> globalThis.decodeURI("%")
Uncaught URIError: URI malformed
    at decodeURI (<anonymous>)

danielnixon avatar Mar 09 '23 04:03 danielnixon

new URL("asdf") is covered by the new no-partial-url-constructor rule.

danielnixon avatar Mar 09 '23 04:03 danielnixon

Throws if passed a "lone surrogate" (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#utf-16_characters_unicode_codepoints_and_grapheme_clusters):

> encodeURI('\ud83d')
Uncaught URIError: URI malformed
> encodeURIComponent('\ud83d')
Uncaught URIError: URI malformed

danielnixon avatar Mar 09 '23 04:03 danielnixon

Debatable:

> parseInt('asdf')
NaN

danielnixon avatar Mar 09 '23 04:03 danielnixon

> new Intl.Locale("asdf")
Uncaught RangeError: Incorrect locale information provided

danielnixon avatar Mar 09 '23 04:03 danielnixon

JSON.stringify({a: 1n})
Uncaught TypeError: Do not know how to serialize a BigInt

danielnixon avatar Mar 09 '23 07:03 danielnixon

> JSON.parse("");
Uncaught SyntaxError: Unexpected end of JSON input

danielnixon avatar Mar 09 '23 07:03 danielnixon

> const foo = new RegExp("[asdf");
Uncaught SyntaxError: Invalid regular expression: /[asdf/: Unterminated character class

although this one is covered by https://eslint.org/docs/latest/rules/no-invalid-regexp

Although... that rule doesn't have access to type information so can't detect this:

const ATurdByAnyOtherName = RegExp;

// flagged by no-invalid-regexp
const foo = new RegExp("[asdf");

// not flagged
const bar = new ATurdByAnyOtherName("[asdf");

If we wrote our own rule we could detect that by following the prototype chain, the same as we do for URLs:

        const ATurdByAnyOtherName = URL;
        const foo = new ATurdByAnyOtherName("");

danielnixon avatar Mar 09 '23 09:03 danielnixon

> escape('\ud')
escape('\ud')

Uncaught SyntaxError: Invalid Unicode escape sequence

danielnixon avatar Mar 13 '23 04:03 danielnixon