| 1 |
const DASH_CHAR = "-"; |
| 2 |
|
| 3 |
const ERROR_MESSAGE = ` |
| 4 |
1. Each individual option should be listed separately in the array of option objects e.g. "-am" should have an option for -a" and an option for "-m" |
| 5 |
|
| 6 |
2. If you would like to provide custom suggestions for common flag sequences, please list them as Suggestion objects using the additionalSuggestions prop with type: shortcut`; |
| 7 |
|
| 8 |
function getDashAmount(string) { |
| 9 |
let count = 0; |
| 10 |
if (string.length === 0) return 0; |
| 11 |
if (string[0] === DASH_CHAR) { |
| 12 |
count++; |
| 13 |
} |
| 14 |
if (string[1] === DASH_CHAR) { |
| 15 |
count++; |
| 16 |
} |
| 17 |
return count; |
| 18 |
} |
| 19 |
|
| 20 |
function checkIsInvalidName(name) { |
| 21 |
const dashAmount = getDashAmount(name); |
| 22 |
const lengthWithoutDashes = name.slice(dashAmount).length; |
| 23 |
return ( |
| 24 |
(dashAmount === 1 && lengthWithoutDashes > 1) || |
| 25 |
(dashAmount === 2 && lengthWithoutDashes === 1) |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
module.exports = { |
| 30 |
meta: { |
| 31 |
type: "problem", |
| 32 |
}, |
| 33 |
create(context) { |
| 34 |
return { |
| 35 |
Property(node) { |
| 36 |
if (node.key.name === "options") { |
| 37 |
if (node.value && node.value.type === "ArrayExpression") { |
| 38 |
node.value.elements.forEach((element) => { |
| 39 |
if (element.type !== "ObjectExpression") return; |
| 40 |
|
| 41 |
|
| 42 |
const nameProp = element.properties.find( |
| 43 |
(prop) => prop.key.name === "name" |
| 44 |
); |
| 45 |
|
| 46 |
|
| 47 |
if (nameProp.value.type === "Literal") { |
| 48 |
const isInvalid = checkIsInvalidName(nameProp.value.value); |
| 49 |
if (isInvalid) { |
| 50 |
context.report({ |
| 51 |
node: element, |
| 52 |
message: ERROR_MESSAGE, |
| 53 |
}); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if (nameProp.value.type === "ArrayExpression") { |
| 58 |
nameProp.value.elements.forEach((literal) => { |
| 59 |
const isInvalid = checkIsInvalidName(literal.value); |
| 60 |
if (isInvalid) { |
| 61 |
context.report({ |
| 62 |
node: literal, |
| 63 |
message: ERROR_MESSAGE, |
| 64 |
}); |
| 65 |
} |
| 66 |
}); |
| 67 |
} |
| 68 |
}); |
| 69 |
} |
| 70 |
} |
| 71 |
}, |
| 72 |
}; |
| 73 |
}, |
| 74 |
}; |