Skip to content

Commit

Permalink
fix: set proper document source if legacy parsed result is given (#1040)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Mar 30, 2020
1 parent 059d74e commit 9fcf27d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
65 changes: 65 additions & 0 deletions src/__tests__/linter.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Resolver } from '@stoplight/json-ref-resolver';
import { DiagnosticSeverity } from '@stoplight/types';
import { parse } from '@stoplight/yaml';
import { IParsedResult } from '../document';
import { isOpenApiv2, isOpenApiv3 } from '../formats';
import { mergeRules, readRuleset } from '../rulesets';
import { RuleCollection, Spectral } from '../spectral';
Expand Down Expand Up @@ -1131,4 +1132,68 @@ responses:: !!foo
expect(results).toEqual([expect.objectContaining({ code: 'no-info' })]);
});
});

describe('legacy parsed document', () => {
beforeEach(() => {
spectral.setRules({
'falsy-document': {
// some dumb rule to have some error
given: '$',
then: {
function: 'falsy',
},
},
});
});

test('should set parsed.source as the source of document', async () => {
const parsedResult: IParsedResult = {
parsed: {
data: {},
diagnostics: [],
ast: {},
lineMap: [],
},
getLocationForJsonPath: jest.fn(),
source: 'foo',
};

const results = await spectral.run(parsedResult, {
ignoreUnknownFormat: true,
});

expect(results).toEqual([
expect.objectContaining({
code: 'falsy-document',
source: 'foo',
}),
]);
});

test('given missing source on parsedResult, should try to set resolveUri as source of the document', async () => {
const parsedResult: IParsedResult = {
parsed: {
data: {},
diagnostics: [],
ast: {},
lineMap: [],
},
getLocationForJsonPath: jest.fn(),
};

const results = await spectral.run(parsedResult, {
ignoreUnknownFormat: true,
resolve: {
documentUri: 'foo',
},
});

expect(results).toEqual([
expect.objectContaining({
code: 'falsy-document',
source: 'foo',
}),
]);
});
});
});
4 changes: 2 additions & 2 deletions src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ export class ParsedDocument<D = unknown, R extends IParsedResult = IParsedResult
public readonly diagnostics: ReadonlyArray<IRuleResult>;
public formats?: string[] | null;

constructor(protected readonly parserResult: R, source?: string) {
constructor(protected readonly parserResult: R) {
// we need to normalize the path in case path with forward slashes is given
this.source = normalizeSource(source);
this.source = normalizeSource(parserResult.source);
this.diagnostics = formatParserDiagnostics(this.parserResult.parsed.diagnostics, this.source);
}

Expand Down
2 changes: 1 addition & 1 deletion src/spectral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class Spectral {
opts.resolve?.documentUri,
);

if (document.source === void 0 && opts.resolve?.documentUri !== void 0) {
if (document.source === null && opts.resolve?.documentUri !== void 0) {
(document as Omit<Document, 'source'> & { source: string }).source = opts.resolve?.documentUri;
}

Expand Down

0 comments on commit 9fcf27d

Please sign in to comment.