From 99aa59698eb737f72a1e6c73953dee40ae96aac2 Mon Sep 17 00:00:00 2001 From: Martin Heidegger Date: Tue, 22 Mar 2022 02:00:07 +0900 Subject: [PATCH] fix: ignore public/weak tokens in imports --- parse.js | 15 ++++++++++++--- test/fixtures/import.json | 7 ++++++- test/fixtures/import.proto | 2 ++ test/index.js | 7 +++++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/parse.js b/parse.js index 2889818..61ee4d2 100644 --- a/parse.js +++ b/parse.js @@ -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 } diff --git a/test/fixtures/import.json b/test/fixtures/import.json index bb53a72..baaa3fd 100644 --- a/test/fixtures/import.json +++ b/test/fixtures/import.json @@ -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": [ diff --git a/test/fixtures/import.proto b/test/fixtures/import.proto index 692ca71..434d355 100644 --- a/test/fixtures/import.proto +++ b/test/fixtures/import.proto @@ -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; diff --git a/test/index.js b/test/index.js index 0b2b056..e8c1305 100644 --- a/test/index.js +++ b/test/index.js @@ -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 . 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()