UNPKG

7.77 kBJavaScriptView Raw
1import { nodes } from 'decaffeinate-coffeescript';
2import { Access as CS1Access, Arr as CS1Arr, Assign as CS1Assign, Base as CS1Base, Block as CS1Block, BooleanLiteral as CS1BooleanLiteral, Call as CS1Call, Class as CS1Class, Code as CS1Code, Comment as CS1Comment, Existence as CS1Existence, Expansion as CS1Expansion, ExportAllDeclaration as CS1ExportAllDeclaration, ExportDeclaration as CS1ExportDeclaration, ExportDefaultDeclaration as CS1ExportDefaultDeclaration, ExportNamedDeclaration as CS1ExportNamedDeclaration, ExportSpecifier as CS1ExportSpecifier, ExportSpecifierList as CS1ExportSpecifierList, Extends as CS1Extends, For as CS1For, IdentifierLiteral as CS1IdentifierLiteral, If as CS1If, ImportClause as CS1ImportClause, ImportDeclaration as CS1ImportDeclaration, ImportDefaultSpecifier as CS1ImportDefaultSpecifier, ImportNamespaceSpecifier as CS1ImportNamespaceSpecifier, ImportSpecifier as CS1ImportSpecifier, ImportSpecifierList as CS1ImportSpecifierList, In as CS1In, Index as CS1Index, InfinityLiteral as CS1InfinityLiteral, Literal as CS1Literal, ModuleDeclaration as CS1ModuleDeclaration, ModuleSpecifier as CS1ModuleSpecifier, ModuleSpecifierList as CS1ModuleSpecifierList, NaNLiteral as CS1NaNLiteral, NullLiteral as CS1NullLiteral, NumberLiteral as CS1NumberLiteral, Obj as CS1Obj, Op as CS1Op, Param as CS1Param, Parens as CS1Parens, PassthroughLiteral as CS1PassthroughLiteral, PropertyName as CS1PropertyName, Range as CS1Range, RegexLiteral as CS1RegexLiteral, RegexWithInterpolations as CS1RegexWithInterpolations, Return as CS1Return, Slice as CS1Slice, Splat as CS1Splat, StatementLiteral as CS1StatementLiteral, StringLiteral as CS1StringLiteral, StringWithInterpolations as CS1StringWithInterpolations, SuperCall as CS1SuperCall, Switch as CS1Switch, TaggedTemplateCall as CS1TaggedTemplateCall, ThisLiteral as CS1ThisLiteral, Throw as CS1Throw, Try as CS1Try, UndefinedLiteral as CS1UndefinedLiteral, Value as CS1Value, While as CS1While, YieldReturn as CS1YieldReturn, } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
3import { Access, Arr, Assign, Base, Block, BooleanLiteral, Call, Class, Code, Existence, Expansion, ExportAllDeclaration, ExportDeclaration, ExportDefaultDeclaration, ExportNamedDeclaration, ExportSpecifier, ExportSpecifierList, Extends, For, IdentifierLiteral, If, ImportClause, ImportDeclaration, ImportDefaultSpecifier, ImportNamespaceSpecifier, ImportSpecifier, ImportSpecifierList, In, Index, InfinityLiteral, Literal, ModuleDeclaration, ModuleSpecifier, ModuleSpecifierList, NaNLiteral, NullLiteral, NumberLiteral, Obj, Op, Param, Parens, PassthroughLiteral, PropertyName, Range, RegexLiteral, RegexWithInterpolations, Return, Slice, Splat, StatementLiteral, StringLiteral, StringWithInterpolations, SuperCall, Switch, TaggedTemplateCall, ThisLiteral, Throw, Try, UndefinedLiteral, Value, While, YieldReturn, } from 'decaffeinate-coffeescript2/lib/coffeescript/nodes';
4var nodeTypeMap = new Map();
5nodeTypeMap.set(CS1Base, Base);
6nodeTypeMap.set(CS1Block, Block);
7nodeTypeMap.set(CS1Literal, Literal);
8nodeTypeMap.set(CS1NumberLiteral, NumberLiteral);
9nodeTypeMap.set(CS1InfinityLiteral, InfinityLiteral);
10nodeTypeMap.set(CS1NaNLiteral, NaNLiteral);
11nodeTypeMap.set(CS1StringLiteral, StringLiteral);
12nodeTypeMap.set(CS1RegexLiteral, RegexLiteral);
13nodeTypeMap.set(CS1PassthroughLiteral, PassthroughLiteral);
14nodeTypeMap.set(CS1IdentifierLiteral, IdentifierLiteral);
15nodeTypeMap.set(CS1PropertyName, PropertyName);
16nodeTypeMap.set(CS1StatementLiteral, StatementLiteral);
17nodeTypeMap.set(CS1ThisLiteral, ThisLiteral);
18nodeTypeMap.set(CS1UndefinedLiteral, UndefinedLiteral);
19nodeTypeMap.set(CS1NullLiteral, NullLiteral);
20nodeTypeMap.set(CS1BooleanLiteral, BooleanLiteral);
21nodeTypeMap.set(CS1Return, Return);
22nodeTypeMap.set(CS1YieldReturn, YieldReturn);
23nodeTypeMap.set(CS1Value, Value);
24nodeTypeMap.set(CS1Call, Call);
25nodeTypeMap.set(CS1SuperCall, SuperCall);
26nodeTypeMap.set(CS1RegexWithInterpolations, RegexWithInterpolations);
27nodeTypeMap.set(CS1TaggedTemplateCall, TaggedTemplateCall);
28nodeTypeMap.set(CS1Extends, Extends);
29nodeTypeMap.set(CS1Access, Access);
30nodeTypeMap.set(CS1Index, Index);
31nodeTypeMap.set(CS1Range, Range);
32nodeTypeMap.set(CS1Slice, Slice);
33nodeTypeMap.set(CS1Obj, Obj);
34nodeTypeMap.set(CS1Arr, Arr);
35nodeTypeMap.set(CS1Class, Class);
36nodeTypeMap.set(CS1ModuleDeclaration, ModuleDeclaration);
37nodeTypeMap.set(CS1ImportDeclaration, ImportDeclaration);
38nodeTypeMap.set(CS1ImportClause, ImportClause);
39nodeTypeMap.set(CS1ExportDeclaration, ExportDeclaration);
40nodeTypeMap.set(CS1ExportNamedDeclaration, ExportNamedDeclaration);
41nodeTypeMap.set(CS1ExportDefaultDeclaration, ExportDefaultDeclaration);
42nodeTypeMap.set(CS1ExportAllDeclaration, ExportAllDeclaration);
43nodeTypeMap.set(CS1ModuleSpecifierList, ModuleSpecifierList);
44nodeTypeMap.set(CS1ImportSpecifierList, ImportSpecifierList);
45nodeTypeMap.set(CS1ExportSpecifierList, ExportSpecifierList);
46nodeTypeMap.set(CS1ModuleSpecifier, ModuleSpecifier);
47nodeTypeMap.set(CS1ImportSpecifier, ImportSpecifier);
48nodeTypeMap.set(CS1ImportDefaultSpecifier, ImportDefaultSpecifier);
49nodeTypeMap.set(CS1ImportNamespaceSpecifier, ImportNamespaceSpecifier);
50nodeTypeMap.set(CS1ExportSpecifier, ExportSpecifier);
51nodeTypeMap.set(CS1Assign, Assign);
52nodeTypeMap.set(CS1Code, Code);
53nodeTypeMap.set(CS1Param, Param);
54nodeTypeMap.set(CS1Splat, Splat);
55nodeTypeMap.set(CS1Expansion, Expansion);
56nodeTypeMap.set(CS1While, While);
57nodeTypeMap.set(CS1Op, Op);
58nodeTypeMap.set(CS1In, In);
59nodeTypeMap.set(CS1Try, Try);
60nodeTypeMap.set(CS1Throw, Throw);
61nodeTypeMap.set(CS1Existence, Existence);
62nodeTypeMap.set(CS1Parens, Parens);
63nodeTypeMap.set(CS1StringWithInterpolations, StringWithInterpolations);
64nodeTypeMap.set(CS1For, For);
65nodeTypeMap.set(CS1Switch, Switch);
66nodeTypeMap.set(CS1If, If);
67/**
68 * Run the CS1 parser and convert the resulting AST into a CS2-compatible AST.
69 */
70export default function parseCS1AsCS2(source) {
71 var cs1AST = nodes(source);
72 var cs2AST = convertCS1NodeToCS2(cs1AST);
73 if (!(cs2AST instanceof Block)) {
74 throw new Error('Expected top-level CS file to convert to a Block');
75 }
76 return cs2AST;
77}
78function convertCS1NodeToCS2(node) {
79 var cs1Constructor = node.constructor;
80 var cs2Constructor = nodeTypeMap.get(cs1Constructor);
81 if (!cs2Constructor) {
82 throw new Error("Unexpected CS1 type for node " + node);
83 }
84 var result = Object.create(cs2Constructor.prototype);
85 for (var _i = 0, _a = Object.keys(node); _i < _a.length; _i++) {
86 var key = _a[_i];
87 var value = node[key];
88 if (Array.isArray(value) && value.length > 0 && value[0] instanceof CS1Base) {
89 result[key] = value
90 .filter(function (child) { return !(child instanceof CS1Comment); })
91 .map(function (child) { return convertCS1NodeToCS2(child); });
92 }
93 else if (key === 'cases') {
94 // Switch cases have a complex structure, so special-case those.
95 result[key] = value.map(function (_a) {
96 var switchCaseCondition = _a[0], block = _a[1];
97 if (Array.isArray(switchCaseCondition)) {
98 return [
99 switchCaseCondition.map(function (condition) { return convertCS1NodeToCS2(condition); }),
100 convertCS1NodeToCS2(block)
101 ];
102 }
103 else {
104 return [convertCS1NodeToCS2(switchCaseCondition), convertCS1NodeToCS2(block)];
105 }
106 });
107 }
108 else if (value instanceof CS1Base) {
109 result[key] = convertCS1NodeToCS2(value);
110 }
111 else {
112 result[key] = value;
113 }
114 }
115 return result;
116}