From b0872d04b2d7cc30b61a1d9ebcd24da7cac43554 Mon Sep 17 00:00:00 2001 From: Paul Loyd Date: Mon, 6 Nov 2017 14:29:26 +0300 Subject: [PATCH] Involve groups of extractors --- lib/collector.js | 23 ++++++++++----- lib/extractors.js | 62 +++++++++++++++++++++++++++-------------- tests/scopes.json | 2 +- tests/shadowing.json | 2 +- tests/typeInMethod.json | 2 +- 5 files changed, 60 insertions(+), 31 deletions(-) diff --git a/lib/collector.js b/lib/collector.js index a86debc..2cf50c1 100644 --- a/lib/collector.js +++ b/lib/collector.js @@ -82,8 +82,10 @@ class Collector { if (isNode(node)) { const {type} = node; - if (type && type in extractors) { - const task = this._collect(node, scope); + const group = type && getExtractorsGroup(type); + + if (group) { + const task = this._collect(group, node, scope); this.tasks.add(task); ++this.taskCount; @@ -99,12 +101,15 @@ class Collector { } while (stack); } - * _collect(node, scope) { - const extractor = extractors[node.type]; + * _collect(group, node, scope) { + const extractor = group[node.type]; if (!extractor) { - this._spawn(node, scope); + console.log('!!!!', node.type, group.entries); + //console.dir(node, {colors: true, depth: 5}); return null; + //this._spawn(node, scope); + //return null; } const iter = extractor(node); @@ -163,11 +168,11 @@ class Collector { result = []; for (const val of value) { - result.push(yield* this._collect(val, scope)); + result.push(yield* this._collect(group, val, scope)); } } else { assert(isNode(value)); - result = yield* this._collect(value, scope); + result = yield* this._collect(group, value, scope); } } } @@ -247,4 +252,8 @@ function pathToNamespace(path) { .join('.'); } +function getExtractorsGroup(type) { + return extractors.find(group => group.entries.includes(type)); +} + module.exports = Collector; diff --git a/lib/extractors.js b/lib/extractors.js index 806e356..aa8aa2b 100644 --- a/lib/extractors.js +++ b/lib/extractors.js @@ -5,7 +5,9 @@ const assert = require('assert'); const {spawn, define, external, provide, query, enter, exit, namespace} = require('./commands'); const {partition} = require('./utils'); -const extractors = { +const typedefsGroup = { + entries: ['TypeAlias', 'InterfaceDeclaration', 'ClassDeclaration'], + * TypeAlias(node) { let schema = yield node.right; @@ -216,12 +218,6 @@ const extractors = { return null; }, - * BlockStatement(node) { - yield enter(); - yield node.body; - yield exit(); - }, - * InterfaceExtends(node) { return yield node.id; }, @@ -237,13 +233,27 @@ const extractors = { * CommentBlock(node) { return extractPragma(node.value); }, +}; - /* - * Imports. - * - * TODO: warning about "import typeof". - * TODO: support form "import *". - */ +/* + * TODO: declarations. + */ +const blocksGroup = { + entries: ['BlockStatement'], + + * BlockStatement(node) { + yield enter(); + yield spawn(node.body); + yield exit(); + }, +}; + +/* + * TODO: warning about "import typeof". + * TODO: support form "import *". + */ +const importsGroup = { + entries: ['ImportDeclaration', 'VariableDeclarator'], * ImportDeclaration(node) { const specifiers = yield node.specifiers; @@ -319,12 +329,17 @@ const extractors = { return node.value; }, - /* - * Exports. - * - * TODO: support "export from" form. - * TODO: support commonjs. - */ + * Identifier(node) { + return node.name; + }, +}; + +/* + * TODO: support "export from" form. + * TODO: support commonjs. + */ +const exportsGroup = Object.assign({}, typedefsGroup, { + entries: ['ExportNamedDeclaration', 'ExportDefaultDeclaration'], * ExportDefaultDeclaration(node) { let schema = yield node.declaration; @@ -375,7 +390,7 @@ const extractors = { exported, }); }, -}; +}); function* extractLastPragma(comments) { const pragmas = (yield comments).filter(Boolean); @@ -541,4 +556,9 @@ function mergeSchemas(schemas) { }; } -module.exports = extractors; +module.exports = [ + typedefsGroup, + blocksGroup, + importsGroup, + exportsGroup, +]; diff --git a/tests/scopes.json b/tests/scopes.json index 885cb80..1cf42e9 100644 --- a/tests/scopes.json +++ b/tests/scopes.json @@ -76,5 +76,5 @@ ] } ], - "taskCount": 7 + "taskCount": 16 } diff --git a/tests/shadowing.json b/tests/shadowing.json index 95c6368..b7fc183 100644 --- a/tests/shadowing.json +++ b/tests/shadowing.json @@ -23,5 +23,5 @@ ] } ], - "taskCount": 2 + "taskCount": 4 } diff --git a/tests/typeInMethod.json b/tests/typeInMethod.json index 2d23643..ecf4032 100644 --- a/tests/typeInMethod.json +++ b/tests/typeInMethod.json @@ -13,5 +13,5 @@ "fields": [{"name": "t", "type": "typeInMethod.Test"}] } ], - "taskCount": 2 + "taskCount": 3 }