Compile sources with babel

master
Paul Loyd 2017-11-16 15:17:15 +03:00
parent 1fc08b013e
commit f9a80591ad
60 changed files with 152 additions and 183 deletions

10
.babelrc Normal file
View File

@ -0,0 +1,10 @@
{
"presets": [
["@babel/env", {
"targets": {
"node": "6.10"
},
"loose": true
}]
]
}

2
.gitignore vendored
View File

@ -1,4 +1,6 @@
lib/
node_modules/ node_modules/
.nyc_output/ .nyc_output/
coverage/ coverage/
npm-debug.log npm-debug.log
package-lock.json

View File

@ -1,29 +1,3 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict'; require('../lib/cli');
const fs = require('fs');
const stringify = require('json-stringify-pretty-compact');
const argv = require('optimist')
.usage('Usage: $0 <path> ...')
.argv;
const collect = require('..');
argv._.forEach(run);
function run(path) {
if (path === '-') {
path = '/dev/stdin';
}
try {
const {schemas} = collect(path);
console.log(stringify(schemas, {maxLength: 100}));
} catch (ex) {
console.error(ex.message);
console.error(ex.stack);
}
}

View File

@ -1,42 +0,0 @@
'use strict';
class Command {
static declare(name, node, params) {
return new Command('declare', [name, node, params]);
}
static define(schema, declared = true) {
return new Command('define', [schema, declared]);
}
static external(external) {
return new Command('external', external);
}
static provide(name, reference = name) {
return new Command('provide', [name, reference]);
}
static query(name, params = null) {
return new Command('query', [name, params]);
}
static enter() {
return new Command('enter');
}
static exit() {
return new Command('exit');
}
static namespace() {
return new Command('namespace');
}
constructor(name, data) {
this.name = name;
this.data = data;
}
}
module.exports = Command;

View File

@ -1,17 +0,0 @@
'use strict';
const Parser = require('./parser');
const Collector = require('./collector');
function collect(path) {
const parser = new Parser;
const collector = new Collector(parser);
collector.collect(path);
return collector;
}
module.exports = collect;
module.exports.Parser = Parser;
module.exports.Collector = Collector;

View File

@ -19,20 +19,26 @@
], ],
"main": "lib/index.js", "main": "lib/index.js",
"bin": { "bin": {
"babylon": "./bin/babylon.js" "flow2avro": "./bin/flow2avro"
}, },
"dependencies": { "dependencies": {
"babylon": "^6.18.0", "babylon": "^7.0.0-beta.32",
"json-stringify-pretty-compact": "^1.0.4", "json-stringify-pretty-compact": "^1.0.4",
"optimist": "^0.6.1", "optimist": "^0.6.1",
"resolve": "^1.5.0" "resolve": "^1.5.0"
}, },
"devDependencies": { "devDependencies": {
"@babel/cli": "^7.0.0-beta.32",
"@babel/core": "^7.0.0-beta.32",
"@babel/preset-env": "^7.0.0-beta.32",
"@babel/register": "^7.0.0-beta.32",
"jasmine": "^2.8.0", "jasmine": "^2.8.0",
"mocha": "^4.0.1", "mocha": "^4.0.1",
"nyc": "^11.3.0" "nyc": "^11.3.0"
}, },
"scripts": { "scripts": {
"test": "nyc mocha tests/do" "prepare": "npm run build",
"build": "babel src/ -d lib/",
"test": "nyc mocha --require @babel/register tests/run.js"
} }
} }

25
src/cli.js Normal file
View File

@ -0,0 +1,25 @@
import stringify from 'json-stringify-pretty-compact';
import * as optimist from 'optimist';
import collect from '.';
const argv = optimist
.usage('Usage: $0 <path> ...')
.argv;
argv._.forEach(run);
function run(path) {
if (path === '-') {
path = '/dev/stdin';
}
try {
const {schemas} = collect(path);
console.log(stringify(schemas, {maxLength: 100}));
} catch (ex) {
console.error(ex.message);
console.error(ex.stack);
}
}

View File

@ -1,18 +1,16 @@
'use strict'; import * as assert from 'assert';
import * as fs from 'fs';
import * as pathlib from 'path';
const assert = require('assert'); import globals from './globals';
const fs = require('fs'); import * as extractors from './extractors';
const pathlib = require('path'); import Command from './commands';
import Module from './module';
import Scope from './scope';
import CircularList from './list';
import {isNode} from './utils';
const globals = require('./globals'); export default class Collector {
const extractors = require('./extractors');
const Command = require('./commands');
const Module = require('./module');
const Scope = require('./scope');
const CircularList = require('./list');
const {isNode} = require('./utils');
class Collector {
constructor(parser, root = '.') { constructor(parser, root = '.') {
this.root = root; this.root = root;
this.parser = parser; this.parser = parser;
@ -130,7 +128,7 @@ class Collector {
return value; return value;
} }
assert(value); assert.ok(value);
if (value instanceof Command) { if (value instanceof Command) {
switch (value.name) { switch (value.name) {
@ -180,7 +178,7 @@ class Collector {
break; break;
case 'exit': case 'exit':
assert(scope.parent); assert.ok(scope.parent);
scope = scope.parent; scope = scope.parent;
break; break;
@ -196,7 +194,7 @@ class Collector {
result.push(yield* this._collect(group, val, scope, params)); result.push(yield* this._collect(group, val, scope, params));
} }
} else { } else {
assert(isNode(value)); assert.ok(isNode(value));
result = yield* this._collect(group, value, scope, params); result = yield* this._collect(group, value, scope, params);
} }
} }
@ -232,7 +230,7 @@ class Collector {
} }
// TODO: reexports. // TODO: reexports.
assert(result.type === 'declaration' || result.type === 'template'); assert.ok(result.type === 'declaration' || result.type === 'template');
scope = result.scope; scope = result.scope;
name = result.name; name = result.name;
@ -329,5 +327,3 @@ function generateGenericName(base, params) {
return name; return name;
} }
module.exports = Collector;

38
src/commands.js Normal file
View File

@ -0,0 +1,38 @@
export default class Command {
constructor(name, data) {
this.name = name;
this.data = data;
}
}
export function declare(name, node, params) {
return new Command('declare', [name, node, params]);
}
export function define(schema, declared = true) {
return new Command('define', [schema, declared]);
}
export function external(external) {
return new Command('external', external);
}
export function provide(name, reference = name) {
return new Command('provide', [name, reference]);
}
export function query(name, params = null) {
return new Command('query', [name, params]);
}
export function enter() {
return new Command('enter');
}
export function exit() {
return new Command('exit');
}
export function namespace() {
return new Command('namespace');
}

View File

@ -1,11 +1,9 @@
'use strict'; import * as assert from 'assert';
const assert = require('assert'); import {declare, define, external, provide, query, enter, exit, namespace} from './commands';
import {partition, isNode} from './utils';
const {declare, define, external, provide, query, enter, exit, namespace} = require('./commands'); export const definition = {
const {partition, isNode} = require('./utils');
const definition = {
entries: [ entries: [
'TypeAlias', 'TypeAlias',
'InterfaceDeclaration', 'InterfaceDeclaration',
@ -245,7 +243,7 @@ const definition = {
}, },
}; };
const declaration = { export const declaration = {
entries: [ entries: [
// Blocks. // Blocks.
'Program', 'Program',
@ -533,7 +531,7 @@ function extractPragma(text) {
const pair = parsePragma(pragma); const pair = parsePragma(pragma);
assert(pair); assert.ok(pair);
const [type, arg] = pair; const [type, arg] = pair;
@ -572,7 +570,7 @@ function unwrapEnumSymbol(node) {
} }
function makeFullname(schema) { function makeFullname(schema) {
assert(schema.namespace); assert.ok(schema.namespace);
return `${schema.namespace}.${schema.name}`; return `${schema.namespace}.${schema.name}`;
} }
@ -614,8 +612,3 @@ function mergeSchemas(schemas) {
function is(type) { function is(type) {
return node => Boolean(node) && node.type === type; return node => Boolean(node) && node.type === type;
} }
module.exports = {
definition,
declaration,
};

View File

@ -1,6 +1,4 @@
'use strict'; export default [
module.exports = [
{ {
name: 'Buffer', name: 'Buffer',
type: 'bytes', type: 'bytes',

14
src/index.js Normal file
View File

@ -0,0 +1,14 @@
import Parser from './parser';
import Collector from './collector';
// @see babel#6805.
//export {Parser, Collector};
export default function collect(path) {
const parser = new Parser;
const collector = new Collector(parser);
collector.collect(path);
return collector;
}

View File

@ -1,8 +1,6 @@
'use strict'; import * as assert from 'assert';
const assert = require('assert'); export default class CircularList {
class CircularList {
constructor() { constructor() {
this.mark = Symbol(); this.mark = Symbol();
this.prev = null; this.prev = null;
@ -14,25 +12,25 @@ class CircularList {
} }
add(entry) { add(entry) {
assert(!entry[this.mark]); assert.ok(!entry[this.mark]);
if (this.prev) { if (this.prev) {
assert(this.walk); assert.ok(this.walk);
this.prev = this.prev[this.mark] = entry; this.prev = this.prev[this.mark] = entry;
} else { } else {
assert(!this.walk); assert.ok(!this.walk);
this.walk = this.prev = entry; this.walk = this.prev = entry;
} }
entry[this.mark] = this.walk; entry[this.mark] = this.walk;
assert(!this.prev || this.prev[this.mark] === this.walk); assert.ok(!this.prev || this.prev[this.mark] === this.walk);
} }
remove() { remove() {
assert(this.walk); assert.ok(this.walk);
const removed = this.walk; const removed = this.walk;
@ -47,5 +45,3 @@ class CircularList {
return removed; return removed;
} }
} }
module.exports = CircularList;

View File

@ -1,10 +1,7 @@
'use strict'; import * as pathlib from 'path';
import * as resolve from 'resolve';
const pathlib = require('path'); export default class Module {
const resolve = require('resolve');
class Module {
constructor(path, namespace) { constructor(path, namespace) {
this.path = path; this.path = path;
this.namespace = namespace; this.namespace = namespace;
@ -45,5 +42,3 @@ class Module {
return this._exports.values(); return this._exports.values();
} }
} }
module.exports = Module;

View File

@ -1,8 +1,6 @@
'use strict'; import * as babylon from 'babylon';
const babylon = require('babylon'); export default class Parser {
class Parser {
parse(code) { parse(code) {
// This parse configuration is intended to be as permissive as possible. // This parse configuration is intended to be as permissive as possible.
return babylon.parse(code, { return babylon.parse(code, {
@ -14,5 +12,3 @@ class Parser {
}); });
} }
} }
module.exports = Parser;

View File

@ -1,8 +1,6 @@
'use strict'; import * as assert from 'assert';
const assert = require('assert'); export default class Scope {
class Scope {
static global(schemas) { static global(schemas) {
const global = new Scope(null, null); const global = new Scope(null, null);
@ -21,7 +19,7 @@ class Scope {
} }
get namespace() { get namespace() {
assert(this.module); assert.ok(this.module);
let namespace = this.module.namespace; let namespace = this.module.namespace;
@ -38,7 +36,7 @@ class Scope {
} }
addDeclaration(name, node, params) { addDeclaration(name, node, params) {
assert(!this.entries.has(name)); assert.ok(!this.entries.has(name));
const isTemplate = Boolean(params); const isTemplate = Boolean(params);
@ -60,7 +58,7 @@ class Scope {
addInstance(name, schema, params) { addInstance(name, schema, params) {
const template = this.entries.get(name); const template = this.entries.get(name);
assert(template); assert.ok(template);
assert.equal(template.type, 'template'); assert.equal(template.type, 'template');
template.instances.push({params, schema}); template.instances.push({params, schema});
@ -70,10 +68,10 @@ class Scope {
const decl = this.entries.get(schema.name); const decl = this.entries.get(schema.name);
if (declared) { if (declared) {
assert(decl); assert.ok(decl);
assert.equal(decl.type, 'declaration'); assert.equal(decl.type, 'declaration');
} else { } else {
assert(!decl); assert.ok(!decl);
} }
this.entries.set(schema.name, { this.entries.set(schema.name, {
@ -84,7 +82,7 @@ class Scope {
} }
addImport(info) { addImport(info) {
assert(!this.entries.has(info.local)); assert.ok(!this.entries.has(info.local));
this.entries.set(info.local, { this.entries.set(info.local, {
type: 'external', type: 'external',
@ -94,13 +92,13 @@ class Scope {
} }
addExport(name, reference) { addExport(name, reference) {
assert(this.module); assert.ok(this.module);
this.module.addExport(name, this, reference); this.module.addExport(name, this, reference);
} }
resolve(path) { resolve(path) {
assert(this.module); assert.ok(this.module);
return this.module.resolve(path); return this.module.resolve(path);
} }
@ -109,7 +107,7 @@ class Scope {
const entry = this.entries.get(name); const entry = this.entries.get(name);
if (entry && entry.type === 'template') { if (entry && entry.type === 'template') {
assert(params); assert.ok(params);
const augmented = entry.params.map((p, i) => params[i] || p.default); const augmented = entry.params.map((p, i) => params[i] || p.default);
const schema = findInstance(entry, augmented); const schema = findInstance(entry, augmented);
@ -149,5 +147,3 @@ function findInstance(template, queried) {
return null; return null;
} }
module.exports = Scope;

View File

@ -1,6 +1,4 @@
'use strict'; export function partition(iter, predicate) {
function partition(iter, predicate) {
const left = []; const left = [];
const right = []; const right = [];
@ -11,11 +9,6 @@ function partition(iter, predicate) {
return [left, right]; return [left, right];
} }
function isNode(it) { export function isNode(it) {
return it && typeof it === 'object' && it.type; return it && typeof it === 'object' && it.type;
} }
module.exports = {
partition,
isNode,
};

14
tests/do → tests/run.js Executable file → Normal file
View File

@ -1,12 +1,8 @@
#!/usr/bin/env node import * as assert from 'assert';
import * as fs from 'fs';
import * as path from 'path';
'use strict'; import collect from '../src';
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const collect = require('..');
function run(title) { function run(title) {
let actual, expected; let actual, expected;
@ -27,7 +23,7 @@ function run(title) {
} }
function main() { function main() {
process.chdir(__dirname); process.chdir(path.join(__dirname, 'samples'));
fs.readdirSync('.') fs.readdirSync('.')
.filter(name => path.extname(name) === '.js') .filter(name => path.extname(name) === '.js')