Skip to content

Commit

Permalink
unlex: Escape identifiers after dots
Browse files Browse the repository at this point in the history
  • Loading branch information
tysonmote committed Sep 25, 2019
1 parent 85a4316 commit 6696891
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
5 changes: 2 additions & 3 deletions src/unlexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ test('Unlexer can convert tokens into a string', () => {
})

test('Unlexer escapes', () => {
const str = unlex([t.Ident('a \\ b $')]).code

expect(str).toEqual('a\\ \\\\\\ b\\ \\$')
expect(unlex([t.Ident('a \\ b $')]).code).toEqual('a\\ \\\\\\ b\\ \\$')
expect(unlex([t.Ident('a'), t.Dot(), t.Ident('b c')]).code).toEqual('a.b\\ c')
})

test('Unlexer and lexer play nicely together', () => {
Expand Down
11 changes: 5 additions & 6 deletions src/unlexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ export default function unlex(tokens: Token[]): UnLexResponse {
continue
}

// Don't add unnecessary spaces after dots
if (i !== 0 && tokens[i - 1].type === TokenType.Dot) {
str += token.value
continue
// Add a space, but not after dots
if (i > 0 && tokens[i - 1].type !== TokenType.Dot) {
str += ' '
}

if (token.type === TokenType.Ident) {
str += ' ' + escape(token.value)
str += escape(token.value)
} else {
str += ' ' + token.value
str += token.value
}
}

Expand Down

0 comments on commit 6696891

Please sign in to comment.