reactjs / reactjs/react-docgen

Escapable commas in CLI options?

Đang mở
#962 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
TypeScript
Star
3.8k
Fork
316
Merge trung bình
5 giờ 7 phút
Pull request đã merge (30 ngày)
4

Mô tả

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.replaceAll could be used instead.
    • subStr.replaceAll(ESCAPED_DELIMETER, DELIMETER))
  • perform the replacement in fewer operations / general performance improvement

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với các định nghĩa --ignore và luồng phân tích cú pháp trong packages/react-docgen-cli/src/commands/parse/command.ts, sau đó đối chiếu chúng với các typing của bộ phân tích đối số của Commander tại các dòng được liên kết trong typings/index.d.ts. Xác định cách bật dấu phẩy được escape mà không thay đổi việc tách bằng dấu phẩy hiện có, đồng thời xác minh rằng --ignore chấp nhận các giá trị chứa dấu phẩy, trong khi các giá trị thông thường vẫn giữ nguyên hành vi hiện tại.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
typescript
Lĩnh vực
cli
Loại issue
Tính năng
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.