Skip to content

Latest commit

 

History

History
132 lines (95 loc) · 2.67 KB

no-obscure-range.md

File metadata and controls

132 lines (95 loc) · 2.67 KB
pageClass sidebarDepth title description since
rule-details
0
regexp/no-obscure-range
disallow obscure character ranges
v0.9.0

regexp/no-obscure-range

💼 This rule is enabled in the ✅ plugin:regexp/recommended config.

disallow obscure character ranges

📖 Rule Details

The character range operator (the - inside character classes) can easily be misused (mostly unintentionally) to construct non-obvious character class. This rule will disallow all non-obvious uses of the character range operator.

/* eslint regexp/no-obscure-range: "error" */

/* ✓ GOOD */
var foo = /[a-z]/;
var foo = /[J-O]/;
var foo = /[1-9]/;
var foo = /[\x00-\x40]/;
var foo = /[\0-\uFFFF]/;
var foo = /[\0-\u{10FFFF}]/u;
var foo = /[\1-\5]/;
var foo = /[\cA-\cZ]/;

/* ✗ BAD */
var foo = /[A-\x43]/;
var foo = /[\41-\x45]/;
var foo = /[!-$]/;
var foo = /[😀-😄]/u;

🔧 Options

{
  "regexp/no-obscure-range": ["error",
    {
      "allowed": "alphanumeric" // or "all" or [...]
    }
  ]
}

This option can be used to override the allowedCharacterRanges setting.

It allows all values that the allowedCharacterRanges setting allows.

"allowed": "alphanumeric"

/* eslint regexp/no-obscure-range: ["error", { "allowed": "alphanumeric" }] */

/* ✓ GOOD */
var foo = /[a-z]/;
var foo = /[J-O]/;
var foo = /[1-9]/;

/* ✗ BAD */
var foo = /[A-\x43]/;
var foo = /[\41-\x45]/;
var foo = /[!-$]/;
var foo = /[😀-😄]/u;

"allowed": "all"

/* eslint regexp/no-obscure-range: ["error", { "allowed": "all" }] */

/* ✓ GOOD */
var foo = /[a-z]/;
var foo = /[J-O]/;
var foo = /[1-9]/;
var foo = /[!-$]/;
var foo = /[😀-😄]/u;

/* ✗ BAD */
var foo = /[A-\x43]/;
var foo = /[\41-\x45]/;

"allowed": [ "alphanumeric", "😀-😏" ]

/* eslint regexp/no-obscure-range: ["error", { "allowed": [ "alphanumeric", "😀-😏" ] }] */

/* ✓ GOOD */
var foo = /[a-z]/;
var foo = /[J-O]/;
var foo = /[1-9]/;
var foo = /[😀-😄]/u;

/* ✗ BAD */
var foo = /[A-\x43]/;
var foo = /[\41-\x45]/;
var foo = /[!-$]/;

🚀 Version

This rule was introduced in eslint-plugin-regexp v0.9.0

🔍 Implementation