From 51e140026f7cc307709039363d057d8cdbad31e2 Mon Sep 17 00:00:00 2001 From: janrywang Date: Mon, 13 Sep 2021 18:05:57 +0800 Subject: [PATCH] fix(path): fix path match destructor --- packages/path/src/__tests__/match.spec.ts | 22 ++++++++++++++++++++++ packages/path/src/__tests__/parser.spec.ts | 13 ++++++++----- packages/path/src/parser.ts | 14 +++++++++----- packages/path/src/tokens.ts | 5 +++-- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/packages/path/src/__tests__/match.spec.ts b/packages/path/src/__tests__/match.spec.ts index 6f01e322492..c27d5f85e21 100644 --- a/packages/path/src/__tests__/match.spec.ts +++ b/packages/path/src/__tests__/match.spec.ts @@ -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': [ @@ -209,6 +230,7 @@ match({ '*(!aa,bb,bb.aa)': [['xx'], ['yyy']], '*(!aaa)': [['bbb']], '*(!aaa,bbb)': [['ccc'], ['ggg']], + '*([startDate,endDate],date,weak)': [['date']], }) unmatch({ diff --git a/packages/path/src/__tests__/parser.spec.ts b/packages/path/src/__tests__/parser.spec.ts index db07c6306f2..402438d1b5c 100644 --- a/packages/path/src/__tests__/parser.spec.ts +++ b/packages/path/src/__tests__/parser.spec.ts @@ -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', + }, }, }, }, diff --git a/packages/path/src/parser.ts b/packages/path/src/parser.ts index c4225eb99f6..29759e40531 100644 --- a/packages/path/src/parser.ts +++ b/packages/path/src/parser.ts @@ -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( @@ -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 } @@ -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() } @@ -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() } diff --git a/packages/path/src/tokens.ts b/packages/path/src/tokens.ts index 0bd34fe7ebb..20e6cf9d2a1 100644 --- a/packages/path/src/tokens.ts +++ b/packages/path/src/tokens.ts @@ -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) @@ -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 @@ -214,6 +214,7 @@ export const parenLTok = TokenType('(', { return ( next === nameTok || next === bracketDLTok || + next === braceLTok || next === bangTok || next === bracketLTok )