Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(refactor): extract common logic for preventing use of reserved keyword to a dedicated resource #541

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions src/askvm/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './resource';
export * from './run';
export * from './type';
export * from './typed';
export * from './preventReservedKeywords';
10 changes: 10 additions & 0 deletions src/askvm/lib/preventReservedKeywords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function preventReservedKeywords(key: any): void {
const reservedKeywords = ['resources'];
reservedKeywords.forEach((reservedKeyword) => {
if (key === reservedKeyword) {
throw new Error(
`Key "resources" is a reserved keyword and cannot be used.`
);
}
});
}
2 changes: 1 addition & 1 deletion src/askvm/resources/core/__test__/assign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe(`assign`, function () {
await expect(
ask(`ask(assign('${reservedWord}','CocoResource'))`)
).rejects.toThrow(
`Key "resources" is a reserved keyword and cannot be assigned to.`
`Key "${reservedWord}" is a reserved keyword and cannot be used.`
);
});
});
2 changes: 1 addition & 1 deletion src/askvm/resources/core/__test__/const.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe(`const`, function () {
for (let i = 0; i < reservedWords.length; i++) {
const reservedWord = reservedWords[i];
await expect(ask(`ask(const('resources',6))`)).rejects.toThrow(
`Key "${reservedWord}" cannot be redeclared`
`Key "${reservedWord}" is a reserved keyword and cannot be used.`
);
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/askvm/resources/core/__test__/let.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe(`let`, function () {
for (let i = 0; i < reservedWords.length; i++) {
const reservedWord = reservedWords[i];
await expect(ask(`ask(let('resources',6))`)).rejects.toThrow(
`Key "${reservedWord}" cannot be redeclared`
`Key "${reservedWord}" is a reserved keyword and cannot be used.`
);
}
});
Expand Down
7 changes: 2 additions & 5 deletions src/askvm/resources/core/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Options,
TypedValue,
JSONable,
preventReservedKeywords,
} from '../../lib';

function assignValue(
Expand All @@ -28,11 +29,7 @@ export const assignRes = resource({
const key: any = await runUntyped(options, children[0]); // FIXME any
const value = await run(options, children[1]);

if (key === 'resources') {
throw new Error(
`Key "resources" is a reserved keyword and cannot be assigned to.`
);
}
preventReservedKeywords(key);

let keyFound = false;
for (
Expand Down
6 changes: 2 additions & 4 deletions src/askvm/resources/core/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
runUntyped,
TypedValue,
JSONable,
preventReservedKeywords,
} from '../../lib';

export const constRes = resource({
Expand All @@ -14,10 +15,7 @@ export const constRes = resource({

const key: any = await runUntyped(options, children[0]); // FIXME any
const value = await run(options, children[1]);

if (key === 'resources') {
throw new Error(`Key "resources" cannot be redeclared`);
}
preventReservedKeywords(key);
options.values![key] = value;
Object.freeze(options.values![key]);
return value;
Expand Down
16 changes: 2 additions & 14 deletions src/askvm/resources/core/let.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,11 @@ import {
resource,
run,
runUntyped,
Options,
TypedValue,
JSONable,
preventReservedKeywords,
} from '../../lib';

function assignValue(
valueObject: { [key: string]: any },
key: string,
assignedValue: any
) {
if (Object.isFrozen(valueObject[key]))
throw new Error(`Cannot assign to a constant variable "${key}"`);
valueObject[key] = assignedValue;
}

export const letRes = resource({
type: any,
async compute(options, code, args): Promise<TypedValue<JSONable>> {
Expand All @@ -26,9 +16,7 @@ export const letRes = resource({
const key: any = await runUntyped(options, children[0]); // FIXME any
const value = await run(options, children[1]);

if (key === 'resources') {
throw new Error(`Key "resources" cannot be redeclared`);
}
preventReservedKeywords(key);

options.values![key] = value;

Expand Down