Skip to content

Commit

Permalink
fix(path): fix path match destructor (#2148)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Sep 13, 2021
1 parent c8176e5 commit f621d98
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
22 changes: 22 additions & 0 deletions packages/path/src/__tests__/match.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,27 @@ test('test segments', () => {
expect(node.match(['a', 0, 'b'])).toEqual(true)
})

test('group match with destructor', () => {
expect(Path.parse('*([startDate,endDate],date,weak)').match('date')).toEqual(
true
)
expect(Path.parse('*({startDate,endDate},date,weak)').match('date')).toEqual(
true
)
expect(Path.parse('*([startDate,endDate],date,weak)').match('xxx')).toEqual(
false
)
expect(Path.parse('*({startDate,endDate},date,weak)').match('xxx')).toEqual(
false
)
expect(
Path.parse('*([startDate,endDate],date,weak)').match('[startDate,endDate]')
).toEqual(true)
expect(
Path.parse('*({startDate,endDate},date,weak)').match('{startDate,endDate}')
).toEqual(true)
})

match({
'*': [[], ['aa'], ['aa', 'bb', 'cc'], ['aa', 'dd', 'gg']],
'*.a.b': [
Expand Down Expand Up @@ -209,6 +230,7 @@ match({
'*(!aa,bb,bb.aa)': [['xx'], ['yyy']],
'*(!aaa)': [['bbb']],
'*(!aaa,bbb)': [['ccc'], ['ggg']],
'*([startDate,endDate],date,weak)': [['date']],
})

unmatch({
Expand Down
13 changes: 8 additions & 5 deletions packages/path/src/__tests__/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ test('relative', () => {
},
source: '2',
after: {
type: 'Identifier',
value: 'dd',
type: 'DotOperator',
after: {
type: 'DotOperator',
type: 'Identifier',
value: 'dd',
after: {
type: 'Identifier',
value: 'bb',
type: 'DotOperator',
after: {
type: 'Identifier',
value: 'bb',
},
},
},
},
Expand Down
14 changes: 9 additions & 5 deletions packages/path/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ export class Parser extends Tokenizer {
: this.parseArrayPattern()
const endPos = this.state.pos
this.state.context.pop()
this.next()
node.source = this.input
.substring(startPos, endPos)
.replace(
Expand Down Expand Up @@ -269,6 +268,7 @@ export class Parser extends Tokenizer {
}
this.relative = undefined
this.pushSegments(node.source)
this.next()
this.append(node, this.parseAtom(this.state.type))
return node
}
Expand All @@ -288,8 +288,10 @@ export class Parser extends Tokenizer {
while (this.state.type !== bracketRTok && this.state.type !== eofTok) {
nodes.push(this.parseAtom(this.state.type))
if (this.state.type === bracketRTok) {
this.next()
break
if (this.includesContext(destructorContext)) {
this.next()
}
return nodes
}
this.next()
}
Expand Down Expand Up @@ -322,8 +324,10 @@ export class Parser extends Tokenizer {
| ArrayPatternNode[]
}
if (this.state.type === braceRTok) {
this.next()
break
if (this.includesContext(destructorContext)) {
this.next()
}
return nodes
}
this.next()
}
Expand Down
5 changes: 3 additions & 2 deletions packages/path/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const braceLTok = TokenType('{', {
if (this.includesContext(destructorContext)) {
return prev === colonTok || prev === commaTok || prev === bracketLTok
}
return prev === dotTok || prev === colonTok
return prev === dotTok || prev === colonTok || prev === parenLTok
},
updateContext() {
this.state.context.push(braceContext)
Expand All @@ -124,7 +124,7 @@ export const braceRTok = TokenType('}', {
next === bracketRTok
)
}
return next === dotTok || next === eofTok
return next === dotTok || next === eofTok || next === commaTok
},
expectPrev(prev) {
return prev === nameTok || prev === braceRTok || prev === bracketRTok
Expand Down Expand Up @@ -214,6 +214,7 @@ export const parenLTok = TokenType('(', {
return (
next === nameTok ||
next === bracketDLTok ||
next === braceLTok ||
next === bangTok ||
next === bracketLTok
)
Expand Down

0 comments on commit f621d98

Please sign in to comment.