Skip to content

Commit

Permalink
feat: allow - in identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
belopash committed Nov 11, 2024
1 parent 31a145b commit 7029188
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@subsquid/manifest",
"type": "commonjs",
"version": "2.0.0-beta.8",
"version": "2.0.0-beta.9",
"homepage": "https://www.subsquid.io",
"repository": "https://github.com/subsquid/manifest.git",
"license": "GPL-3.0-or-later",
Expand Down
14 changes: 9 additions & 5 deletions src/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import assert from 'assert';

export const EXPR_PATTERN = /(\${{[^}]*}})/;

const nums = new Set('0123456789'.split(''));
const alpha = new Set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''));
const alphaNum = new Set([...nums, ...alpha]);
const identifiers = new Set([...alphaNum, '_', '-']);

export class Parser {
constructor() {}

Expand Down Expand Up @@ -69,11 +74,10 @@ export class Tokenizer {

id() {
const start = this.pos;
while (
this.str[this.pos] &&
(/[a-zA-Z_$]/.test(this.str[this.pos]) ||
(this.pos > start && /[0-9]/.test(this.str[this.pos])))
) {
while (identifiers.has(this.str[this.pos])) {
if (this.pos === start && nums.has(this.str[this.pos])) break;
if (this.str[this.pos] === '-' && !alphaNum.has(this.str[this.pos + 1])) break;

this.pos++;
}

Expand Down
2 changes: 1 addition & 1 deletion src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,4 @@ export const manifestSchema = Joi.object<ManifestValue>({
.description('[DEPRECATED] Please use "manifest_version" instead.')
.valid(...AVAILABLE_MANIFEST_VERSIONS)
.meta({ deprecated: true }),
}).oxor('slot', 'version', 'tag');
}).oxor('slot', 'version', 'tag');
2 changes: 1 addition & 1 deletion test/addon_postgres.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,4 @@ describe('Addon Postgres', () => {

expect(error).toBeUndefined();
});
});
});
27 changes: 27 additions & 0 deletions test/expression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@ describe('Expression', () => {
expect(value).toEqual('foo');
});

it('should resolve identifier 2', () => {
const value = parser.parse('${{foo-bar}}').eval({ ['foo-bar']: 'foo-bar' });
expect(value).toEqual('foo-bar');
});

it('should resolve identifier 3', () => {
const value = parser.parse('${{foo_bar}}').eval({ ['foo_bar']: 'foo_bar' });
expect(value).toEqual('foo_bar');
});

it('should resolve identifier 4', () => {
const value = parser.parse('${{foo1337}}').eval({ ['foo1337']: 'foo1337' });
expect(value).toEqual('foo1337');
});

it('should throw on invalid identifier', () => {
expect(() => parser.parse('${{foo-}}').eval({})).toThrow(
new EvaluationError("Unexpected '-' [0,6]"),
);
});

it('should throw on invalid identifier 2', () => {
expect(() => parser.parse('${{9foo}}').eval({})).toThrow(
new EvaluationError("Unexpected '9' [0,3]"),
);
});

it('should throw on not defined identifier', () => {
expect(() => parser.parse('${{foo}}').eval({})).toThrow(
new EvaluationError('"foo" is not defined'),
Expand Down

0 comments on commit 7029188

Please sign in to comment.