Skip to content

Commit

Permalink
fix(compiler): do not generate imports for external files (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdiaz5513 authored Nov 20, 2017
1 parent 123fed6 commit b1dd5b3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var tslint = require('gulp-tslint');
var realTslint = require('tslint');
var mergeStream = require('merge-stream');
var spawnSync = require('child_process').spawnSync;
var path = require('path');

// FIXME: Not yet able to compile all the schema files, so this whitelist is needed.
const CAPNP_WHITELIST = [
Expand Down Expand Up @@ -37,7 +38,7 @@ function compileCapnp() {
files.filter(function (file) {
return file.path.endsWith('.capnp') && CAPNP_WHITELIST.some((p) => file.path.endsWith(p));
}).forEach(function (file) {
var options = ['-o./packages/capnpc-ts/bin/capnpc-ts.js', file.path];
var options = ['-o./packages/capnpc-ts/bin/capnpc-ts.js', '-I', path.join(__dirname, 'src', 'std'), file.path];
var result = spawnSync('capnpc', options, { stdio: 'inherit' });
if (result.status !== 0) {
throw new Error('Process exited with non-zero status: ' + result.status);
Expand Down
8 changes: 8 additions & 0 deletions packages/capnpc-ts/src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ export function getUnnamedUnionFields(node: s.Node): s.Field[] {

}

export function hasNode(ctx: CodeGeneratorFileContext, lookup: { getId(): capnp.Uint64 } | capnp.Uint64): boolean {

const id = lookup instanceof capnp.Uint64 ? lookup : lookup.getId();

return ctx.nodes.some((n) => n.getId().equals(id));

}

export function loadRequestedFile(
req: s.CodeGeneratorRequest, file: s.CodeGeneratorRequest_RequestedFile): CodeGeneratorFileContext {

Expand Down
2 changes: 2 additions & 0 deletions packages/capnpc-ts/src/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
getFullClassName,
getJsType,
getUnnamedUnionFields,
hasNode,
lookupNode,
needsConcreteListClass,
} from './file';
Expand Down Expand Up @@ -89,6 +90,7 @@ export function generateNestedImports(ctx: CodeGeneratorFileContext): void {
.forEach((n) => {

const imports = n.getNestedNodes()
.filter((nested) => hasNode(ctx, nested))
.filter((nested) => lookupNode(ctx, nested).isStruct())
.map((nested) => nested.getName()).join(', ');

Expand Down
3 changes: 3 additions & 0 deletions packages/capnpc-ts/test/integration/import-foo.capnp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
@0xfc552bdafbb0b889;

using Cxx = import "/capnp/c++.capnp";
using Bar = import "import-bar.capnp";

$Cxx.namespace("Initrode");

struct Foo {
baz @0 :Bar.Baz;
}

0 comments on commit b1dd5b3

Please sign in to comment.