reactjs / reactjs/react-docgen
Escapable commas in CLI options?
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- TypeScript
- Sterne
- 3.8k
- Forks
- 316
- Ø Merge
- 5 Std. 7 Min.
- Gemergte PRs (30 T.)
- 4
Beschreibung
The commander argument parser for the --ignore option:
https://github.com/reactjs/react-docgen/blob/f5d644a3041c227c4889059e787fcbaab57ec70e/packages/react-docgen-cli/src/commands/parse/command.ts#L26-L39
https://github.com/reactjs/react-docgen/blob/f5d644a3041c227c4889059e787fcbaab57ec70e/packages/react-docgen-cli/src/commands/parse/command.ts#L62-L67
... splits on a comma, which makes it impossible to use glob brace expansion for this option or commas in general for any option that also uses this argument parser.
Would supporting escapable commas be possible?
To avoid breaking changes you may need to make it opt-in (behind a new option --comma-escaping or similar.)
The problem seems common enough and has been solved different ways in different languages. Here is a rough first pass after a little (very little) research:
/**
* Split on commas (","), unless they are escaped with a backslash("\,").
*
* Escaped commas are replaced with individual commas.
*/
export function splitOnComma(str: string): string[] {
if (typeof str !== 'string') {
const errorMessage = 'input must be a string'
throw new TypeError(errorMessage)
}
const DELIMETER = ','
const ESCAPE_CHAR = '\\'
const ESCAPED_DELIMETER = ESCAPE_CHAR + DELIMETER
const hasEscapedDelimeter = str.includes(ESCAPED_DELIMETER)
// Negative Lookbehind: https://www.regular-expressions.info/lookaround.html
const result = str.split(/,(?<!\\,)/)
if (hasEscapedDelimeter) {
return result.map((subStr) => subStr.replace(/\\,/gi, DELIMETER))
}
return result
}
I don't have experience with negative lookbehinds. I wrote the example to favor being excessively self-documenting. Refinements could include:
- improved error message
- reducing the number of lines needed
- adding unit tests
- if the build tools are configured for ES2021+,
String.prototype.replaceAllcould be used instead.subStr.replaceAll(ESCAPED_DELIMETER, DELIMETER))
- perform the replacement in fewer operations / general performance improvement
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie mit den --ignore-Definitionen und dem Parsing-Ablauf in packages/react-docgen-cli/src/commands/parse/command.ts, und vergleichen Sie sie anschließend mit den Typings des Commander-Argument-Parsers in den verlinkten Zeilen von typings/index.d.ts. Ermitteln Sie, wie escaped commas aktiviert werden sollen, ohne die bestehende Kommatrennung zu ändern, und überprüfen Sie, dass --ignore Werte mit Kommas akzeptiert, während gewöhnliche Werte ihr aktuelles Verhalten beibehalten.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- cli
- Issue-Typ
- Feature
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100