diff --git a/package.json b/package.json index 6faf380..9339f91 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,7 @@ }, "test-tokenize": { "skip": [ - "dotenv-multiline", - "php-dotenv-exported" + "dotenv-multiline" ] } } diff --git a/src/tokenizeDotenv.js b/src/tokenizeDotenv.js index 381d599..2fc64b2 100644 --- a/src/tokenizeDotenv.js +++ b/src/tokenizeDotenv.js @@ -99,6 +99,7 @@ const RE_EVERYTHING = /^./ export const initialLineState = { state: State.TopLevelContent, + stack: [], } export const hasArrayReturn = true @@ -112,6 +113,7 @@ export const tokenizeLine = (line, lineState) => { let tokens = [] let token = TokenType.None let state = lineState.state + let stack = [...lineState.stack] while (index < line.length) { const part = line.slice(index) switch (state) { @@ -139,8 +141,15 @@ export const tokenizeLine = (line, lineState) => { if ((next = part.match(RE_WHITESPACE))) { token = TokenType.Whitespace state = State.AfterKeywordAndHasSeenWhitespace + } else if ((next = part.match(RE_DOUBLE_QUOTE))) { + stack.push(State.AfterVariableName) + token = TokenType.PunctuationString + state = State.InsideDoubleQuoteString + } else if ((next = part.match(RE_SINGLE_QUOTE))) { + stack.push(State.AfterVariableName) + token = TokenType.PunctuationString + state = State.InsideSingleQuoteString } else { - line throw new Error('no') } break @@ -148,6 +157,17 @@ export const tokenizeLine = (line, lineState) => { if ((next = part.match(RE_VARIABLE_NAME))) { token = TokenType.VariableName state = State.AfterVariableName + } else if ((next = part.match(RE_DOUBLE_QUOTE))) { + stack.push(State.AfterVariableName) + token = TokenType.PunctuationString + state = State.InsideDoubleQuoteString + } else if ((next = part.match(RE_SINGLE_QUOTE))) { + stack.push(State.AfterVariableName) + token = TokenType.PunctuationString + state = State.InsideSingleQuoteString + } else if ((next = part.match(RE_WHITESPACE))) { + token = TokenType.Whitespace + state = State.AfterKeywordAndHasSeenWhitespace } else { throw new Error('no') } @@ -169,7 +189,7 @@ export const tokenizeLine = (line, lineState) => { case State.InsideDoubleQuoteString: if ((next = part.match(RE_DOUBLE_QUOTE))) { token = TokenType.PunctuationString - state = State.AfterVariableValue + state = stack.pop() || State.AfterVariableValue } else if ((next = part.match(RE_STRING_DOUBLE_QUOTE_CONTENT))) { token = TokenType.VariableValueString state = State.InsideDoubleQuoteString @@ -316,5 +336,6 @@ export const tokenizeLine = (line, lineState) => { return { state, tokens, + stack, } } diff --git a/test/baselines/double-quoted-export.txt b/test/baselines/double-quoted-export.txt new file mode 100644 index 0000000..1a7e35f --- /dev/null +++ b/test/baselines/double-quoted-export.txt @@ -0,0 +1,9 @@ +KeywordExport +Whitespace +PunctuationString +String +PunctuationString +Whitespace +Punctuation +Whitespace +Numeric \ No newline at end of file diff --git a/test/baselines/php-dotenv-exported.txt b/test/baselines/php-dotenv-exported.txt new file mode 100644 index 0000000..fb504be --- /dev/null +++ b/test/baselines/php-dotenv-exported.txt @@ -0,0 +1,52 @@ +KeywordExport +Whitespace +JsonPropertyName +Punctuation +PunctuationString +String +PunctuationString +NewLine +KeywordExport +Whitespace +JsonPropertyName +Whitespace +Punctuation +Whitespace +PunctuationString +String +PunctuationString +NewLine +KeywordExport +Whitespace +JsonPropertyName +Punctuation +PunctuationString +String +PunctuationString +NewLine +KeywordExport +Whitespace +PunctuationString +String +PunctuationString +Whitespace +Punctuation +Whitespace +Numeric +NewLine +KeywordExport +Whitespace +PunctuationString +String +Punctuation +Whitespace +Error +NewLine +NewLine +KeywordExport +Whitespace +JsonPropertyName +Punctuation +PunctuationString +PunctuationString +NewLine \ No newline at end of file diff --git a/test/cases/double-quoted-export.env b/test/cases/double-quoted-export.env new file mode 100644 index 0000000..8e5a303 --- /dev/null +++ b/test/cases/double-quoted-export.env @@ -0,0 +1 @@ +export "EDQUOTED" = 123 \ No newline at end of file