Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: implement mappers for interpolated strings and heregexes #190

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/mappers/mapCall.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import SourceType from 'coffee-lex/dist/SourceType';
import { Call, Splat } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
import { Call, Literal, Parens, Splat, Value } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
import { inspect } from 'util';
import {
AssignOp,
Expand All @@ -8,7 +8,9 @@ import {
} from '../nodes';
import isHeregexTemplateNode from '../util/isHeregexTemplateNode';
import locationsEqual from '../util/locationsEqual';
import makeHeregex from '../util/makeHeregex';
import ParseContext from '../util/ParseContext';
import parseString from '../util/parseString';
import mapAny from './mapAny';
import { UnsupportedNodeError } from './mapAnyWithFallback';
import mapBase from './mapBase';
Expand All @@ -17,7 +19,22 @@ export default function mapCall(context: ParseContext, node: Call): Node {
let { line, column, start, end, raw } = mapBase(context, node);

if (isHeregexTemplateNode(node, context)) {
throw new UnsupportedNodeError(node);
let firstArg = node.args[0];
if (!(firstArg instanceof Value) || !(firstArg.base instanceof Parens)) {
throw new Error('Expected a valid first heregex arg in the AST.');
}
let strNode = firstArg.base.body.expressions[0];
let flags;
if (node.args.length > 1) {
let secondArg = node.args[1];
if (!(secondArg instanceof Value) || !(secondArg.base instanceof Literal)) {
throw new Error('Expected a string flags value in the heregex AST.');
}
flags = parseString(secondArg.base.value);
} else {
flags = '';
}
return makeHeregex(context, strNode, flags);
}

let args = node.args.map(arg => mapAny(context, arg));
Expand Down
18 changes: 11 additions & 7 deletions src/mappers/mapOp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { inspect } from 'util';
import {
BinaryOp, BitAndOp, BitNotOp, BitOrOp, BitXorOp, ChainedComparisonOp, DeleteOp, DivideOp, ExpOp, EQOp, GTEOp, GTOp,
InstanceofOp, LeftShiftOp, LogicalAndOp, LogicalNotOp, LogicalOrOp, LTEOp, LTOp, ModuloOp, MultiplyOp, NewOp, Node,
NEQOp, OfOp, Op, OperatorInfo, PostDecrementOp, PostIncrementOp, PreDecrementOp, PreIncrementOp, RemOp,
SignedRightShiftOp, SubtractOp, TypeofOp, UnaryNegateOp, UnaryOp, UnaryPlusOp, UnsignedRightShiftOp, Yield, YieldFrom,
YieldReturn
NEQOp, OfOp, Op, OperatorInfo, PlusOp, PostDecrementOp, PostIncrementOp, PreDecrementOp, PreIncrementOp,
RemOp, SignedRightShiftOp, SubtractOp, TypeofOp, UnaryNegateOp, UnaryOp, UnaryPlusOp, UnsignedRightShiftOp, Yield,
YieldFrom, YieldReturn
} from '../nodes';
import getOperatorInfoInRange from '../util/getOperatorInfoInRange';
import isChainedComparison from '../util/isChainedComparison';
import isImplicitPlusOp from '../util/isImplicitPlusOp';
import makeString from '../util/makeString';
import ParseContext from '../util/ParseContext';
import unwindChainedComparison from '../util/unwindChainedComparison';
import mapAny from './mapAny';
Expand Down Expand Up @@ -150,12 +152,14 @@ function mapOpWithoutChainedComparison(context: ParseContext, node: CoffeeOp): N
throw new UnsupportedNodeError(node);
}

function mapPlusOp(context: ParseContext, node: CoffeeOp): Op {
function mapPlusOp(context: ParseContext, node: CoffeeOp): Node {
if (node.second) {
// TODO: string interpolations and binary addition
throw new UnsupportedNodeError(node);
if (isImplicitPlusOp(node, context)) {
return makeString(context, node);
} else {
return mapBinaryOp(context, node, PlusOp);
}
}

return mapUnaryOp(context, node, UnaryPlusOp);
}

Expand Down
3 changes: 1 addition & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import unwindChainedComparison from './util/unwindChainedComparison';
import getOperatorInfoInRange from './util/getOperatorInfoInRange';
import isHeregexTemplateNode from './util/isHeregexTemplateNode';
import isImplicitPlusOp from './util/isImplicitPlusOp';
import isInterpolatedString from './util/isInterpolatedString';
import fixInvalidLocationData from './util/fixInvalidLocationData';
import lex, { SourceType } from 'coffee-lex';
import locationsEqual from './util/locationsEqual';
Expand Down Expand Up @@ -400,7 +399,7 @@ function convert(context: ParseContext, map: (context: ParseContext, node: Base,

case 'Op': {
return map(context, node, () => {
if (isImplicitPlusOp(node, context) && isInterpolatedString(node, ancestors, context)) {
if (isImplicitPlusOp(node, context)) {
let { quasis, unmappedExpressions, start, end } = getTemplateLiteralComponents(context, node);
return makeNodeFromSourceRange(context, 'String', start, end, {
quasis,
Expand Down
74 changes: 0 additions & 74 deletions src/util/isInterpolatedString.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/util/makeString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { String } from '../nodes';
import getTemplateLiteralComponents from './getTemplateLiteralComponents';
import ParseContext from './ParseContext';

export default function makeString(context: ParseContext, node: Base) {
export default function makeString(context: ParseContext, node: Base): String {
let { quasis, unmappedExpressions, start, end } = getTemplateLiteralComponents(context, node);
let startLoc = context.linesAndColumns.locationForIndex(start);
if (!startLoc) {
Expand Down
52 changes: 0 additions & 52 deletions test/util/isInterpolatedString_test.js

This file was deleted.