Skip to content

Commit

Permalink
fix: RegExp pattern generated result is wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Dec 8, 2022
1 parent 0ddd675 commit c7d09e4
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/IndexGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const indexFilePaths = [
join(__dirname, './__fixtures__/cross_negative/index.txt'),
join(__dirname, './__fixtures__/re/index.js'),
join(__dirname, './__fixtures__/re_pattern/index'),
join(__dirname, './__fixtures__/bug1/index'),
]

test('ok', async () => {
Expand Down
22 changes: 12 additions & 10 deletions src/IndexGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ export class IndexGenerator {
const strPatterns = marker.patterns.filter(
(p): p is string => typeof p === 'string',
)
const rePatterns = marker.patterns.filter(
(p): p is RegExp => p instanceof RegExp,
)
const rePatterns = marker.patterns
.filter((p): p is RegExp => p instanceof RegExp)
.map(re => ({
re: new RegExp(re, re.flags.replace(/g/g, '')),
isNegative: re.flags.includes('g'),
}))
let paths = await globby(strPatterns, {
dot: true,
onlyFiles: false,
Expand All @@ -48,14 +51,13 @@ export class IndexGenerator {
if (rePatterns.length) {
paths = paths.filter(path => {
const pp = parse(path)
const relativePath = IndexGenerator.getRelativePath(
fileDir,
join(pp.dir, pp.name),
)
const relativePath =
IndexGenerator.getRelativePath(fileDir, join(pp.dir, pp.name)) +
pp.ext
return rePatterns.every(re =>
re.flags.includes('g')
? !re.test(relativePath)
: re.test(relativePath),
re.isNegative
? !re.re.test(relativePath)
: re.re.test(relativePath),
)
})
}
Expand Down
Empty file added src/__fixtures__/bug1/a.vue
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions src/__fixtures__/bug1/index
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @index(['./**/*.vue', /\/(components|blocks|_(?!layout))/g], f => `${f.name}`)

// @endindex
19 changes: 19 additions & 0 deletions src/__snapshots__/IndexGenerator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export * from './j'
"
`;

exports[`ok: generated content of /bug1/index 1`] = `
"// @index(['./**/*.vue', /\\\\/(components|blocks|_(?!layout))/g], f => \`\${f.name}\`)
a
// @endindex
"
`;

exports[`ok: generated content of /cross/index.txt 1`] = `
"# @index('../**/*', f => \`\${f.path}\${f.ext}\`, { onlyFiles: true })
../all_ts_tsx/a.ts
Expand All @@ -20,6 +27,13 @@ exports[`ok: generated content of /cross/index.txt 1`] = `
../all_ts_tsx/e/hello.ts
../all_ts_tsx/index.ts
../all_ts_tsx/j/index.tsx
../bug1/a.vue
../bug1/components/表单.vue
../bug1/components/表单内容.vue
../bug1/components/表单按钮.vue
../bug1/components/表单操作.vue
../bug1/components/表单项.vue
../bug1/index
../cross_negative/index.txt
../invalid/empty
../invalid/error
Expand Down Expand Up @@ -62,6 +76,8 @@ exports[`ok: generated content of /cross/index.txt 1`] = `
../all_ts_tsx/d
../all_ts_tsx/e
../all_ts_tsx/j
../bug1
../bug1/components
./
../cross_negative
../invalid
Expand All @@ -86,6 +102,7 @@ exports[`ok: generated content of /cross/index.txt 1`] = `
exports[`ok: generated content of /cross_negative/index.txt 1`] = `
"# @index(['../*', '!../scss'], f => \`\${f.path}\${f.ext}\`)
../all_ts_tsx
../bug1
../cross
./
../invalid
Expand Down Expand Up @@ -242,6 +259,8 @@ exports[`ok: generated content of /types/index.ts 1`] = `
exports[`ok: messages of /all_ts_tsx/index.ts 1`] = `Array []`;
exports[`ok: messages of /bug1/index 1`] = `Array []`;
exports[`ok: messages of /cross/index.txt 1`] = `Array []`;
exports[`ok: messages of /cross_negative/index.txt 1`] = `Array []`;
Expand Down

0 comments on commit c7d09e4

Please sign in to comment.