Skip to content

Commit b8d048f

Browse files
committed
Treat numbers in the root scope as number literals
1 parent 85f5f8c commit b8d048f

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/Lexer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ class StreamLexer {
312312
}
313313
if (numberLooking.includes(literalString[0])) {
314314
// eslint-disable-next-line @typescript-eslint/naming-convention
315-
const [_, ok] = this.parseJSON(literalString);
315+
const [, ok] = this.parseJSON(literalString);
316316
return ok;
317317
}
318318
return false;

src/Parser.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ class TokenParser {
110110
switch (token.type) {
111111
case Token.TOK_VARIABLE:
112112
return { type: 'Variable', name: token.value as string };
113+
case Token.TOK_NUMBER: {
114+
if (this.lookahead(0) === Token.TOK_RBRACKET) {
115+
throw new Error('Syntax error: numeric literal is not allowed as identifier.');
116+
} else {
117+
return { type: 'Literal', value: token.value };
118+
}
119+
}
113120
case Token.TOK_LITERAL:
114121
return { type: 'Literal', value: token.value };
115122
case Token.TOK_UNQUOTEDIDENTIFIER: {
@@ -190,7 +197,7 @@ class TokenParser {
190197
}
191198
case Token.TOK_LPAREN: {
192199
const args: ExpressionNode[] = [];
193-
let expression = this.expression(0);
200+
const expression = this.expression(0);
194201
args.push(expression);
195202
this.match(Token.TOK_RPAREN);
196203
return args[0];

test/jmespath-extensions.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,47 @@ describe('root', () => {
216216
expect(value).toEqual([1, 99]);
217217
});
218218
});
219+
220+
describe('arithmetic', () => {
221+
it('should add', () => {
222+
const value = search({ foo: 60, bar: 10 }, 'foo + bar');
223+
expect(value).toEqual(70);
224+
});
225+
226+
it('should subtract', () => {
227+
const value = search({ foo: 60, bar: 10 }, 'foo - bar');
228+
expect(value).toEqual(50);
229+
});
230+
231+
it('should multiply', () => {
232+
const value = search({ foo: 60, bar: 10 }, 'foo * bar');
233+
expect(value).toEqual(600);
234+
});
235+
236+
it('should divide', () => {
237+
const value = search({ foo: 60, bar: 10 }, 'foo / bar');
238+
expect(value).toEqual(6);
239+
});
240+
241+
it('should mod', () => {
242+
const value = search({ foo: 62, bar: 10 }, 'foo % bar');
243+
expect(value).toEqual(2);
244+
});
245+
246+
it('should div', () => {
247+
const value = search({ foo: 62, bar: 10 }, 'foo // bar');
248+
expect(value).toEqual(6);
249+
});
250+
});
251+
252+
describe('number literals', () => {
253+
it('should compare number literals', () => {
254+
const value = search({ foo: 60, bar: 10 }, 'foo > 50');
255+
expect(value).toEqual(true);
256+
});
257+
258+
it('should add number literals', () => {
259+
const value = search({}, '10 + 60');
260+
expect(value).toEqual(70);
261+
});
262+
});

0 commit comments

Comments
 (0)