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

fix: more sane errors for unclosed flow collections #52

Merged
merged 1 commit into from
Apr 21, 2021
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
},
"dependencies": {
"@stoplight/ordered-object-literal": "^1.0.1",
"@stoplight/types": "^11.9.0",
"@stoplight/types": "^12.0.0",
"@stoplight/yaml-ast-parser": "0.0.48",
"tslib": "^1.12.0"
"tslib": "^2.2.0"
},
"devDependencies": {
"@stoplight/scripts": "3.1.0",
Expand Down
84 changes: 83 additions & 1 deletion src/__tests__/parseWithPointers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('yaml parser', () => {
expect(result.data).toEqual(HugeJSON);
});

it('parses string according to YAML 1.2 spec', () => {
test('parses string according to YAML 1.2 spec', () => {
const { data } = parseWithPointers(spectral481);
expect(data).toHaveProperty(
'components.schemas.RandomRequest.properties.implicit_string_date.example',
Expand Down Expand Up @@ -136,6 +136,88 @@ prop2: true
},
]);
});

test('unclosed flow sequence', () => {
const result = parseWithPointers(`austrian-cities: [
- Vienna
- Graz
- Linz
- Salzburg
`);
expect(result.diagnostics).toEqual([
{
severity: DiagnosticSeverity.Error,
message: 'invalid mixed usage of block and flow styles',
code: 'YAMLException',
range: {
start: {
character: 0,
line: 1,
},
end: {
character: 0,
line: 5,
},
},
},
{
severity: DiagnosticSeverity.Error,
message: 'unexpected end of the stream within a flow collection',
code: 'YAMLException',
range: {
start: {
character: 0,
line: 5,
},
end: {
character: 0,
line: 5,
},
},
},
]);
});

test('unclosed flow mapping', () => {
const result = parseWithPointers(`austrian-cities: {
- Vienna
- Graz
- Linz
- Salzburg
`);
expect(result.diagnostics).toEqual([
{
severity: DiagnosticSeverity.Error,
message: 'invalid mixed usage of block and flow styles',
code: 'YAMLException',
range: {
start: {
character: 0,
line: 1,
},
end: {
character: 0,
line: 5,
},
},
},
{
severity: DiagnosticSeverity.Error,
message: 'unexpected end of the stream within a flow collection',
code: 'YAMLException',
range: {
start: {
character: 0,
line: 5,
},
end: {
character: 0,
line: 5,
},
},
},
]);
});
});

describe('dereferencing anchor refs', () => {
Expand Down
17 changes: 16 additions & 1 deletion src/parseWithPointers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import createOrderedObject, { getOrder } from '@stoplight/ordered-object-literal';
import { DiagnosticSeverity, Dictionary, IDiagnostic, Optional } from '@stoplight/types';
import { DiagnosticSeverity, Dictionary, IDiagnostic, IPosition, IRange, Optional } from '@stoplight/types';
import {
determineScalarType,
load as loadAST,
Expand Down Expand Up @@ -164,6 +164,9 @@ function getLineLength(lineMap: number[], line: number) {

const transformErrors = (errors: YAMLException[], lineMap: number[]): IDiagnostic[] => {
const validations: IDiagnostic[] = [];
let possiblyUnexpectedFlow = -1;
let i = 0;

for (const error of errors) {
const validation: IDiagnostic = {
code: error.name,
Expand All @@ -181,7 +184,19 @@ const transformErrors = (errors: YAMLException[], lineMap: number[]): IDiagnosti
},
};

const isBrokenFlow = error.reason === 'missed comma between flow collection entries';
if (isBrokenFlow) {
possiblyUnexpectedFlow = possiblyUnexpectedFlow === -1 ? i : possiblyUnexpectedFlow;
} else if (possiblyUnexpectedFlow !== -1) {
(validations[possiblyUnexpectedFlow].range as Dictionary<IPosition, keyof IRange>).end = validation.range.end;
validations[possiblyUnexpectedFlow].message = 'invalid mixed usage of block and flow styles';
validations.length = possiblyUnexpectedFlow + 1;
i = validations.length;
possiblyUnexpectedFlow = -1;
}

validations.push(validation);
i++;
}

return validations;
Expand Down
15 changes: 10 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1580,10 +1580,10 @@
typedoc "0.13.x"
typescript-plugin-styled-components "1.0.x"

"@stoplight/types@^11.9.0":
version "11.9.0"
resolved "https://registry.yarnpkg.com/@stoplight/types/-/types-11.9.0.tgz#ced7de2dd53439d2409a3cb390bf7d5b76382ac6"
integrity sha512-4bzPpWZobt0e+d0OtALCJyl1HGzKo6ur21qxnId9dTl8v3yeD+5HJKZ2h1mv7e94debH5QDtimMU80V6jbXM8Q==
"@stoplight/types@^12.0.0":
version "12.0.0"
resolved "https://registry.yarnpkg.com/@stoplight/types/-/types-12.0.0.tgz#3fc68ffa954e7c0a0e6261a982d5c0cb52d5d969"
integrity sha512-BbL0MIbcCwrU4RidcVyMKnRwACnhr7WAEgPH8d6rwhg/j2DegdLadnXCnq8UJXUWW9Apbntgvic3yVGvLtVmxA==
dependencies:
"@types/json-schema" "^7.0.4"
utility-types "^3.10.0"
Expand Down Expand Up @@ -12637,11 +12637,16 @@ [email protected]:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286"
integrity sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ==

tslib@^1.12.0, tslib@^1.7.1, tslib@^1.8.0, tslib@^1.8.1, tslib@^1.9.0, tslib@^1.9.3:
tslib@^1.7.1, tslib@^1.8.0, tslib@^1.8.1, tslib@^1.9.0, tslib@^1.9.3:
version "1.12.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.12.0.tgz#d1fc9cacd06a1456c62f2902b361573e83d66473"
integrity sha512-5rxCQkP0kytf4H1T4xz1imjxaUUPMvc5aWp0rJ/VMIN7ClRiH1FwFvBt8wOeMasp/epeUnmSW6CixSIePtiLqA==

tslib@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.2.0.tgz#fb2c475977e35e241311ede2693cee1ec6698f5c"
integrity sha512-gS9GVHRU+RGn5KQM2rllAlR3dU6m7AcpJKdtH8gFvQiC4Otgk98XnmMU+nZenHt/+VhnBPWwgrJsyrdcw6i23w==

[email protected]:
version "1.15.0"
resolved "https://registry.yarnpkg.com/tslint-config-prettier/-/tslint-config-prettier-1.15.0.tgz#76b9714399004ab6831fdcf76d89b73691c812cf"
Expand Down