Skip to content

Commit

Permalink
fix: improve the behavior when inputting an invalid command comment
Browse files Browse the repository at this point in the history
  • Loading branch information
wadackel committed Aug 28, 2024
1 parent 13b1e61 commit cc2b343
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
21 changes: 14 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34254,7 +34254,7 @@ const unicodeIdContinueReg = /[0-9A-Z_a-z\xAA\xB5\xB7\xBA\xC0-\xD6\xD8-\xF6\xF8-

/** @see https://github.com/GregRos/parjs/issues/59 */
/* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/consistent-type-imports */
const { anyCharOf, string, stringLen, noCharOf, anyStringOf, regexp, float: parse_float, whitespace, eof } = __nccwpck_require__(8768);
const { rest, anyCharOf, string, stringLen, noCharOf, anyStringOf, regexp, float: parse_float, whitespace, eof } = __nccwpck_require__(8768);
const { map, qthen, or, many, between, then, thenq, manySepBy, stringify } = __nccwpck_require__(7935);
/* eslint-enable @typescript-eslint/no-require-imports, @typescript-eslint/consistent-type-imports */
// String
Expand Down Expand Up @@ -34303,8 +34303,6 @@ const pPair = pKey
const pParams = pPair.pipe(manySepBy(',')).pipe(map((pair) => Object.assign({}, ...pair)));
// Command
const pCommand = string('.').pipe(qthen(pIdent)).pipe(between(whitespace()));
// Program
const pProgram = pCommand.pipe(then(pParams.pipe(or(eof())))).pipe(map((v) => ({ command: v[0], params: v[1] })));
const parse = (input) => {
const _input = input.trim();
if (_input === '') {
Expand All @@ -34314,17 +34312,26 @@ const parse = (input) => {
error: null,
};
}
const result = pProgram.parse(_input);
if (!result.isOk) {
const command = pCommand.pipe(then(rest())).parse(_input);
if (!command.isOk) {
return {
command: null,
params: {},
error: null,
};
}
const params = pParams.pipe(or(eof())).parse(command.value[1]);
if (!params.isOk) {
return {
command: null,
params: null,
error: result.toString(),
error: params.toString(),
};
}
return {
error: null,
...result.value,
command: command.value[0],
params: params.value,
};
};

Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ test.each([
},
],

[
'non command',
`foo bar=baz`,
{
command: null,
params: {},
error: null,
},
],

[
'boolean - true',
`.command key=true`,
Expand Down
24 changes: 17 additions & 7 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { unicodeIdContinueReg, unicodeIdStartReg } from './unicode-regex.js';

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

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

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

const result = pProgram.parse(_input);
if (!result.isOk) {
const command = pCommand.pipe(then(rest())).parse(_input);
if (!command.isOk) {
return {
command: null,
params: {},
error: null,
};
}

const params = pParams.pipe(or(eof())).parse(command.value[1]);
if (!params.isOk) {
return {
command: null,
params: null,
error: result.toString(),
error: params.toString(),
};
}

return {
error: null,
...result.value,
command: command.value[0],
params: params.value,
};
};

0 comments on commit cc2b343

Please sign in to comment.