ota-meshi / eslint-plugin-regexp

ESLint plugin for finding regex mistakes and style guide violations.

Home Page:https://ota-meshi.github.io/eslint-plugin-regexp/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Remove unnecessary elements in class intersections and subtractions

RunDevelopment opened this issue · comments

Motivation
The v flags adds class intersection and subtraction as new features. They are simple set operations but they also make it possible to have completely useless elements.

Description
Report useless elements in class intersections and subtractions.

Examples

/* ✗ BAD */
var foo = /[\w&&\d]/v // => /[\d]/v
// \d is a subset of \w, so remove the intersection.
var foo = /[\w&&\s]/v // => /[]/v
// \w and \s are disjoint, so the intersection will be empty.
var foo = /[\w&&[\d\s]]/v // => /[\w&&[\d]]/v
// \s in [\d\s] and \w are disjoint, so \s can be removed.
// Proof: [\w&&[\d\s]] == [[\w&&[\d]][\w&&\s]] == [[\w&&[\d]][]] == [\w&&[\d]]
var foo = /[\w&&[^\d\s]]/v // => /[\w&&[^\d]]/v
// \s in [^\d\s] and \w are disjoint, so \s can be removed.
// Proof: [\w&&[^\d\s]] == [\w&&[[^\d]&&[^\s]]] == [\w&&[[^\s]&&[^\d]]] == [[\w&&[^\s]]&&[^\d]] == [[\w]&&[^\d]]

var foo = /[\w--\s]/v // => /[\w]/v
// \w and \s are disjoint, so the subtraction is useless.
var foo = /[\d--\w]/v // => /[]/v
// \d is a subset of \w, so the subtraction will be empty.
var foo = /[\w--[\d\s]]/v // => /[\w--[\d]]/v
// \s in [\d\s] and \w are disjoint, so \s can be removed.
// Proof: [\w--[\d\s]] == [[\w--\s]--[\d]] == [\w--[\d]]
var foo = /[\w--[^\d\s]]/v // => /[\w--[^\d]]/v
// \s in [^\d\s] and \w are disjoint, so \s can be removed.
// Proof: [\w--[^\d\s]] == [\w--[[^\d]&&[^\s]] == [[\w--[^\d]][\w--[^\s]]] == [[\w--[^\d]][]] == [\w--[^\d]]

Thank you for the rule suggestions! I like the new rule.