Fix -pipe should combine with other flags

This commit is contained in:
Vincent Tavernier 2021-04-01 00:43:11 +02:00 committed by Oliver Blanthorn
parent 1be771e6c1
commit 03e2f9f470
No known key found for this signature in database
GPG key ID: 2BB8C36BB504BFF3
2 changed files with 25 additions and 15 deletions

View file

@ -141,6 +141,15 @@ const testCases = [
warnings: [], warnings: [],
}, },
}, },
{
sources: ["-qpipe a href"],
expected: {
rapid: true,
pipeAttribute: "href",
selectors: ["a"],
warnings: [],
},
},
] ]
// Check all test cases // Check all test cases

View file

@ -81,7 +81,7 @@ export class HintConfig implements HintOptions {
} }
const result = new HintConfig() const result = new HintConfig()
const twoLetterFlags = ["fr", "wp", "br"] const multiLetterFlags = ["fr", "wp", "br", "pipe"]
// Parser state // Parser state
let state = State.Initial let state = State.Initial
@ -91,34 +91,32 @@ export class HintConfig implements HintOptions {
switch (state) { switch (state) {
case State.Initial: case State.Initial:
if (arg == "-pipe") { if (arg.length >= 2 && arg[0] === "-" && arg[1] !== "-") {
// Special case for -pipe, which is not a |1,2]-letter argument
state = State.ExpectPipeSelector
} else if (
arg.length >= 2 &&
arg[0] === "-" &&
arg[1] !== "-"
) {
// Parse short arguments, i.e. - followed by (mostly) single-letter arguments, // Parse short arguments, i.e. - followed by (mostly) single-letter arguments,
// and some two-letter arguments. // and some two-letter arguments.
for (let i = 1; i < arg.length; ++i) { for (let i = 1; i < arg.length; ++i) {
const letter = arg[i] let flag = arg[i]
let flag = letter
// Fix two-letter flags using lookahead // Fix two-letter flags using lookahead
if (i < arg.length - 1) { if (i < arg.length - 1) {
const twoLetterFlag = letter + arg[i + 1] const multiLetterFlag = multiLetterFlags.find(
tlf =>
arg.substring(i, i + tlf.length) ===
tlf,
)
if (twoLetterFlags.includes(twoLetterFlag)) { if (multiLetterFlag !== undefined) {
flag = twoLetterFlag flag = multiLetterFlag
i++ i += multiLetterFlag.length - 1
} }
} }
// Process flag // Process flag
let newOpenMode: undefined | OpenMode let newOpenMode: undefined | OpenMode
let newState: undefined | State let newState: undefined | State
// eslint-disable-next-line sonarjs/max-switch-cases
switch (flag) { switch (flag) {
case "br": case "br":
// Equivalent to -qb, but deprecated // Equivalent to -qb, but deprecated
@ -152,6 +150,9 @@ export class HintConfig implements HintOptions {
case "c": case "c":
newState = State.ExpectSelector newState = State.ExpectSelector
break break
case "pipe":
newState = State.ExpectPipeSelector
break
case "t": case "t":
newOpenMode = OpenMode.Tab newOpenMode = OpenMode.Tab
break break