-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: don't treat explicit + operations as string interpolations (#71)
Closes decaffeinate/decaffeinate#425 Closes decaffeinate/decaffeinate#194 There was already some logic to distinguish between explicit `+` operations and implicit ones generated as part of string interpolation parsing, but that logic wasn't used in all places. Now, we also use it in `isInterpolatedString`, which avoids a problem where `+` was seen as a string interpolation in some cases.
- Loading branch information
1 parent
67b514b
commit d01083f
Showing
5 changed files
with
125 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { OPERATOR } from 'coffee-lex'; | ||
|
||
/** | ||
* Given the ranges of two operands, determine from the token list whether there | ||
* is a real '+' operator between them. A plus operation without an actual '+' | ||
* operator is an implicit string interpolation operation. | ||
*/ | ||
export default function isPlusTokenBetweenRanges(leftRange, rightRange, context) { | ||
let tokens = context.sourceTokens; | ||
let leftEnd = tokens.indexOfTokenContainingSourceIndex(leftRange[1] - 1); | ||
let rightStart = tokens.indexOfTokenContainingSourceIndex(rightRange[0]); | ||
// Normal '+' operators should find tokens here, so if we don't, this must be | ||
// an implicit '+' operator. | ||
if (!leftEnd || !rightStart) { | ||
return false; | ||
} | ||
let tokensBetweenOperands = tokens.slice(leftEnd.next(), rightStart); | ||
// If we find an actual operator, this must have been a real '+'. Otherwise, | ||
// this must be an implicit '+'. | ||
let foundPlusToken = false; | ||
tokensBetweenOperands.forEach(({ type, start, end }) => { | ||
if (type === OPERATOR && context.source.slice(start, end) === '+') { | ||
foundPlusToken = true; | ||
} | ||
}); | ||
return foundPlusToken; | ||
} |
1 change: 1 addition & 0 deletions
1
test/examples/string-interpolation-plus-normal-string/input.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"#{a}" + "b" |
90 changes: 90 additions & 0 deletions
90
test/examples/string-interpolation-plus-normal-string/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
{ | ||
"type": "Program", | ||
"line": 1, | ||
"column": 1, | ||
"range": [ | ||
0, | ||
13 | ||
], | ||
"raw": "\"#{a}\" + \"b\"\n", | ||
"body": { | ||
"type": "Block", | ||
"line": 1, | ||
"column": 1, | ||
"range": [ | ||
0, | ||
12 | ||
], | ||
"statements": [ | ||
{ | ||
"type": "PlusOp", | ||
"line": 1, | ||
"column": 1, | ||
"range": [ | ||
0, | ||
12 | ||
], | ||
"left": { | ||
"type": "TemplateLiteral", | ||
"line": 1, | ||
"column": 1, | ||
"range": [ | ||
0, | ||
6 | ||
], | ||
"raw": "\"#{a}\"", | ||
"quasis": [ | ||
{ | ||
"type": "String", | ||
"line": 1, | ||
"column": 1, | ||
"range": [ | ||
0, | ||
1 | ||
], | ||
"data": "", | ||
"raw": "\"" | ||
}, | ||
{ | ||
"type": "String", | ||
"data": "", | ||
"raw": "\"", | ||
"line": 1, | ||
"column": 6, | ||
"range": [ | ||
5, | ||
6 | ||
] | ||
} | ||
], | ||
"expressions": [ | ||
{ | ||
"type": "Identifier", | ||
"line": 1, | ||
"column": 4, | ||
"range": [ | ||
3, | ||
4 | ||
], | ||
"data": "a", | ||
"raw": "a" | ||
} | ||
] | ||
}, | ||
"right": { | ||
"type": "String", | ||
"line": 1, | ||
"column": 10, | ||
"range": [ | ||
9, | ||
12 | ||
], | ||
"data": "b", | ||
"raw": "\"b\"" | ||
}, | ||
"raw": "\"#{a}\" + \"b\"" | ||
} | ||
], | ||
"raw": "\"#{a}\" + \"b\"" | ||
} | ||
} |