Skip to content

Commit

Permalink
chore: merge printValue & printProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Sep 18, 2020
1 parent b4f2624 commit 9b2b9be
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 31 deletions.
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 #{{printValue()}} 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 @@ -981,7 +981,7 @@ responses:: !!foo
severity: DiagnosticSeverity.Error,
recommended: true,
description: 'A parameter in the header should be written in kebab-case',
message: '#{{printValue()}} is not kebab-cased: {{error}}',
message: '#{{print("value")}} is not kebab-cased: {{error}}',
given: "$..parameters[?(@.in === 'header')]",
then: {
field: 'name',
Expand Down Expand Up @@ -1032,7 +1032,7 @@ responses:: !!foo
severity: DiagnosticSeverity.Error,
recommended: true,
description: 'Should be falsy',
message: 'Value #{{printValue()}} 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: '#{{printProperty()}}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: '#{{printProperty()}}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: '#{{printProperty()}}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: '#{{printProperty()}}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: '#{{printProperty()}}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: '#{{printProperty()}}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: '#{{printProperty()}}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: `#{{printProperty()}}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: '#{{printProperty()}}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: '#{{printProperty()}}should be undefined',
message: '#{{print("property")}}should be undefined',
},
];
}
Expand Down
35 changes: 22 additions & 13 deletions src/rulesets/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,29 @@ export type MessageInterpolator = (str: string, values: IMessageVars) => string;

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

MessageReplacer.addFunction('printProperty', ({ property }) => {
if (property !== void 0) {
return `\`${property}\` property `;
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 '';
});

MessageReplacer.addFunction('printValue', ({ value }) => {
if (isObject(value)) {
return Array.isArray(value) ? 'Array[]' : 'Object{}';
}

return JSON.stringify(value);
});

export const message: MessageInterpolator = MessageReplacer.print.bind(MessageReplacer);
4 changes: 2 additions & 2 deletions src/rulesets/reader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Cache } from '@stoplight/json-ref-resolver';
import { ICache } from '@stoplight/json-ref-resolver/types';
import { extname, join, toFSPath } from '@stoplight/path';
import { extname, join } from '@stoplight/path';
import { Optional } from '@stoplight/types';
import { readFile, readParsable } from '../fs/reader';
import { createHttpAndFileResolver, IHttpAndFileResolverOptions } from '../resolvers/http-and-file';
Expand Down Expand Up @@ -79,7 +79,7 @@ const createRulesetProcessor = (
dereferenceInline: false,
uriCache,
async parseResolveResult(opts) {
opts.result = parseContent(opts.result, toFSPath(opts.targetAuthority.pathname()));
opts.result = parseContent(opts.result, opts.targetAuthority.pathname());
return opts;
},
},
Expand Down
5 changes: 4 additions & 1 deletion src/utils/__tests__/replacer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ describe('Replacer', () => {

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/utils/replacer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Dictionary } from '@stoplight/types';
import { eval, parse } from 'expression-eval';

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

export class Replacer<V extends object> {
protected readonly regex: RegExp;
Expand All @@ -25,7 +25,7 @@ export class Replacer<V extends object> {
return String(
eval(parse(identifier), {
...Object.entries(this.functions).reduce((fns, [name, fn]) => {
fns[name] = fn.bind(null, values);
fns[name] = fn.bind(values);
return fns;
}, {}),
...values,
Expand Down

0 comments on commit 9b2b9be

Please sign in to comment.