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/
.nyc_output/
coverage/
npm-debug.log
package-lock.json

View File

@ -1,29 +1,3 @@
#!/usr/bin/env node
'use strict';
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);
}
}
require('../lib/cli');

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",
"bin": {
"babylon": "./bin/babylon.js"
"flow2avro": "./bin/flow2avro"
},
"dependencies": {
"babylon": "^6.18.0",
"babylon": "^7.0.0-beta.32",
"json-stringify-pretty-compact": "^1.0.4",
"optimist": "^0.6.1",
"resolve": "^1.5.0"
},
"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",
"mocha": "^4.0.1",
"nyc": "^11.3.0"
},
"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');
const fs = require('fs');
const pathlib = require('path');
import globals from './globals';
import * as extractors from './extractors';
import Command from './commands';
import Module from './module';
import Scope from './scope';
import CircularList from './list';
import {isNode} from './utils';
const globals = require('./globals');
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 {
export default class Collector {
constructor(parser, root = '.') {
this.root = root;
this.parser = parser;
@ -130,7 +128,7 @@ class Collector {
return value;
}
assert(value);
assert.ok(value);
if (value instanceof Command) {
switch (value.name) {
@ -180,7 +178,7 @@ class Collector {
break;
case 'exit':
assert(scope.parent);
assert.ok(scope.parent);
scope = scope.parent;
break;
@ -196,7 +194,7 @@ class Collector {
result.push(yield* this._collect(group, val, scope, params));
}
} else {
assert(isNode(value));
assert.ok(isNode(value));
result = yield* this._collect(group, value, scope, params);
}
}
@ -232,7 +230,7 @@ class Collector {
}
// TODO: reexports.
assert(result.type === 'declaration' || result.type === 'template');
assert.ok(result.type === 'declaration' || result.type === 'template');
scope = result.scope;
name = result.name;
@ -329,5 +327,3 @@ function generateGenericName(base, params) {
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');
const {partition, isNode} = require('./utils');
const definition = {
export const definition = {
entries: [
'TypeAlias',
'InterfaceDeclaration',
@ -245,7 +243,7 @@ const definition = {
},
};
const declaration = {
export const declaration = {
entries: [
// Blocks.
'Program',
@ -533,7 +531,7 @@ function extractPragma(text) {
const pair = parsePragma(pragma);
assert(pair);
assert.ok(pair);
const [type, arg] = pair;
@ -572,7 +570,7 @@ function unwrapEnumSymbol(node) {
}
function makeFullname(schema) {
assert(schema.namespace);
assert.ok(schema.namespace);
return `${schema.namespace}.${schema.name}`;
}
@ -614,8 +612,3 @@ function mergeSchemas(schemas) {
function is(type) {
return node => Boolean(node) && node.type === type;
}
module.exports = {
definition,
declaration,
};

View File

@ -1,6 +1,4 @@
'use strict';
module.exports = [
export default [
{
name: 'Buffer',
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');
class CircularList {
export default class CircularList {
constructor() {
this.mark = Symbol();
this.prev = null;
@ -14,25 +12,25 @@ class CircularList {
}
add(entry) {
assert(!entry[this.mark]);
assert.ok(!entry[this.mark]);
if (this.prev) {
assert(this.walk);
assert.ok(this.walk);
this.prev = this.prev[this.mark] = entry;
} else {
assert(!this.walk);
assert.ok(!this.walk);
this.walk = this.prev = entry;
}
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() {
assert(this.walk);
assert.ok(this.walk);
const removed = this.walk;
@ -47,5 +45,3 @@ class CircularList {
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');
const resolve = require('resolve');
class Module {
export default class Module {
constructor(path, namespace) {
this.path = path;
this.namespace = namespace;
@ -45,5 +42,3 @@ class Module {
return this._exports.values();
}
}
module.exports = Module;

View File

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

View File

@ -1,6 +1,4 @@
'use strict';
function partition(iter, predicate) {
export function partition(iter, predicate) {
const left = [];
const right = [];
@ -11,11 +9,6 @@ function partition(iter, predicate) {
return [left, right];
}
function isNode(it) {
export function isNode(it) {
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';
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const collect = require('..');
import collect from '../src';
function run(title) {
let actual, expected;
@ -27,7 +23,7 @@ function run(title) {
}
function main() {
process.chdir(__dirname);
process.chdir(path.join(__dirname, 'samples'));
fs.readdirSync('.')
.filter(name => path.extname(name) === '.js')