Remove unnecessary elements in class intersections and subtractions
RunDevelopment opened this issue · comments
Michael Schmidt commented
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]]
Yosuke Ota commented
Thank you for the rule suggestions! I like the new rule.