From dc7ee9c050af793f1aea524da4723a40a8bfaa62 Mon Sep 17 00:00:00 2001 From: Yilun Sun Date: Mon, 8 Jan 2024 18:17:13 +0800 Subject: [PATCH] fix(parser): not able to correctly parse key in array shape and object shape node --- ...-c7976742-fba3-495d-8c41-1bda8d80516f.json | 7 ++++++ src/phpdoc-parser/parser/type-parser.ts | 22 +++++++++---------- 2 files changed, 17 insertions(+), 12 deletions(-) create mode 100644 change/@rightcapital-phpdoc-parser-c7976742-fba3-495d-8c41-1bda8d80516f.json diff --git a/change/@rightcapital-phpdoc-parser-c7976742-fba3-495d-8c41-1bda8d80516f.json b/change/@rightcapital-phpdoc-parser-c7976742-fba3-495d-8c41-1bda8d80516f.json new file mode 100644 index 0000000..e9d4c7e --- /dev/null +++ b/change/@rightcapital-phpdoc-parser-c7976742-fba3-495d-8c41-1bda8d80516f.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "fix(parser): not able to correctly parse key in array shape and object shape node", + "packageName": "@rightcapital/phpdoc-parser", + "email": "yilunsun11@yeah.net", + "dependentChangeType": "patch" +} diff --git a/src/phpdoc-parser/parser/type-parser.ts b/src/phpdoc-parser/parser/type-parser.ts index bb6cd24..caa61d0 100644 --- a/src/phpdoc-parser/parser/type-parser.ts +++ b/src/phpdoc-parser/parser/type-parser.ts @@ -751,16 +751,14 @@ export class TypeParser { ): ConstExprIntegerNode | ConstExprStringNode | IdentifierTypeNode { const startLine = tokens.currentTokenLine(); const startIndex = tokens.currentTokenIndex(); + let key: ConstExprIntegerNode | IdentifierTypeNode | ConstExprStringNode; if (tokens.isCurrentTokenType(Lexer.TOKEN_INTEGER)) { - const key = new ConstExprIntegerNode( + key = new ConstExprIntegerNode( tokens.currentTokenValue().replaceAll('_', ''), ); tokens.next(); - return this.enrichWithAttributes(tokens, key, startLine, startIndex); - } - let key: ConstExprIntegerNode | IdentifierTypeNode | ConstExprStringNode; - if (tokens.isCurrentTokenType(Lexer.TOKEN_SINGLE_QUOTED_STRING)) { + } else if (tokens.isCurrentTokenType(Lexer.TOKEN_SINGLE_QUOTED_STRING)) { if (this.quoteAwareConstExprString) { key = new QuoteAwareConstExprStringNode( StringUnescaper.unescapeString(tokens.currentTokenValue()), @@ -772,8 +770,7 @@ export class TypeParser { ); } tokens.next(); - } - if (tokens.isCurrentTokenType(Lexer.TOKEN_DOUBLE_QUOTED_STRING)) { + } else if (tokens.isCurrentTokenType(Lexer.TOKEN_DOUBLE_QUOTED_STRING)) { if (this.quoteAwareConstExprString) { key = new QuoteAwareConstExprStringNode( StringUnescaper.unescapeString(tokens.currentTokenValue()), @@ -784,10 +781,12 @@ export class TypeParser { tokens.currentTokenValue().replace(/(^"|"$)/g, ''), ); } + tokens.next(); } else { key = new IdentifierTypeNode(tokens.currentTokenValue()); tokens.consumeTokenType(Lexer.TOKEN_IDENTIFIER); } + return this.enrichWithAttributes< ConstExprIntegerNode | IdentifierTypeNode | ConstExprStringNode >(tokens, key, startLine, startIndex); @@ -839,8 +838,8 @@ export class TypeParser { ): ConstExprStringNode | IdentifierTypeNode { const startLine = tokens.currentTokenLine(); const startIndex = tokens.currentTokenIndex(); - let key: ConstExprStringNode | IdentifierTypeNode; + if (tokens.isCurrentTokenType(Lexer.TOKEN_SINGLE_QUOTED_STRING)) { if (this.quoteAwareConstExprString) { key = new QuoteAwareConstExprStringNode( @@ -849,12 +848,11 @@ export class TypeParser { ); } else { key = new ConstExprStringNode( - tokens.currentTokenValue().replace(/(^"|"$)/g, ''), + tokens.currentTokenValue().replace(/(^'|'$)/g, ''), ); } tokens.next(); - } - if (tokens.isCurrentTokenType(Lexer.TOKEN_DOUBLE_QUOTED_STRING)) { + } else if (tokens.isCurrentTokenType(Lexer.TOKEN_DOUBLE_QUOTED_STRING)) { if (this.quoteAwareConstExprString) { key = new QuoteAwareConstExprStringNode( StringUnescaper.unescapeString(tokens.currentTokenValue()), @@ -865,12 +863,12 @@ export class TypeParser { tokens.currentTokenValue().replace(/(^"|"$)/g, ''), ); } - tokens.next(); } else { key = new IdentifierTypeNode(tokens.currentTokenValue()); tokens.consumeTokenType(Lexer.TOKEN_IDENTIFIER); } + return this.enrichWithAttributes(tokens, key, startLine, startIndex); } }