-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
250 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# `regex101` | ||
|
||
Generate up-to-date [regex101](https://regex101.com/) links for your RegExp patterns in jsdoc comments. Helps you test and inspect the RegExp easily. | ||
|
||
## Triggers | ||
|
||
- `// @regex101` | ||
- `/* @regex101 */` | ||
|
||
## Examples | ||
|
||
```js | ||
/** | ||
* RegExp to match foo or bar, optionally wrapped in quotes. | ||
* | ||
* @regex101 | ||
*/ | ||
const foo = /(['"])?(foo|bar)\\1?/gi | ||
``` | ||
|
||
Will be updated to: | ||
|
||
```js | ||
/** | ||
* RegExp to match foo or bar, optionally wrapped in quotes. | ||
* | ||
* @regex101 https://regex101.com/?regex=%28%5B%27%22%5D%29%3F%28foo%7Cbar%29%5C1%3F&flags=gi&flavor=javascript | ||
*/ | ||
const foo = /(['"])?(foo|bar)\\1?/gi | ||
``` | ||
|
||
An whenever you update the RegExp pattern, the link will be updated as well. | ||
|
||
### Optional Test Strings | ||
|
||
Test string can also be provided via an optional `@example` tag: | ||
|
||
```js | ||
/** | ||
* Some jsdoc | ||
* | ||
* @example str | ||
* \`\`\`js | ||
* if ('foo'.match(foo)) { | ||
* const foo = bar | ||
* } | ||
* \`\`\` | ||
* | ||
* @regex101 | ||
*/ | ||
const foo = /(['"])?(foo|bar)\\1?/gi | ||
``` | ||
|
||
Will be updated to: | ||
|
||
```js | ||
/** | ||
* Some jsdoc | ||
* | ||
* @example str | ||
* \`\`\`js | ||
* if ('foo'.match(foo)) { | ||
* const foo = bar | ||
* } | ||
* \`\`\` | ||
* | ||
* @regex101 https://regex101.com/?regex=%28%5B%27%22%5D%29%3F%28foo%7Cbar%29%5C1%3F&flags=gi&flavor=javascript&testString=if+%28%27foo%27.match%28foo%29%29+%7B%0A++const+foo+%3D+bar%0A%7D | ||
*/ | ||
const foo = /(['"])?(foo|bar)\\1?/gi | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { regex101 as command } from './regex101' | ||
import { $, run } from './_test-utils' | ||
|
||
run( | ||
command, | ||
// basic | ||
{ | ||
code: $` | ||
// @regex101 | ||
const foo = /(?:\\b|\\s)@regex101(\\s[^\\s]+)?(?:\\s|\\b|$)/g | ||
`, | ||
output: output => expect(output).toMatchInlineSnapshot(` | ||
"// @regex101 https://regex101.com/?regex=%28%3F%3A%5Cb%7C%5Cs%29%40regex101%28%5Cs%5B%5E%5Cs%5D%2B%29%3F%28%3F%3A%5Cs%7C%5Cb%7C%24%29&flags=g&flavor=javascript | ||
const foo = /(?:\\b|\\s)@regex101(\\s[^\\s]+)?(?:\\s|\\b|$)/g" | ||
`), | ||
errors: ['command-fix'], | ||
}, | ||
// block comment | ||
{ | ||
code: $` | ||
/** | ||
* Some jsdoc | ||
* | ||
* @regex101 | ||
* @deprecated | ||
*/ | ||
const foo = /(['"])?(foo|bar)\\1?/gi | ||
`, | ||
output: output => expect(output).toMatchInlineSnapshot(` | ||
"/** | ||
* Some jsdoc | ||
* | ||
* @regex101 https://regex101.com/?regex=%28%5B%27%22%5D%29%3F%28foo%7Cbar%29%5C1%3F&flags=gi&flavor=javascript | ||
* @deprecated | ||
*/ | ||
const foo = /(['"])?(foo|bar)\\1?/gi" | ||
`), | ||
errors: ['command-fix'], | ||
}, | ||
// example block | ||
{ | ||
code: $` | ||
/** | ||
* Some jsdoc | ||
* | ||
* @example str | ||
* \`\`\`js | ||
* if ('foo'.match(foo)) { | ||
* const foo = bar | ||
* } | ||
* \`\`\` | ||
* | ||
* @regex101 | ||
*/ | ||
const foo = /(['"])?(foo|bar)\\1?/gi | ||
`, | ||
output: output => expect(output).toMatchInlineSnapshot(` | ||
"/** | ||
* Some jsdoc | ||
* | ||
* @example str | ||
* \`\`\`js | ||
* if ('foo'.match(foo)) { | ||
* const foo = bar | ||
* } | ||
* \`\`\` | ||
* | ||
* @regex101 https://regex101.com/?regex=%28%5B%27%22%5D%29%3F%28foo%7Cbar%29%5C1%3F&flags=gi&flavor=javascript&testString=if+%28%27foo%27.match%28foo%29%29+%7B%0A++const+foo+%3D+bar%0A%7D | ||
*/ | ||
const foo = /(['"])?(foo|bar)\\1?/gi" | ||
`), | ||
errors: ['command-fix'], | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { parseComment } from '@es-joy/jsdoccomment' | ||
import type { Command, Tree } from '../types' | ||
|
||
// @regex101 https://regex101.com/?regex=%60%60%60%28.*%29%5Cn%28%5B%5Cs%5CS%5D*%29%5Cn%60%60%60&flavor=javascript | ||
const reCodeBlock = /```(.*)\n([\s\S]*)\n```/ | ||
|
||
export const regex101: Command = { | ||
name: 'regex101', | ||
/** | ||
* @regex101 https://regex101.com/?regex=%28%5Cb%7C%5Cs%7C%5E%29%28%40regex101%29%28%5Cs%5B%5E%5Cs%5D%2B%29%3F%28%5Cb%7C%5Cs%7C%24%29&flavor=javascript | ||
*/ | ||
match: /(\b|\s|^)(@regex101)(\s[^\s]+)?(\b|\s|$)/, | ||
commentType: 'both', | ||
action(ctx) { | ||
const literal = ctx.findNodeBelow((n) => { | ||
return n.type === 'Literal' && 'regex' in n | ||
}) as Tree.RegExpLiteral | undefined | ||
if (!literal) | ||
return ctx.reportError('Unable to find arrow function to convert') | ||
|
||
const [ | ||
_fullStr = '', | ||
spaceBefore = '', | ||
commandStr = '', | ||
existingUrl = '', | ||
_spaceAfter = '', | ||
] = ctx.matches as string[] | ||
|
||
let example: string | undefined | ||
|
||
if (ctx.comment.value.includes('```') && ctx.comment.value.includes('@example')) { | ||
try { | ||
const parsed = parseComment(ctx.comment, '') | ||
const tag = parsed.tags.find(t => t.tag === 'example') | ||
const description = tag?.description | ||
const code = description?.match(reCodeBlock)?.[2].trim() | ||
if (code) | ||
example = code | ||
} | ||
catch (e) {} | ||
} | ||
|
||
// docs: https://github.com/firasdib/Regex101/wiki/FAQ#how-to-prefill-the-fields-on-the-interface-via-url | ||
const query = new URLSearchParams() | ||
query.set('regex', literal.regex.pattern) | ||
if (literal.regex.flags) | ||
query.set('flags', literal.regex.flags) | ||
query.set('flavor', 'javascript') | ||
if (example) | ||
query.set('testString', example) | ||
const url = `https://regex101.com/?${query}` | ||
|
||
if (existingUrl.trim() === url.trim()) | ||
return | ||
|
||
const indexStart = ctx.comment.range[0] + ctx.matches.index! + spaceBefore.length + 2 /** comment prefix */ | ||
const indexEnd = indexStart + commandStr.length + existingUrl.length | ||
|
||
ctx.report({ | ||
loc: { | ||
start: ctx.source.getLocFromIndex(indexStart), | ||
end: ctx.source.getLocFromIndex(indexEnd), | ||
}, | ||
removeComment: false, | ||
message: `Update the regex101 link`, | ||
fix(fixer) { | ||
return fixer.replaceTextRange([indexStart, indexEnd], `@regex101 ${url}`) | ||
}, | ||
}) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters