Skip to content

Commit

Permalink
feat: fully supporting import flags
Browse files Browse the repository at this point in the history
  • Loading branch information
martinheidegger committed Mar 23, 2022
1 parent 8ecb592 commit bcba046
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 8 deletions.
22 changes: 18 additions & 4 deletions parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,26 @@ function onoptionMap (tokens) {

function onimport (tokens) {
tokens.shift()
const file = tokens.shift().replace(/^"+|"+$/gm, '')
let token = tokens.shift()

if (tokens[0] !== ';') throw new Error('Unexpected token: ' + tokens[0] + '. Expected ";"')
let flag = null
if (token === 'public' || token === 'weak') {
flag = token
token = tokens.shift()
}
if (!/^".*"$/.test(token)) {
throw new Error('Unexpected import <' + token + '>. Expecting a string literal.')
}
const file = token.replace(/^"+|"+$/gm, '')

tokens.shift()
return file
token = tokens.shift()

if (token !== ';') throw new Error('Unexpected token: ' + token + '. Expected ";"')

return {
file: file,
flag: flag
}
}

function onservice (tokens) {
Expand Down
2 changes: 1 addition & 1 deletion stringify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function onimport (i, result) {
result.push('import "' + i + '";', '')
result.push('import ' + (i.flag ? i.flag + ' "' : '"') + i.file + '";', '')
return result
}

Expand Down
7 changes: 6 additions & 1 deletion test/fixtures/import.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"syntax": 3,
"package": null,
"imports": ["./result.proto", "./other_result.proto"],
"imports": [
{ "file": "./result.proto", "flag": null },
{ "file": "./other_result.proto", "flag": null },
{ "file": "./public.proto", "flag": "public" },
{ "file": "./weak.proto", "flag": "weak" }
],
"enums": [],
"extends": [],
"messages": [
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/import.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import "./result.proto";
import "./other_result.proto";
import public "./public.proto";
import weak "./weak.proto";

message SearchResponse {
repeated Result result = 1;
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/option.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"syntax": 3,
"package": null,
"imports": [
"google/protobuf/descriptor.proto"
{
"file": "google/protobuf/descriptor.proto",
"flag": null
}
],
"enums": [
{
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/search.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"syntax": 3,
"package": null,
"imports": ["./result.proto"],
"imports": [{ "file": "./result.proto", "flag": null }],
"enums": [],
"extends": [],
"messages": [
Expand Down
7 changes: 7 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ tape('schema with imports', function (t) {
t.end()
})

tape('schema without quotes', function (t) {
t.throws(function () {
schema.parse('import foo;')
}, /Unexpected import <foo>. Expecting a string literal./)
t.end()
})

tape('schema with imports loaded by path', function (t) {
t.same(schema.parse(fixture('search.proto')), require('./fixtures/search.json'))
t.end()
Expand Down

0 comments on commit bcba046

Please sign in to comment.