Skip to content

Commit

Permalink
Merge pull request #141 from mongodb-js/export-fn-to-js
Browse files Browse the repository at this point in the history
feat: compile functions to js
  • Loading branch information
mcasimir committed Feb 24, 2021
1 parent 7871374 commit 2987990
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 13 deletions.
25 changes: 25 additions & 0 deletions packages/bson-transpilers/.github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Run Tests
on:
push:
branches:
- master
pull_request:

jobs:
check-and-test:
name: Check and Test
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12.4.0

- name: Install NPM Packages
run: npm ci

- name: Check and Test
run: |
npm run check
npm run compile
npm run test
9 changes: 0 additions & 9 deletions packages/bson-transpilers/.travis.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,12 @@ module.exports = (ANTLRVisitor) => class CodeGenerationVisitor extends ANTLRVisi
);
}

generateFuncDefExpression() {
throw new BsonTranspilersUnimplementedError(
'Support for exporting functions to languages other than javascript is not yet available.'
);
}

/**
* Overrides the ANTLR visitChildren method so that options can be set.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ module.exports = (Visitor) => class Generator extends Visitor {
constructor() {
super();
}

generateFuncDefExpression(ctx) {
return ctx.getText();
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ module.exports = (CodeGenerationVisitor) => class Visitor extends CodeGeneration
this.visitTypeofExpression =
this.visitInExpression =
this.visitInstanceofExpression =
this.visitFuncDefExpression =
this.visitAssignmentExpression =
this.visitAssignmentOperatorExpression =
this.visitMemberIndexExpression =
Expand Down Expand Up @@ -288,6 +287,10 @@ module.exports = (CodeGenerationVisitor) => class Visitor extends CodeGeneration
return this.visitChildren(ctx);
}

visitFuncDefExpression(ctx) {
return this.generateFuncDefExpression(ctx);
}

/*
*
* Process Methods
Expand Down
5 changes: 2 additions & 3 deletions packages/bson-transpilers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
"antlr4-js": "java -Xmx500M -cp './antlr-4.7.2-complete.jar:$CLASSPATH' org.antlr.v4.Tool -Dlanguage=JavaScript -lib grammars -o lib/antlr -visitor -Xexact-output-dir grammars/ECMAScript.g4",
"antlr4-py": "java -Xmx500M -cp './antlr-4.7.2-complete.jar:$CLASSPATH' org.antlr.v4.Tool -Dlanguage=JavaScript -lib grammars -o lib/antlr -visitor -Xexact-output-dir grammars/Python3.g4",
"symbol-table": "node compile-symbol-table.js",
"test": "npm run symbol-table && mocha",
"test": "mocha",
"prepublishOnly": "npm run compile",
"check": "mongodb-js-precommit './codegeneration/**/*{.js,.jsx}' './test/**/*.js' index.js",
"ci": "npm run check && npm run test"
"check": "mongodb-js-precommit './codegeneration/**/*{.js,.jsx}' './test/**/*.js' index.js"
},
"homepage": "http://github.com/mongodb-js/bson-transpilers",
"repository": {
Expand Down
29 changes: 29 additions & 0 deletions packages/bson-transpilers/test/functions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const bsonTranspilers = require('..');
const assert = require('assert');

const {
BsonTranspilersUnimplementedError
} = require('../helper/error');

describe('function expressions (shell)', () => {
it('compiles functions to javascript', () => {
assert.strictEqual(
bsonTranspilers.shell.javascript.compile('function(){}'),
'function(){}'
);
});

['object', 'csharp', 'java', 'python'].forEach((language) => {
it(`throws an unsupported error compiling functions to ${language}`, () => {
assert.throws(
() => {
bsonTranspilers.shell[language].compile('function(){}');
},
new BsonTranspilersUnimplementedError(
'Support for exporting functions to languages other than javascript is not yet available.'
)
);
});
});
});

0 comments on commit 2987990

Please sign in to comment.