diff --git a/lib/collector.js b/lib/collector.js index 0fbe8aa..a86debc 100644 --- a/lib/collector.js +++ b/lib/collector.js @@ -4,6 +4,7 @@ const assert = require('assert'); const fs = require('fs'); const pathlib = require('path'); +const globals = require('./globals'); const extractors = require('./extractors'); const Command = require('./commands'); const {Scope, Module} = require('./scope'); @@ -18,7 +19,7 @@ class Collector { this.taskCount = 0; this.active = true; this.modules = new Map; - this.global = Scope.global([]); + this.global = Scope.global(globals); this.running = false; } diff --git a/lib/extractors.js b/lib/extractors.js index 70a5910..806e356 100644 --- a/lib/extractors.js +++ b/lib/extractors.js @@ -197,12 +197,12 @@ const extractors = { * GenericTypeAnnotation(node) { const name = yield node.id; - // TODO: shadowing? - if (name === 'Buffer') { - return 'bytes'; + const schema = yield query(name); + + if (schema.$unwrap) { + return schema.type; } - const schema = yield query(name); const enclosing = yield namespace(); if (schema.namespace === enclosing) { @@ -433,30 +433,18 @@ function extractRequire(node) { function parsePragma(pragma) { let [type, arg] = pragma.split(/\s+/); - switch (type) { - case 'null': - case 'int': - case 'long': - case 'float': - case 'double': - case 'bytes': - case 'string': - case 'boolean': - if (arg != null) { - return null; - } - - break; - case 'fixed': - arg = Number(arg); - - if (!Number.isInteger(arg)) { - return null; - } + if (isPrimitive(type)) { + if (arg != null) { + return null; + } + } else if (type === 'fixed') { + arg = Number(arg); - break; - default: + if (!Number.isInteger(arg)) { return null; + } + } else { + return null; } return [type, arg]; @@ -493,6 +481,22 @@ function isEnumSymbol(node) { return node.type === 'StringLiteralTypeAnnotation'; } +function isPrimitive(type) { + switch (type) { + case 'null': + case 'int': + case 'long': + case 'float': + case 'double': + case 'bytes': + case 'string': + case 'boolean': + return true; + default: + return false; + } +} + function unwrapEnumSymbol(node) { return node.value; } diff --git a/lib/globals.js b/lib/globals.js new file mode 100644 index 0000000..ddfa2f9 --- /dev/null +++ b/lib/globals.js @@ -0,0 +1,9 @@ +'use strict'; + +module.exports = [ + { + name: 'Buffer', + type: 'bytes', + $unwrap: true, + }, +]; diff --git a/tests/shadowing.js b/tests/shadowing.js new file mode 100644 index 0000000..adae167 --- /dev/null +++ b/tests/shadowing.js @@ -0,0 +1,11 @@ +type X = { + x: Buffer, +}; + +(function () { + interface Buffer {} + + type Y = { + y: Buffer, + }; +})(); diff --git a/tests/shadowing.json b/tests/shadowing.json new file mode 100644 index 0000000..95c6368 --- /dev/null +++ b/tests/shadowing.json @@ -0,0 +1,27 @@ +{ + "schemas": [ + { + "type": "record", + "name": "X", + "namespace": "shadowing", + "fields": [ + {"name": "x", "type": "bytes"} + ] + }, + { + "type": "record", + "name": "Buffer", + "namespace": "shadowing._1", + "fields": [] + }, + { + "type": "record", + "name": "Y", + "namespace": "shadowing._1", + "fields": [ + {"name": "y", "type": "Buffer"} + ] + } + ], + "taskCount": 2 +}