-
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.
refactor: restructure
ChainedComparisonOp
to make chained operands …
…explicit (#145) Refs decaffeinate/decaffeinate#783 BREAKING CHANGE: Rather than relying on a visitor to infer which nested binary operations are part of the chain, this flattens the structure to enumerate the operands and operators in lists.
- Loading branch information
1 parent
a9a8c5a
commit 2f21b1e
Showing
16 changed files
with
673 additions
and
229 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
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,29 @@ | ||
import SourceType from 'coffee-lex/dist/SourceType'; | ||
import { OperatorInfo } from '../nodes'; | ||
import ParseContext from './ParseContext'; | ||
|
||
export default function getOperatorInfoInRange(context: ParseContext, start: number, end: number): OperatorInfo { | ||
let startIndex = context.sourceTokens.indexOfTokenNearSourceIndex(start); | ||
let endIndex = context.sourceTokens.indexOfTokenNearSourceIndex(end); | ||
|
||
if (!startIndex || !endIndex) { | ||
throw new Error(`cannot find token indexes of range bounds: [${start}, ${end}]`); | ||
} | ||
|
||
let operatorIndex = context.sourceTokens.indexOfTokenMatchingPredicate( | ||
token => token.type !== SourceType.LPAREN && token.type !== SourceType.RPAREN, | ||
startIndex.next(), | ||
endIndex | ||
); | ||
|
||
let operatorToken = operatorIndex && context.sourceTokens.tokenAtIndex(operatorIndex); | ||
|
||
if (!operatorToken) { | ||
throw new Error(`cannot find operator token in range: [${start}, ${end}]`); | ||
} | ||
|
||
return new OperatorInfo( | ||
context.source.slice(operatorToken.start, operatorToken.end), | ||
operatorToken | ||
); | ||
} |
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,25 @@ | ||
import { Base, Op } from 'decaffeinate-coffeescript/lib/coffee-script/nodes'; | ||
import { inspect } from 'util'; | ||
|
||
export default function unwindChainedComparison(node: Op): Array<Base> { | ||
let operands: Array<Base> = []; | ||
|
||
for (let link: Base = node;;) { | ||
if (link instanceof Op) { | ||
let { first, second } = link; | ||
|
||
if (!second) { | ||
throw new Error(`unexpected unary operator inside chained comparison: ${inspect(node)}`); | ||
} | ||
|
||
operands = [second, ...operands]; | ||
|
||
link = first; | ||
} else { | ||
operands = [link, ...operands]; | ||
break; | ||
} | ||
} | ||
|
||
return operands; | ||
} |
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
Oops, something went wrong.