-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
73 lines (64 loc) · 2.19 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const assert = require('assert');
const {
tokenizer,
parser,
transformer,
codeGenerator,
compiler
} = require('./js-trailing-closure-toy-compiler');
const input = `
a(1){
}
a{
}
`;
// tokens
const tokens = [{type:'Identifier',value:'a'},
{type:'ParenLeft',value:'('},
{type:'NumericLiteral',value:'1'},
{type:'ParenRight',value:')'},
{type:'BraceLeft',value:'{'},
{type:'BraceRight',value:'}'},
{type:'Identifier',value:'a'},
{type:'BraceLeft',value:'{'},
{type:'BraceRight',value:'}'}];
// jtc AST
const jtcAst = {
type:'Program',
body:[{type:'CallExpression',
value:'a',
params:[{type:'NumericLiteral',value:'1',parentType:'ARGUMENTS_PARENT_TYPE'}],
hasTrailingBlock:true,
trailingBlockParams:[],
trailingBody:[]},
{type:'CallExpression',
value:'a',
params:[],
hasTrailingBlock:true,
trailingBlockParams:[],
trailingBody:[]}]};
const newAst = {
type:'Program',
body:[
{type:'CallExpression',
callee:{type:'Identifier',name:'a'},
arguments:[{type:'NumericLiteral',value:'1'},
{type:'ArrowFunctionExpression',
arguments:[],
blockBody:[]}],
blockBody:[]},
{type:'CallExpression',
callee:{type:'Identifier',name:'a'},
arguments:[
{type:'ArrowFunctionExpression',
arguments:[],
blockBody:[]}],
blockBody:[]}]};
const output = `a(1, () => {});
a(() => {})`;
assert.deepStrictEqual(tokenizer(input), tokens, 'Tokenizer should turn `input` string into `tokens` array');
assert.deepStrictEqual(parser(tokens), jtcAst, 'Parser should turn `tokens` array into `jtcAst` object');
assert.deepStrictEqual(transformer(jtcAst), newAst, 'Transformer should turn `jtcAst` Object into `newAst` object');
assert.deepStrictEqual(codeGenerator(newAst), output, 'CodeGenerator should turn `newAst` Object into `output` string');
assert.deepStrictEqual(compiler(input), output, 'Compiler should turn `input` string into `output` string');
console.log('All test cases passed!');