From f7c7082506fe1154b16df445af6361947bff8d8d Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 18 Sep 2023 00:40:10 +0200 Subject: [PATCH] Fix strings detection (#78) --- lib/index.mjs | 7 +++++++ test/ast.test.js | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/index.mjs b/lib/index.mjs index 8a83622..2ce2778 100644 --- a/lib/index.mjs +++ b/lib/index.mjs @@ -218,12 +218,19 @@ function tokenize(code) { */ function classify(token) { const isLineBreak = token === '\n' + // First checking if they're attributes values if (inJsxTag() && inStringQuotes()) { return T_STRING } + // Then determine if they're jsx literals const isJsxLiterals = inJsxLiterals() if (isJsxLiterals) { return T_JSX_LITERALS + + } + // Determine strings first before other types + if (inStringQuotes()) { + return T_STRING } else if (keywords.has(token)) { return last[1] === '.' ? T_IDENTIFIER : T_KEYWORD } else if (isLineBreak) { diff --git a/test/ast.test.js b/test/ast.test.js index 57f75b3..c7c4797 100644 --- a/test/ast.test.js +++ b/test/ast.test.js @@ -403,6 +403,17 @@ describe('strings', () => { 'keyword', 'identifier', 'sign', 'string', 'string', 'string', ]) }) + + it('number in string', () => { + const code = `'123'\n'true'` + const tokens = tokenize(code) + expect(extractTokenValues(tokens)).toEqual([ + "'", '123', "'", "'", 'true', "'", + ]) + expect(extractTokensTypes(tokens)).toEqual([ + 'string', 'string', 'string', 'break', 'string', 'string', 'string', + ]) + }) }) describe('class', () => {