Skip to content

Commit 655f568

Browse files
authored
feat: switch to the CS2 AST format (#317)
There's some CS2-specific code, but we don't yet ever run the CS2 parser. Instead, we run the CS1 parser, convert that AST to a CS2 AST, then convert that to a decaffeinate-parser AST. This should make it possible to variably use the CS1 or CS2 parser depending on configuration. BREAKING CHANGE: Node 4 is no longer supported. The returned CoffeeScript AST is now a CS2 AST, and there are slight differences in ranges and AST structure for comment-only blocks.
1 parent 8c30515 commit 655f568

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+349
-131
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ node_js:
1010
- '9'
1111
- '8'
1212
- '6'
13-
- '4'
1413
before_install:
1514
# TODO: Switch this back to yarn when greenkeeperio/greenkeeper-lockfile#98 is fixed.
1615
# https://github.com/greenkeeperio/greenkeeper-lockfile/issues/98

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
"files": [
2323
"dist/"
2424
],
25+
"engines": {
26+
"node": ">=6"
27+
},
2528
"dependencies": {
2629
"babylon": "^6.18.0",
2730
"coffee-lex": "^8.1.1",
2831
"decaffeinate-coffeescript": "1.12.7-patch.2",
32+
"decaffeinate-coffeescript2": "2.2.1-patch.1",
2933
"json-stable-stringify": "^1.0.1",
3034
"lines-and-columns": "^1.1.6"
3135
},

src/ext/coffee-script.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { Base, Op } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
1+
import { Base as CS1Base, Op as CS1Op } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
2+
import { Base, Op } from 'decaffeinate-coffeescript2/lib/coffeescript/nodes';
23

34
export function patchCoffeeScript(): void {
5+
CS1Op.prototype.invert = invert;
6+
CS1Base.prototype.invert = invert;
47
Op.prototype.invert = invert;
58
Base.prototype.invert = invert;
69
}

src/mappers/mapAny.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
2-
Arr, Assign, Base, Block, Call, Class, Code, Comment, Existence, Expansion, Extends,
3-
For, If, In, Literal, ModuleDeclaration, Obj, Op, Param, Parens, Range, Return, Splat, Switch, TaggedTemplateCall,
4-
Throw, Try, Value, While,
5-
} from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
2+
Arr, Assign, Base, Block, Call, Class, Code, Existence, Expansion, Extends, For, If, In, Literal, ModuleDeclaration,
3+
Obj, Op, Param, Parens, Range, Return, Splat, StringWithInterpolations, Switch, TaggedTemplateCall, Throw, Try, Value,
4+
While,
5+
} from 'decaffeinate-coffeescript2/lib/coffeescript/nodes';
66
import { Node } from '../nodes';
77
import ParseContext from '../util/ParseContext';
88
import UnsupportedNodeError from '../util/UnsupportedNodeError';
@@ -79,7 +79,7 @@ export default function mapAny(context: ParseContext, node: Base): Node {
7979
return mapObj(context, node);
8080
}
8181

82-
if (node instanceof Parens) {
82+
if (node instanceof Parens || node instanceof StringWithInterpolations) {
8383
return mapParens(context, node);
8484
}
8585

@@ -143,10 +143,5 @@ export default function mapAny(context: ParseContext, node: Base): Node {
143143
return mapModuleDeclaration(context, node);
144144
}
145145

146-
if (node instanceof Comment) {
147-
throw new UnsupportedNodeError(
148-
node, 'Expected comment notes to be filtered out by mapBlock rather than processed directly.');
149-
}
150-
151146
throw new UnsupportedNodeError(node);
152147
}

src/mappers/mapArr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Arr } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
1+
import { Arr } from 'decaffeinate-coffeescript2/lib/coffeescript/nodes';
22
import { ArrayInitialiser } from '../nodes';
33
import getLocation from '../util/getLocation';
44
import ParseContext from '../util/ParseContext';

src/mappers/mapAssign.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Assign } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
1+
import { Assign } from 'decaffeinate-coffeescript2/lib/coffeescript/nodes';
22
import { AssignOp, BaseAssignOp, BitAndOp, BitOrOp, BitXorOp, CompoundAssignOp, DivideOp, ExistsOp, ExpOp, FloorDivideOp, LeftShiftOp, LogicalAndOp, LogicalOrOp, ModuloOp, MultiplyOp, PlusOp, RemOp, SignedRightShiftOp, SubtractOp, UnsignedRightShiftOp } from '../nodes';
33
import getLocation from '../util/getLocation';
44
import ParseContext from '../util/ParseContext';

src/mappers/mapBlock.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { SourceType } from 'coffee-lex';
2-
import { Assign, Base, Block as CoffeeBlock, Comment, Obj, Value } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
2+
import { Assign, Base, Block as CoffeeBlock, Obj, Value } from 'decaffeinate-coffeescript2/lib/coffeescript/nodes';
33
import { inspect } from 'util';
44
import { AssignOp, Block, BoundFunction, BoundGeneratorFunction, ClassProtoAssignOp, Constructor, Identifier, MemberAccessOp, Node, This } from '../nodes';
55
import getLocation from '../util/getLocation';
6+
import isCommentOnlyNode from '../util/isCommentOnlyNode';
67
import ParseContext from '../util/ParseContext';
78
import mapAny from './mapAny';
89

@@ -28,7 +29,7 @@ export default function mapBlock(context: ParseContext, node: CoffeeBlock): Bloc
2829
return new Block(
2930
line, column, start, end, raw,
3031
node.expressions
31-
.filter(expression => !(expression instanceof Comment))
32+
.filter(expression => !isCommentOnlyNode(expression))
3233
.map(expression => mapChild(context, childContext, expression))
3334
.reduce((arr, current) => arr.concat(current), []),
3435
inline
@@ -44,9 +45,7 @@ function mapChild(blockContext: ParseContext, childContext: ParseContext, node:
4445

4546
let statements: Array<Node> = [];
4647
for (let property of obj.properties) {
47-
if (property instanceof Comment) {
48-
continue;
49-
} else if (property instanceof Assign) {
48+
if (property instanceof Assign) {
5049
let { line, column, start, end, raw } = getLocation(childContext, property);
5150
let key = mapAny(childContext, property.variable);
5251
let value = mapAny(childContext, property.value);

src/mappers/mapCall.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import SourceType from 'coffee-lex/dist/SourceType';
2-
import { Call, Literal, Parens, Splat, SuperCall, Value } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
2+
import {
3+
Call,
4+
Literal,
5+
Splat,
6+
StringWithInterpolations,
7+
SuperCall,
8+
Value,
9+
} from 'decaffeinate-coffeescript2/lib/coffeescript/nodes';
310
import { inspect } from 'util';
411
import {
512
AssignOp,
@@ -20,7 +27,7 @@ export default function mapCall(context: ParseContext, node: Call): Node {
2027

2128
if (isHeregexTemplateNode(node, context)) {
2229
let firstArg = node.args[0];
23-
if (!(firstArg instanceof Value) || !(firstArg.base instanceof Parens)) {
30+
if (!(firstArg instanceof Value) || !(firstArg.base instanceof StringWithInterpolations)) {
2431
throw new Error('Expected a valid first heregex arg in the AST.');
2532
}
2633
let strNode = firstArg.base.body.expressions[0];

src/mappers/mapClass.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Class as CoffeeClass } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
1+
import { Class as CoffeeClass } from 'decaffeinate-coffeescript2/lib/coffeescript/nodes';
22
import { Class } from '../nodes';
33
import getLocation from '../util/getLocation';
44
import ParseContext from '../util/ParseContext';

src/mappers/mapCode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Code } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
1+
import { Code } from 'decaffeinate-coffeescript2/lib/coffeescript/nodes';
22
import { BaseFunction, BoundFunction, BoundGeneratorFunction, Function, GeneratorFunction } from '../nodes';
33
import getLocation from '../util/getLocation';
44
import ParseContext from '../util/ParseContext';

0 commit comments

Comments
 (0)