Skip to content

Commit

Permalink
feature: improve highlighting for exported variable names with quotes (
Browse files Browse the repository at this point in the history
  • Loading branch information
levivilet authored Feb 20, 2024
1 parent b80ea1a commit ef0da5c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
},
"test-tokenize": {
"skip": [
"dotenv-multiline",
"php-dotenv-exported"
"dotenv-multiline"
]
}
}
25 changes: 23 additions & 2 deletions src/tokenizeDotenv.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const RE_EVERYTHING = /^./

export const initialLineState = {
state: State.TopLevelContent,
stack: [],
}

export const hasArrayReturn = true
Expand All @@ -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) {
Expand Down Expand Up @@ -139,15 +141,33 @@ 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
case State.AfterKeywordAndHasSeenWhitespace:
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')
}
Expand All @@ -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
Expand Down Expand Up @@ -316,5 +336,6 @@ export const tokenizeLine = (line, lineState) => {
return {
state,
tokens,
stack,
}
}
9 changes: 9 additions & 0 deletions test/baselines/double-quoted-export.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
KeywordExport
Whitespace
PunctuationString
String
PunctuationString
Whitespace
Punctuation
Whitespace
Numeric
52 changes: 52 additions & 0 deletions test/baselines/php-dotenv-exported.txt
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/cases/double-quoted-export.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export "EDQUOTED" = 123

0 comments on commit ef0da5c

Please sign in to comment.