Skip to content

Commit cdb6530

Browse files
authored
Merge pull request #5 from knowledge-work/fix/non-command-string
Fix a bug that causes an error when the command comment format is invalid
2 parents 13b1e61 + cc2b343 commit cdb6530

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

dist/index.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -34254,7 +34254,7 @@ const unicodeIdContinueReg = /[0-9A-Z_a-z\xAA\xB5\xB7\xBA\xC0-\xD6\xD8-\xF6\xF8-
3425434254

3425534255
/** @see https://github.com/GregRos/parjs/issues/59 */
3425634256
/* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/consistent-type-imports */
34257-
const { anyCharOf, string, stringLen, noCharOf, anyStringOf, regexp, float: parse_float, whitespace, eof } = __nccwpck_require__(8768);
34257+
const { rest, anyCharOf, string, stringLen, noCharOf, anyStringOf, regexp, float: parse_float, whitespace, eof } = __nccwpck_require__(8768);
3425834258
const { map, qthen, or, many, between, then, thenq, manySepBy, stringify } = __nccwpck_require__(7935);
3425934259
/* eslint-enable @typescript-eslint/no-require-imports, @typescript-eslint/consistent-type-imports */
3426034260
// String
@@ -34303,8 +34303,6 @@ const pPair = pKey
3430334303
const pParams = pPair.pipe(manySepBy(',')).pipe(map((pair) => Object.assign({}, ...pair)));
3430434304
// Command
3430534305
const pCommand = string('.').pipe(qthen(pIdent)).pipe(between(whitespace()));
34306-
// Program
34307-
const pProgram = pCommand.pipe(then(pParams.pipe(or(eof())))).pipe(map((v) => ({ command: v[0], params: v[1] })));
3430834306
const parse = (input) => {
3430934307
const _input = input.trim();
3431034308
if (_input === '') {
@@ -34314,17 +34312,26 @@ const parse = (input) => {
3431434312
error: null,
3431534313
};
3431634314
}
34317-
const result = pProgram.parse(_input);
34318-
if (!result.isOk) {
34315+
const command = pCommand.pipe(then(rest())).parse(_input);
34316+
if (!command.isOk) {
34317+
return {
34318+
command: null,
34319+
params: {},
34320+
error: null,
34321+
};
34322+
}
34323+
const params = pParams.pipe(or(eof())).parse(command.value[1]);
34324+
if (!params.isOk) {
3431934325
return {
3432034326
command: null,
3432134327
params: null,
34322-
error: result.toString(),
34328+
error: params.toString(),
3432334329
};
3432434330
}
3432534331
return {
3432634332
error: null,
34327-
...result.value,
34333+
command: command.value[0],
34334+
params: params.value,
3432834335
};
3432934336
};
3433034337

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/parse.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ test.each([
4040
},
4141
],
4242

43+
[
44+
'non command',
45+
`foo bar=baz`,
46+
{
47+
command: null,
48+
params: {},
49+
error: null,
50+
},
51+
],
52+
4353
[
4454
'boolean - true',
4555
`.command key=true`,

src/parse.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { unicodeIdContinueReg, unicodeIdStartReg } from './unicode-regex.js';
22

33
/** @see https://github.com/GregRos/parjs/issues/59 */
44
/* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/consistent-type-imports */
5-
const { anyCharOf, string, stringLen, noCharOf, anyStringOf, regexp, float, whitespace, eof } =
5+
const { rest, anyCharOf, string, stringLen, noCharOf, anyStringOf, regexp, float, whitespace, eof } =
66
require('parjs') as typeof import('parjs');
77
const { map, qthen, or, many, between, then, thenq, manySepBy, stringify } =
88
require('parjs/combinators') as typeof import('parjs/combinators');
@@ -76,8 +76,8 @@ const pParams = pPair.pipe(manySepBy(',')).pipe(map((pair) => Object.assign({},
7676
// Command
7777
const pCommand = string('.').pipe(qthen(pIdent)).pipe(between(whitespace()));
7878

79-
// Program
80-
const pProgram = pCommand.pipe(then(pParams.pipe(or(eof())))).pipe(map((v) => ({ command: v[0], params: v[1] })));
79+
// // Program
80+
// const pProgram = pCommand.pipe(then(pParams.pipe(or(eof())))).pipe(map((v) => ({ command: v[0], params: v[1] })));
8181

8282
// Main Parser
8383
export type Params = Record<string, string | number | boolean | null>;
@@ -108,17 +108,27 @@ export const parse = (input: string): ParseResult => {
108108
};
109109
}
110110

111-
const result = pProgram.parse(_input);
112-
if (!result.isOk) {
111+
const command = pCommand.pipe(then(rest())).parse(_input);
112+
if (!command.isOk) {
113+
return {
114+
command: null,
115+
params: {},
116+
error: null,
117+
};
118+
}
119+
120+
const params = pParams.pipe(or(eof())).parse(command.value[1]);
121+
if (!params.isOk) {
113122
return {
114123
command: null,
115124
params: null,
116-
error: result.toString(),
125+
error: params.toString(),
117126
};
118127
}
119128

120129
return {
121130
error: null,
122-
...result.value,
131+
command: command.value[0],
132+
params: params.value,
123133
};
124134
};

0 commit comments

Comments
 (0)