1 | # @eslint-community/regexpp
|
---|
2 |
|
---|
3 | [![npm version](https://img.shields.io/npm/v/@eslint-community/regexpp.svg)](https://www.npmjs.com/package/@eslint-community/regexpp)
|
---|
4 | [![Downloads/month](https://img.shields.io/npm/dm/@eslint-community/regexpp.svg)](http://www.npmtrends.com/@eslint-community/regexpp)
|
---|
5 | [![Build Status](https://github.com/eslint-community/regexpp/workflows/CI/badge.svg)](https://github.com/eslint-community/regexpp/actions)
|
---|
6 | [![codecov](https://codecov.io/gh/eslint-community/regexpp/branch/main/graph/badge.svg)](https://codecov.io/gh/eslint-community/regexpp)
|
---|
7 |
|
---|
8 | A regular expression parser for ECMAScript.
|
---|
9 |
|
---|
10 | ## 💿 Installation
|
---|
11 |
|
---|
12 | ```bash
|
---|
13 | $ npm install @eslint-community/regexpp
|
---|
14 | ```
|
---|
15 |
|
---|
16 | - require Node@^12.0.0 || ^14.0.0 || >=16.0.0.
|
---|
17 |
|
---|
18 | ## 📖 Usage
|
---|
19 |
|
---|
20 | ```ts
|
---|
21 | import {
|
---|
22 | AST,
|
---|
23 | RegExpParser,
|
---|
24 | RegExpValidator,
|
---|
25 | RegExpVisitor,
|
---|
26 | parseRegExpLiteral,
|
---|
27 | validateRegExpLiteral,
|
---|
28 | visitRegExpAST
|
---|
29 | } from "@eslint-community/regexpp"
|
---|
30 | ```
|
---|
31 |
|
---|
32 | ### parseRegExpLiteral(source, options?)
|
---|
33 |
|
---|
34 | Parse a given regular expression literal then make AST object.
|
---|
35 |
|
---|
36 | This is equivalent to `new RegExpParser(options).parseLiteral(source)`.
|
---|
37 |
|
---|
38 | - **Parameters:**
|
---|
39 | - `source` (`string | RegExp`) The source code to parse.
|
---|
40 | - `options?` ([`RegExpParser.Options`]) The options to parse.
|
---|
41 | - **Return:**
|
---|
42 | - The AST of the regular expression.
|
---|
43 |
|
---|
44 | ### validateRegExpLiteral(source, options?)
|
---|
45 |
|
---|
46 | Validate a given regular expression literal.
|
---|
47 |
|
---|
48 | This is equivalent to `new RegExpValidator(options).validateLiteral(source)`.
|
---|
49 |
|
---|
50 | - **Parameters:**
|
---|
51 | - `source` (`string`) The source code to validate.
|
---|
52 | - `options?` ([`RegExpValidator.Options`]) The options to validate.
|
---|
53 |
|
---|
54 | ### visitRegExpAST(ast, handlers)
|
---|
55 |
|
---|
56 | Visit each node of a given AST.
|
---|
57 |
|
---|
58 | This is equivalent to `new RegExpVisitor(handlers).visit(ast)`.
|
---|
59 |
|
---|
60 | - **Parameters:**
|
---|
61 | - `ast` ([`AST.Node`]) The AST to visit.
|
---|
62 | - `handlers` ([`RegExpVisitor.Handlers`]) The callbacks.
|
---|
63 |
|
---|
64 | ### RegExpParser
|
---|
65 |
|
---|
66 | #### new RegExpParser(options?)
|
---|
67 |
|
---|
68 | - **Parameters:**
|
---|
69 | - `options?` ([`RegExpParser.Options`]) The options to parse.
|
---|
70 |
|
---|
71 | #### parser.parseLiteral(source, start?, end?)
|
---|
72 |
|
---|
73 | Parse a regular expression literal.
|
---|
74 |
|
---|
75 | - **Parameters:**
|
---|
76 | - `source` (`string`) The source code to parse. E.g. `"/abc/g"`.
|
---|
77 | - `start?` (`number`) The start index in the source code. Default is `0`.
|
---|
78 | - `end?` (`number`) The end index in the source code. Default is `source.length`.
|
---|
79 | - **Return:**
|
---|
80 | - The AST of the regular expression.
|
---|
81 |
|
---|
82 | #### parser.parsePattern(source, start?, end?, flags?)
|
---|
83 |
|
---|
84 | Parse a regular expression pattern.
|
---|
85 |
|
---|
86 | - **Parameters:**
|
---|
87 | - `source` (`string`) The source code to parse. E.g. `"abc"`.
|
---|
88 | - `start?` (`number`) The start index in the source code. Default is `0`.
|
---|
89 | - `end?` (`number`) The end index in the source code. Default is `source.length`.
|
---|
90 | - `flags?` (`{ unicode?: boolean, unicodeSets?: boolean }`) The flags to enable Unicode mode, and Unicode Set mode.
|
---|
91 | - **Return:**
|
---|
92 | - The AST of the regular expression pattern.
|
---|
93 |
|
---|
94 | #### parser.parseFlags(source, start?, end?)
|
---|
95 |
|
---|
96 | Parse a regular expression flags.
|
---|
97 |
|
---|
98 | - **Parameters:**
|
---|
99 | - `source` (`string`) The source code to parse. E.g. `"gim"`.
|
---|
100 | - `start?` (`number`) The start index in the source code. Default is `0`.
|
---|
101 | - `end?` (`number`) The end index in the source code. Default is `source.length`.
|
---|
102 | - **Return:**
|
---|
103 | - The AST of the regular expression flags.
|
---|
104 |
|
---|
105 | ### RegExpValidator
|
---|
106 |
|
---|
107 | #### new RegExpValidator(options)
|
---|
108 |
|
---|
109 | - **Parameters:**
|
---|
110 | - `options` ([`RegExpValidator.Options`]) The options to validate.
|
---|
111 |
|
---|
112 | #### validator.validateLiteral(source, start, end)
|
---|
113 |
|
---|
114 | Validate a regular expression literal.
|
---|
115 |
|
---|
116 | - **Parameters:**
|
---|
117 | - `source` (`string`) The source code to validate.
|
---|
118 | - `start?` (`number`) The start index in the source code. Default is `0`.
|
---|
119 | - `end?` (`number`) The end index in the source code. Default is `source.length`.
|
---|
120 |
|
---|
121 | #### validator.validatePattern(source, start, end, flags)
|
---|
122 |
|
---|
123 | Validate a regular expression pattern.
|
---|
124 |
|
---|
125 | - **Parameters:**
|
---|
126 | - `source` (`string`) The source code to validate.
|
---|
127 | - `start?` (`number`) The start index in the source code. Default is `0`.
|
---|
128 | - `end?` (`number`) The end index in the source code. Default is `source.length`.
|
---|
129 | - `flags?` (`{ unicode?: boolean, unicodeSets?: boolean }`) The flags to enable Unicode mode, and Unicode Set mode.
|
---|
130 |
|
---|
131 | #### validator.validateFlags(source, start, end)
|
---|
132 |
|
---|
133 | Validate a regular expression flags.
|
---|
134 |
|
---|
135 | - **Parameters:**
|
---|
136 | - `source` (`string`) The source code to validate.
|
---|
137 | - `start?` (`number`) The start index in the source code. Default is `0`.
|
---|
138 | - `end?` (`number`) The end index in the source code. Default is `source.length`.
|
---|
139 |
|
---|
140 | ### RegExpVisitor
|
---|
141 |
|
---|
142 | #### new RegExpVisitor(handlers)
|
---|
143 |
|
---|
144 | - **Parameters:**
|
---|
145 | - `handlers` ([`RegExpVisitor.Handlers`]) The callbacks.
|
---|
146 |
|
---|
147 | #### visitor.visit(ast)
|
---|
148 |
|
---|
149 | Validate a regular expression literal.
|
---|
150 |
|
---|
151 | - **Parameters:**
|
---|
152 | - `ast` ([`AST.Node`]) The AST to visit.
|
---|
153 |
|
---|
154 | ## 📰 Changelog
|
---|
155 |
|
---|
156 | - [GitHub Releases](https://github.com/eslint-community/regexpp/releases)
|
---|
157 |
|
---|
158 | ## 🍻 Contributing
|
---|
159 |
|
---|
160 | Welcome contributing!
|
---|
161 |
|
---|
162 | Please use GitHub's Issues/PRs.
|
---|
163 |
|
---|
164 | ### Development Tools
|
---|
165 |
|
---|
166 | - `npm test` runs tests and measures coverage.
|
---|
167 | - `npm run build` compiles TypeScript source code to `index.js`, `index.js.map`, and `index.d.ts`.
|
---|
168 | - `npm run clean` removes the temporary files which are created by `npm test` and `npm run build`.
|
---|
169 | - `npm run lint` runs ESLint.
|
---|
170 | - `npm run update:test` updates test fixtures.
|
---|
171 | - `npm run update:ids` updates `src/unicode/ids.ts`.
|
---|
172 | - `npm run watch` runs tests with `--watch` option.
|
---|
173 |
|
---|
174 | [`AST.Node`]: src/ast.ts#L4
|
---|
175 | [`RegExpParser.Options`]: src/parser.ts#L743
|
---|
176 | [`RegExpValidator.Options`]: src/validator.ts#L220
|
---|
177 | [`RegExpVisitor.Handlers`]: src/visitor.ts#L291
|
---|