Skip to content

Commit

Permalink
fix: ignore public/weak tokens in imports
Browse files Browse the repository at this point in the history
  • Loading branch information
martinheidegger committed Mar 23, 2022
1 parent 8ecb592 commit 99aa596
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
15 changes: 12 additions & 3 deletions parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,20 @@ 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 ";"')
if (token === 'public' || token === 'weak') {
token = tokens.shift()
}
if (!/^".*"$/.test(token)) {
throw new Error('Unexpected import <' + token + '>. Expecting a string literal.')
}
const file = token.replace(/^"+|"+$/gm, '')

token = tokens.shift()

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

tokens.shift()
return file
}

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": [
"./result.proto",
"./other_result.proto",
"./public.proto",
"./weak.proto"
],
"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
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 99aa596

Please sign in to comment.