Skip to content

Commit

Permalink
Handle Buffer shadowing
Browse files Browse the repository at this point in the history
  • Loading branch information
loyd committed Nov 5, 2017
1 parent bdd83fe commit 62fd586
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 27 deletions.
3 changes: 2 additions & 1 deletion lib/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
}

Expand Down
56 changes: 30 additions & 26 deletions lib/extractors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions lib/globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = [
{
name: 'Buffer',
type: 'bytes',
$unwrap: true,
},
];
11 changes: 11 additions & 0 deletions tests/shadowing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type X = {
x: Buffer,
};

(function () {
interface Buffer {}

type Y = {
y: Buffer,
};
})();
27 changes: 27 additions & 0 deletions tests/shadowing.json
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 62fd586

Please sign in to comment.