eslint-plugin-total-functions
eslint-plugin-total-functions copied to clipboard
New rule: Ban functions that throw?
-
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
> decodeURI('%')
Uncaught URIError: URI malformed
> global.decodeURIComponent("%")
Uncaught URIError: URI malformed
at decodeURIComponent (<anonymous>)
> globalThis.decodeURI("%")
Uncaught URIError: URI malformed
at decodeURI (<anonymous>)
new URL("asdf")
is covered by the new no-partial-url-constructor
rule.
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
Debatable:
> parseInt('asdf')
NaN
> new Intl.Locale("asdf")
Uncaught RangeError: Incorrect locale information provided
JSON.stringify({a: 1n})
Uncaught TypeError: Do not know how to serialize a BigInt
> JSON.parse("");
Uncaught SyntaxError: Unexpected end of JSON input
> 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("");
> escape('\ud')
escape('\ud')
Uncaught SyntaxError: Invalid Unicode escape sequence