Skip to content

Commit

Permalink
Merge pull request #105 from siefkenj/fixes1
Browse files Browse the repository at this point in the history
Add ability to specify --dependency style deps
  • Loading branch information
pjmolina authored Apr 15, 2023
2 parents 9e9007c + e6347c7 commit 55f26aa
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
3 changes: 3 additions & 0 deletions examples/bar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file exists as a dependency of minimal-with-dep.pegjs
const foo = 'I AM THE CONST FOO';
export default foo;
2 changes: 2 additions & 0 deletions examples/minimal-with-dep.pegjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
START
= "a" { return foo }
8 changes: 8 additions & 0 deletions src/passes/generate-ts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Config, ast } from 'peggy';
import { escapedString } from '../libs/helpers';
import { TypeExtractor } from '../libs/type-extractor';
import { TsPegjsParserBuildOptions } from '../types';
import { COMMON_TYPES_STR } from './constants';
Expand Down Expand Up @@ -59,6 +60,13 @@ export const generateParser: Config['passes']['generate'][number] = (
// Set a new rootNode that we control
ast.code = rootNode;

if (options.dependencies) {
const dependencyImports = Object.entries(options.dependencies).map(
([varName, importPath]) => `import ${varName} from ${escapedString(importPath)};`
);
rootNode.prepend('\n' + dependencyImports.join('\n') + '\n');
}

// Custom import statements should come near the top, if there are any
if (options.tspegjs?.customHeader) {
rootNode.prepend(options.tspegjs.customHeader + '\n\n');
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ interface _ParserBuildOptions extends BuildOptionsBase {
export type TsPegjsParserBuildOptions = _ParserBuildOptions & {
tspegjs?: TsPegjsOptions;
returnTypes?: Record<string, string>;
dependencies?: Record<string, string>;
};
33 changes: 32 additions & 1 deletion test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import peggy from 'peggy';

// Local imports
import packageJson from '../package.json';
import tspegjs from '../src/tspegjs';

const exec = promisify(execNode);

Expand Down Expand Up @@ -44,6 +43,38 @@ describe('CLI Tests', () => {
throw new Error(stderr);
}
});
it(`Can specify dependency list`, async () => {
const GRAMMAR_FILE = path.join(ROOT_DIR, 'examples/minimal-with-dep.pegjs');
const outTsName = path.join(ROOT_DIR, 'output/minimal-with-dep.ts');
const barSource = path.join(ROOT_DIR, 'examples/bar.ts');
const barDest = path.join(ROOT_DIR, 'output/bar.ts');
// Copy the dependency to the output directory
await exec(`cp "${barSource}" "${barDest}"`);
{
// Create the parser
const { stdout, stderr } = await exec(
`npx peggy --plugin "${PLUGIN_PATH}" --dependency foo:./bar -o "${outTsName}" "${GRAMMAR_FILE}"`
);
if (stderr) {
throw new Error(stderr);
}
}
{
// Compile the parser
const { stdout, stderr } = await exec(
`tsc --target es6 --module commonjs --declaration "${outTsName}"`
);
if (stderr) {
throw new Error(stderr);
}
}
{
// Run the parser
const parserPath = path.join(ROOT_DIR, 'output/minimal-with-dep.js');
const parser = await import(parserPath);
expect(parser.parse('a')).toEqual('I AM THE CONST FOO');
}
});
});

/**
Expand Down

0 comments on commit 55f26aa

Please sign in to comment.