Skip to content

Commit

Permalink
Remove native JavaScript Gherkin parser - delegate to Go executable (#…
Browse files Browse the repository at this point in the history
…448)

* Remove native JavaScript Gherkin parser - delegate to Go executable

* misc js build improvements

* Prettify js
  • Loading branch information
aslakhellesoy authored Aug 4, 2018
1 parent 3602f00 commit 8b85909
Show file tree
Hide file tree
Showing 84 changed files with 9,377 additions and 16,810 deletions.
21 changes: 21 additions & 0 deletions .templates/javascript/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"parserOptions": {
"ecmaVersion": 8
},
"plugins": [
"prettier"
],
"rules": {
"prettier/prettier": [
"error", {"trailingComma": "es5", "singleQuote": true, "semi": false}
]
},
"env": {
"node": true,
"es6": true
},
"extends": [
"eslint:recommended",
"prettier"
]
}
13 changes: 13 additions & 0 deletions .templates/javascript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.idea/
.nyc_output/
coverage/
dist/
node_modules/
yarn.lock
npm-debug.log
package-lock.json
.deps
.tested
.eslinted
acceptance
*-go/
9 changes: 7 additions & 2 deletions .templates/javascript/default.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@ ASYNC_SUPPORTED := $(shell node --eval "async function foo(){}" 2> /dev/null; ec

.tested: yarn.lock .deps $(JAVASCRIPT_SOURCE_FILES)
ifeq ($(ASYNC_SUPPORTED),0)
yarn test
yarn coverage
touch $@
else
yarn build
yarn build-test
yarn mocha-built
touch $@
endif

default: .tested
default: .tested .eslinted
.PHONY: default

.eslinted: $(JAVASCRIPT_SOURCE_FILES)
yarn eslint-fix
touch $@

yarn.lock: package.json
yarn install --network-concurrency 1
yarn link
Expand Down
10 changes: 10 additions & 0 deletions .templates/javascript/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var path = require('path')
var fs = require('fs')
var exec = require('child_process').exec

fs.access(path.join(__dirname, '..', 'dist'), function(err) {
if (!err) return
exec('yarn build', { cwd: path.join(__dirname, '..') }, function(err) {
if (err) throw err
})
})
1 change: 1 addition & 0 deletions .templates/javascript/test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--recursive
4 changes: 3 additions & 1 deletion cucumber-expressions/javascript/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ dist/
node_modules/
yarn.lock
npm-debug.log
build/
package-lock.json
.deps
.tested
.eslinted
acceptance
*-go/
9 changes: 7 additions & 2 deletions cucumber-expressions/javascript/default.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@ ASYNC_SUPPORTED := $(shell node --eval "async function foo(){}" 2> /dev/null; ec

.tested: yarn.lock .deps $(JAVASCRIPT_SOURCE_FILES)
ifeq ($(ASYNC_SUPPORTED),0)
yarn test
yarn coverage
touch $@
else
yarn build
yarn build-test
yarn mocha-built
touch $@
endif

default: .tested
default: .tested .eslinted
.PHONY: default

.eslinted: $(JAVASCRIPT_SOURCE_FILES)
yarn eslint-fix
touch $@

yarn.lock: package.json
yarn install --network-concurrency 1
yarn link
Expand Down
4 changes: 1 addition & 3 deletions cucumber-expressions/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
"description": "Cucumber Expressions - a simpler alternative to Regular Expressions",
"main": "dist/src/index.js",
"scripts": {
"test": "npm run eslint && npm run coverage",
"eslint": "eslint src test",
"test": "mocha",
"eslint-fix": "eslint --fix src test",
"mocha": "mocha",
"coverage": "nyc --reporter=html --reporter=text mocha",
"build": "babel src --out-dir dist/src",
"build-test": "babel test --out-dir dist/test",
Expand Down
10 changes: 0 additions & 10 deletions cucumber-expressions/javascript/scripts/test

This file was deleted.

1 change: 1 addition & 0 deletions cucumber-expressions/javascript/test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--recursive
21 changes: 21 additions & 0 deletions gherkin/javascript/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"parserOptions": {
"ecmaVersion": 8
},
"plugins": [
"prettier"
],
"rules": {
"prettier/prettier": [
"error", {"trailingComma": "es5", "singleQuote": true, "semi": false}
]
},
"env": {
"node": true,
"es6": true
},
"extends": [
"eslint:recommended",
"prettier"
]
}
11 changes: 9 additions & 2 deletions gherkin/javascript/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
acceptance/
.idea/
.nyc_output/
coverage/
dist/
node_modules/
npm-debug.log
yarn.lock
npm-debug.log
package-lock.json
.deps
.tested
.eslinted
acceptance
*-go/
5 changes: 0 additions & 5 deletions gherkin/javascript/.npmignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
berp
examples
test
testdata
.gitrepo
*.razor
*.berp
Makefile
3 changes: 0 additions & 3 deletions gherkin/javascript/.rsync
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@
../../.templates/javascript/ .
../../.templates/s3-download scripts/s3-download
../testdata/ testdata/
../gherkin.berp gherkin.berp
../bin/ berp/
../gherkin-languages.json lib/gherkin/gherkin-languages.json
4 changes: 0 additions & 4 deletions gherkin/javascript/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,3 @@ Just run `npm test` from this directory (you need to `npm install` first).

Keep in mind that this will only run unit tests. The acceptance tests are only
run when you build with `make`.

## Browser Build

make dist/gherkin.js
25 changes: 4 additions & 21 deletions gherkin/javascript/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,27 @@ include default.mk
GOOD_FEATURE_FILES = $(shell find testdata/good -name "*.feature")
BAD_FEATURE_FILES = $(shell find testdata/bad -name "*.feature")

TOKENS = $(patsubst testdata/%.feature,acceptance/testdata/%.feature.tokens,$(GOOD_FEATURE_FILES))
ASTS = $(patsubst testdata/%.feature,acceptance/testdata/%.feature.ast.ndjson,$(GOOD_FEATURE_FILES))
PICKLES = $(patsubst testdata/%.feature,acceptance/testdata/%.feature.pickles.ndjson,$(GOOD_FEATURE_FILES))
SOURCES = $(patsubst testdata/%.feature,acceptance/testdata/%.feature.source.ndjson,$(GOOD_FEATURE_FILES))
PROTOBUFS = $(patsubst testdata/%.feature,acceptance/testdata/%.feature.protobuf.bin.ndjson,$(GOOD_FEATURE_FILES))
ERRORS = $(patsubst testdata/%.feature,acceptance/testdata/%.feature.errors.ndjson,$(BAD_FEATURE_FILES))

.DELETE_ON_ERROR:

.deps: lib/gherkin/parser.js
.deps:
./scripts/s3-download gherkin-go $(LIBRARY_VERSION)
# Linking will only work when we're building in the monorepo, so allow this to fail
# If it fails, we'll be using cucumber-messages from package.json
-yarn link cucumber-messages
touch $@

.tested: $(PROTOBUFS) $(TOKENS) $(ASTS) $(PICKLES) $(ERRORS) $(SOURCES)

acceptance/testdata/%.feature.tokens: testdata/%.feature testdata/%.feature.tokens .built
mkdir -p `dirname $@`
bin/gherkin-generate-tokens $< > $@
diff --unified $<.tokens $@
.tested: $(ASTS) $(PICKLES) $(ERRORS) $(SOURCES)

acceptance/testdata/%.feature.ast.ndjson: testdata/%.feature testdata/%.feature.ast.ndjson .built
mkdir -p `dirname $@`
bin/gherkin --no-source --no-pickles $< | jq --sort-keys --compact-output "." > $@
diff --unified <(jq "." $<.ast.ndjson) <(jq "." $@)

acceptance/testdata/%.feature.protobuf.bin.ndjson: testdata/%.feature.protobuf.bin .built
mkdir -p `dirname $@`
cat $< | bin/gherkin | jq --sort-keys --compact-output "." > $@
diff --unified <(jq "." $<.ndjson) <(jq "." $@)

acceptance/testdata/%.feature.pickles.ndjson: testdata/%.feature testdata/%.feature.pickles.ndjson .built
mkdir -p `dirname $@`
bin/gherkin --no-source --no-ast $< | jq --sort-keys --compact-output "." > $@
Expand All @@ -50,12 +39,6 @@ acceptance/testdata/%.feature.errors.ndjson: testdata/%.feature testdata/%.featu
bin/gherkin --no-source $< | jq --sort-keys --compact-output "." > $@
diff --unified <(jq "." $<.errors.ndjson) <(jq "." $@)

lib/gherkin/parser.js: gherkin.berp gherkin-javascript.razor berp/berp.exe
-mono berp/berp.exe -g gherkin.berp -t gherkin-javascript.razor -o $@
# Remove BOM
awk 'NR==1{sub(/^\xef\xbb\xbf/,"")}{print}' < $@ > $@.nobom
mv $@.nobom $@

clean:
rm -rf acceptance lib/gherkin/parser.js
rm -rf acceptance gherkin-go
.PHONY: clean
2 changes: 1 addition & 1 deletion gherkin/javascript/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[![Build Status](https://secure.travis-ci.org/cucumber/gherkin-javascript.svg)](http://travis-ci.org/cucumber/gherkin-javascript)

Gherkin parser/compiler for JavaScript. Please see [Gherkin](https://github.com/cucumber/gherkin) for details.
Gherkin parser/compiler for JavaScript. Please see [Gherkin](https://github.com/cucumber/cucumber/tree/master/gherkin) for details.
Binary file removed gherkin/javascript/berp/CommandLine.dll
Binary file not shown.
Binary file removed gherkin/javascript/berp/RazorEngine.dll
Binary file not shown.
Binary file removed gherkin/javascript/berp/RazorEngine.pdb
Binary file not shown.
Binary file removed gherkin/javascript/berp/System.Web.Razor.dll
Binary file not shown.
Binary file removed gherkin/javascript/berp/berp.exe
Binary file not shown.
Binary file removed gherkin/javascript/berp/berp.pdb
Binary file not shown.
96 changes: 27 additions & 69 deletions gherkin/javascript/bin/gherkin
Original file line number Diff line number Diff line change
@@ -1,90 +1,48 @@
#!/usr/bin/env node
'use strict'
var Stream = require('stream')
var fs = require('fs')
var m = require('cucumber-messages').io.cucumber.messages
var SourceStream = require('../lib/gherkin/messages/source_stream')
var MessageStream = require('../lib/gherkin/messages/message_stream')
var ProtobufMessageStream = require('../lib/gherkin/messages/protobuf_message_stream')
var args = process.argv.slice(2)
var options = {
const Stream = require('stream')
const Gherkin = require('../src/Gherkin')
const args = process.argv.slice(2)
const options = {
includeSource: true,
includeGherkinDocument: true,
includePickles: true,
protobuf: false
}

var paths = []
const paths = []
while (args.length > 0) {
var arg = args.shift()
const arg = args.shift()
switch (arg) {
case '--no-source':
options.includeSource = false;
break;
options.includeSource = false
break

case '--no-ast':
options.includeGherkinDocument = false;
break;
options.includeGherkinDocument = false
break

case '--no-pickles':
options.includePickles = false;
break;

case '--protobuf':
options.protobuf = true;
break;
options.includePickles = false
break

default:
paths.push(arg);
paths.push(arg)
}
}

function pipeMessagesTo(paths, i, stream) {
var path = paths[i++]
if (!path) {
return stream.end()
const protobufNdsonStream = new Stream.Transform({
objectMode: true,
transform: function (message, _, callback) {
const ob = message.constructor.toObject(message, {defaults: true})
// This reviver omits printing fields with empty values
// This is to make it behave the same as Golang's protobuf->JSON converter
const json = JSON.stringify(ob, function (key, value) {
return (value === null) || (value === "") || (value === 0) || (Array.isArray(value) && value.length === 0) ? undefined : value
})
this.push(json + "\n")
callback()
}
var messageStream = createMessageStream(path)
messageStream.pipe(stream, {end: false})
messageStream.on('end', () => pipeMessagesTo(paths, i, stream))
}

function createMessageStream(path) {
return fs.createReadStream(path, {encoding: 'UTF-8'})
.pipe(new SourceStream(path))
.pipe(new MessageStream(options))
}
})

var messageTransformerStream
if (options.protobuf) {
messageTransformerStream = new Stream.Transform({
objectMode: true,
transform: function (message, _, callback) {
var buffer = message.constructor.encodeDelimited(message).finish()
this.push(buffer)
callback()
}
})
} else {
messageTransformerStream = new Stream.Transform({
objectMode: true,
transform: function (message, _, callback) {
var ob = message.constructor.toObject(message, {defaults: true})
// This reviver omits printing fields with empty values
// This is to make it behave the same as Golang's protobuf->JSON converter
var json = JSON.stringify(ob, function (key, value) {
return (value === null) || (value === "") || (value === 0) || (Array.isArray(value) && value.length === 0) ? undefined : value
});
this.push(json + "\n")
callback()
}
})
}

messageTransformerStream.pipe(process.stdout)

if (paths.length > 0) {
pipeMessagesTo(paths, 0, messageTransformerStream)
} else {
process.stdin.pipe(new ProtobufMessageStream(m.Wrapper)).pipe(messageTransformerStream)
}
const gherkin = new Gherkin(paths, [], options)
gherkin.messages().pipe(protobufNdsonStream).pipe(process.stdout)
16 changes: 0 additions & 16 deletions gherkin/javascript/bin/gherkin-generate-tokens

This file was deleted.

Loading

0 comments on commit 8b85909

Please sign in to comment.