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

feat(replacer): allow code to be evaluated #1341

Merged
merged 6 commits into from
Sep 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"blueimp-md5": "2.13.0",
"chalk": "4.0.0",
"eol": "0.9.1",
"expression-eval": "3.1.2",
"fast-glob": "3.1.0",
"jsonpath-plus": "4.0.0",
"lodash": "4.17.19",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/linter.jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ console.log(this.cache.get('test') || this.cache.set('test', []).get('test'));
severity: DiagnosticSeverity.Error,
recommended: true,
description: 'Should be falsy',
message: 'Value {{value|to-string}} should be falsy',
message: 'Value #{{print("value")}} should be falsy',
given: '$..empty',
then: {
function: 'falsy',
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ responses:: !!foo
severity: DiagnosticSeverity.Error,
recommended: true,
description: 'A parameter in the header should be written in kebab-case',
message: '{{value|to-string}} is not kebab-cased: {{error}}',
message: '#{{print("value")}} is not kebab-cased: {{error}}',
given: "$..parameters[?(@.in === 'header')]",
then: {
field: 'name',
Expand Down Expand Up @@ -1063,7 +1063,7 @@ responses:: !!foo
severity: DiagnosticSeverity.Error,
recommended: true,
description: 'Should be falsy',
message: 'Value {{value|to-string}} should be falsy',
message: 'Value #{{print("value")}} should be falsy',
given: '$..empty',
then: {
function: 'falsy',
Expand Down
2 changes: 1 addition & 1 deletion src/functions/__tests__/defined.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('defined', () => {
test('should return an error message if target value is undefined', () => {
expect(runDefined(void 0)).toEqual([
{
message: '{{property|gravis|append-property}}should be defined',
message: '#{{print("property")}}should be defined',
},
]);
});
Expand Down
2 changes: 1 addition & 1 deletion src/functions/__tests__/falsy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('falsy', () => {
test('returns error message if target value is not falsy', () => {
expect(runFalsy(true)).toEqual([
{
message: '{{property|gravis|append-property}}is not falsy',
message: '#{{print("property")}}is not falsy',
},
]);
});
Expand Down
2 changes: 1 addition & 1 deletion src/functions/__tests__/schemaPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe('schema-path', () => {
};
expect(runSchemaPath(target, invalidFieldToCheck, path)).toEqual([
{
message: '{{property|gravis|append-property}}does not exist',
message: '#{{print("property")}}does not exist',
path: ['nonsense'],
},
]);
Expand Down
4 changes: 2 additions & 2 deletions src/functions/__tests__/truthy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ describe('truthy', () => {
test('should return an error message if target value is falsy', () => {
expect(runTruthy(false)).toEqual([
{
message: '{{property|gravis|append-property}}is not truthy',
message: '#{{print("property")}}is not truthy',
},
]);
});

test('should return an error message if target value is null', () => {
expect(runTruthy(null)).toEqual([
{
message: '{{property|gravis|append-property}}is not truthy',
message: '#{{print("property")}}is not truthy',
},
]);
});
Expand Down
2 changes: 1 addition & 1 deletion src/functions/defined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const defined: IFunction = (targetVal): void | IFunctionResult[] => {
if (typeof targetVal === 'undefined') {
return [
{
message: '{{property|gravis|append-property}}should be defined',
message: '#{{print("property")}}should be defined',
},
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/functions/falsy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const falsy: IFunction = (targetVal): void | IFunctionResult[] => {
if (targetVal) {
return [
{
message: '{{property|gravis|append-property}}is not falsy',
message: '#{{print("property")}}is not falsy',
},
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/functions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const schema: ISchemaFunction = (targetVal, opts, paths, { rule }) => {
return [
{
path,
message: `{{property|gravis|append-property}}does not exist`,
message: `#{{print("property")}}does not exist`,
},
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/functions/truthy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const truthy: IFunction = (targetVal): void | IFunctionResult[] => {
if (!targetVal) {
return [
{
message: '{{property|gravis|append-property}}is not truthy',
message: '#{{print("property")}}is not truthy',
},
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/functions/undefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const undefined: IFunction = (targetVal): void | IFunctionResult[] => {
if (typeof targetVal !== 'undefined') {
return [
{
message: '{{property|gravis|append-property}}should be undefined',
message: '#{{print("property")}}should be undefined',
},
];
}
Expand Down
15 changes: 15 additions & 0 deletions src/rulesets/__tests__/message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ describe('message util', () => {
).toEqual('oops... "description" is missing;error: expected property to be truthy');
});

test('evaluates code', () => {
const template = 'Property "#{{value.param}}" is missing. Path: #{{path.toUpperCase()}}';
expect(
message(template, {
property: 'description',
error: 'expected property to be truthy',
path: 'test',
description: null,
value: {
param: 'test',
},
}),
).toEqual('Property "test" is missing. Path: TEST');
});

test.each([0, false, null, undefined])('interpolates %s value correctly', value => {
const template = 'Value must not equal {{value}}';
expect(
Expand Down
39 changes: 23 additions & 16 deletions src/rulesets/message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Segment } from '@stoplight/types';
import { capitalize, isObject } from 'lodash';
import { isObject } from 'lodash';
import { Replacer } from '../utils/replacer';

export interface IMessageVars {
Expand All @@ -14,22 +14,29 @@ export type MessageInterpolator = (str: string, values: IMessageVars) => string;

const MessageReplacer = new Replacer<IMessageVars>(2);

MessageReplacer.addTransformer('double-quotes', (id, value) => (value ? `"${value}"` : ''));
MessageReplacer.addTransformer('single-quotes', (id, value) => (value ? `'${value}'` : ''));
MessageReplacer.addTransformer('gravis', (id, value) => (value ? `\`${value}\`` : ''));
MessageReplacer.addTransformer('capitalize', (id, value) => capitalize(String(value)));

MessageReplacer.addTransformer('append-property', (id, value) => (value ? `${value} property ` : ''));
MessageReplacer.addTransformer('optional-typeof', (id, value, values) =>
value ? String(value) : `${typeof values.value} `,
);

MessageReplacer.addTransformer('to-string', (id, value) => {
if (isObject(value)) {
return Array.isArray(value) ? 'Array[]' : 'Object{}';
MessageReplacer.addFunction('print', function (type) {
if (typeof type !== 'string') return '';
const { property, value } = this;
switch (type) {
case 'property':
if (property !== void 0) {
return `\`${property}\` property `;
}

return '';
case 'value':
if (isObject(value)) {
return Array.isArray(value) ? 'Array[]' : 'Object{}';
}

return JSON.stringify(value);
default:
if (type in this && this[type] !== null) {
return String(this[type]);
}

return '';
}

return JSON.stringify(value);
});

export const message: MessageInterpolator = MessageReplacer.print.bind(MessageReplacer);
41 changes: 38 additions & 3 deletions src/utils/__tests__/replacer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Dictionary } from '@stoplight/types';
import { Replacer } from '../replacer';

describe('Replacer', () => {
afterEach(() => {
jest.restoreAllMocks();
});

it('interpolates correctly', () => {
const replacer = new Replacer<Dictionary<unknown>>(2);
const template = 'oops... "{{property}}" is missing;error: {{error}}';
Expand Down Expand Up @@ -53,16 +57,47 @@ describe('Replacer', () => {
).toEqual('missing :(');
});

it('supports transformers', () => {
it('evaluates expressions', () => {
const replacer = new Replacer<Dictionary<unknown>>(2);
replacer.addTransformer('dot', (id, value) => (Array.isArray(value) ? value.join('.') : String(value)));

const template = '{{path|dot}}';
const template = "#{{path[1] + '-' + path[2].toUpperCase()}}";

expect(
replacer.print(template, {
path: ['foo', 'bar', '/a'],
}),
).toEqual('bar-/A');
});

it('supports custom functions', () => {
const replacer = new Replacer<Dictionary<unknown>>(2);
replacer.addFunction('printPath', function () {
const { path } = this;
return Array.isArray(path) ? path.join('.') : String(path);
});

const template = '#{{printPath()}}';

expect(
replacer.print(template, {
path: ['foo', 'bar', '/a'],
}),
).toEqual('foo.bar./a');
});

it('handles and prints out exceptions thrown during evaluation', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('handles and prints out exceptions thrown during evaluation', () => {
it('ignores and prints out exceptions thrown during evaluation', () => {

@nulltoken looks good? 😁

const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {
// no-op
});

const replacer = new Replacer<Dictionary<unknown>>(2);
const template = 'value is: #{{value.name}}';

expect(
replacer.print(template, {
value: null,
}),
).toEqual('value is: ');
expect(warnSpy).toBeCalledWith(new TypeError("Cannot read property 'name' of null"));
});
});
42 changes: 27 additions & 15 deletions src/utils/replacer.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
import { Dictionary } from '@stoplight/types';
import { eval, parse } from 'expression-eval';

export type Transformer<V = unknown, VV = object> = (identifier: string, value: V, values: VV) => string;
export type Transformer<V = object> = (this: V, ...args: unknown[]) => string;

export class Replacer<V extends object> {
protected readonly regex: RegExp;
protected readonly transformers: Dictionary<Transformer<V[keyof V], V>>;
protected readonly functions: Dictionary<Transformer<V>>;

constructor(count: number) {
this.regex = new RegExp(`${'{'.repeat(count)}([^}\n]+)${'}'.repeat(count)}`, 'g');
this.regex = new RegExp(`#?${'{'.repeat(count)}([^}\n]+)${'}'.repeat(count)}`, 'g');

this.transformers = {};
this.functions = {};
}

public addTransformer(name: string, filter: Transformer<V[keyof V], V>) {
this.transformers[name] = filter;
public addFunction(name: string, filter: Transformer<V>): void {
this.functions[name] = filter;
}

public print(input: string, values: V) {
return input.replace(this.regex, (substr, expr) => {
const [identifier, ...transformers] = expr.split('|');
public print(input: string, values: V): string {
return input.replace(this.regex, (substr, identifier, index) => {
const shouldEvaluate = input[index] === '#';

if (shouldEvaluate) {
try {
return String(
eval(parse(identifier), {
...Object.entries(this.functions).reduce((fns, [name, fn]) => {
fns[name] = fn.bind(values);
return fns;
}, {}),
...values,
}),
);
} catch (ex) {
console.warn(ex);
return '';
nulltoken marked this conversation as resolved.
Show resolved Hide resolved
}
}

if (!(identifier in values)) {
return '';
}

for (const transformer of transformers) {
if (transformer in this.transformers) {
values[identifier] = this.transformers[transformer](identifier, values[identifier], values);
}
}

return String(values[identifier]);
});
}
Expand Down
15 changes: 11 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2766,6 +2766,13 @@ export-files@^2.0.1:
dependencies:
lazy-cache "^1.0.3"

expression-eval@3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/expression-eval/-/expression-eval-3.1.2.tgz#45ddf0b8fdb8bf0633d02f4061d6f25516eddb30"
integrity sha512-c8ZN8fuAz0TRYKoGsrIq5kLNHtm81KAqWSBORHIY0DpJmZZrwK/r2zFDOhFIAJDV47gJ6irV7dWf1TOFpKvULQ==
dependencies:
jsep "^0.3.0"

extend-shallow@^2.0.0, extend-shallow@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-2.0.1.tgz#51af7d614ad9a9f610ea1bafbb989d6b1c56890f"
Expand Down Expand Up @@ -4511,10 +4518,10 @@ jsdom@^15.2.1:
ws "^7.0.0"
xml-name-validator "^3.0.0"

jsep@^0.3.4:
version "0.3.4"
resolved "https://registry.yarnpkg.com/jsep/-/jsep-0.3.4.tgz#55ebd4400c5c5861cb1ff949a7a4cd97fcaacaa0"
integrity sha512-ovGD9wE+wvudIIYxZGrRcZCxNyZ3Cl1N7Bzyp7/j4d/tA0BaUwcVM9bu0oZaSrefMiNwv6TwZ9X15gvZosteCQ==
jsep@^0.3.0, jsep@^0.3.4:
version "0.3.5"
resolved "https://registry.yarnpkg.com/jsep/-/jsep-0.3.5.tgz#3fd79ebd92f6f434e4857d5272aaeef7d948264d"
integrity sha512-AoRLBDc6JNnKjNcmonituEABS5bcfqDhQAWWXNTFrqu6nVXBpBAGfcoTGZMFlIrh9FjmE1CQyX9CTNwZrXMMDA==

jsesc@^2.5.1:
version "2.5.2"
Expand Down