Skip to content

Remove unnecessary elements in class intersections and subtractions #573

@RunDevelopment

Description

@RunDevelopment

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]]

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions