Support class referencing from inside

master
Paul Loyd 2017-11-03 01:42:40 +03:00
parent e8fdcccdea
commit a49624fb6e
4 changed files with 36 additions and 5 deletions

View File

@ -27,11 +27,11 @@ class Collector {
const scope = global.extend(namespace);
this._visit(ast.program, scope);
this._spawn(ast.program, scope);
}
// Given the AST output of babylon parse, walk through in a depth-first order.
_visit(root, scope) {
_spawn(root, scope) {
let stack;
let parent;
let keys = [];
@ -74,7 +74,7 @@ class Collector {
const extractor = extractors[node.type];
if (!extractor) {
this._visit(node, scope);
this._spawn(node, scope);
return null;
}
@ -93,6 +93,10 @@ class Collector {
result = null;
} else if (value instanceof Command) {
switch (value.name) {
case 'spawn':
this._spawn(value.data, scope);
break;
case 'provide':
const schema = value.data;

View File

@ -1,6 +1,10 @@
'use strict';
class Command {
static spawn(node) {
return new Command('spawn', node);
}
static provide(schema) {
return new Command('provide', schema);
}

View File

@ -2,7 +2,7 @@
const assert = require('assert');
const {provide, query, enter, exit, namespace} = require('./commands');
const {spawn, provide, query, enter, exit, namespace} = require('./commands');
const {partition} = require('./utils');
const extractors = {
@ -56,7 +56,7 @@ const extractors = {
},
* ClassMethod(node) {
yield node.body;
yield spawn(node.body);
return null;
},

23
tests/typeInMethod.js Normal file
View File

@ -0,0 +1,23 @@
class Test {
foo() {
type X = {
t: Test,
};
}
}
// ###
[
{
type: 'record',
name: 'Test',
namespace: 'typeInMethod',
fields: [],
},
{
type: 'record',
name: 'X',
namespace: 'typeInMethod._0',
fields: [{name: 't', type: 'typeInMethod.Test'}],
},
]