-
-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): do not return duplicate results (#681)
- Loading branch information
Showing
6 changed files
with
168 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/cli/services/linter/utils/__tests__/__fixtures__/duplicate-validation-results.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
[ | ||
{ | ||
"code": "valid-schema-example-in-content", | ||
"message": "\"schema.example\" property type should be integer", | ||
"path": [ | ||
"paths", | ||
"/resource", | ||
"get", | ||
"responses", | ||
"200", | ||
"content", | ||
"application/json", | ||
"schema", | ||
"example" | ||
], | ||
"severity": 0, | ||
"source": "/home/Spectral/test-harness/scenarios/documents/invalid-schema-example/lib.yaml", | ||
"range": { | ||
"start": { | ||
"line": 20, | ||
"character": 15 | ||
}, | ||
"end": { | ||
"line": 20, | ||
"character": 19 | ||
} | ||
} | ||
}, | ||
{ | ||
"code": "valid-schema-example-in-content", | ||
"message": "\"schema.example\" property type should be integer", | ||
"path": [ | ||
"paths", | ||
"/resource", | ||
"get", | ||
"responses", | ||
"200", | ||
"content", | ||
"application/json", | ||
"schema", | ||
"example" | ||
], | ||
"severity": 0, | ||
"source": "/home/Spectral/test-harness/scenarios/documents/invalid-schema-example/lib.yaml", | ||
"range": { | ||
"start": { | ||
"line": 20, | ||
"character": 15 | ||
}, | ||
"end": { | ||
"line": 20, | ||
"character": 19 | ||
} | ||
} | ||
}, | ||
{ | ||
"code": "valid-schema-example-in-content", | ||
"message": "\"schema.example\" property type should be integer", | ||
"path": [ | ||
"paths", | ||
"/resource", | ||
"get", | ||
"responses", | ||
"200", | ||
"content", | ||
"application/json", | ||
"schema", | ||
"example" | ||
], | ||
"severity": 0, | ||
"source": "/home/Spectral/test-harness/scenarios/documents/invalid-schema-example/lib.yaml", | ||
"range": { | ||
"start": { | ||
"line": 20, | ||
"character": 15 | ||
}, | ||
"end": { | ||
"line": 20, | ||
"character": 19 | ||
} | ||
} | ||
}, | ||
{ | ||
"code": "valid-example-in-schemas", | ||
"message": "\"Test.example\" property type should be integer", | ||
"path": [ | ||
"components", | ||
"schemas", | ||
"Test", | ||
"example" | ||
], | ||
"severity": 0, | ||
"source": "/home/Spectral/test-harness/scenarios/documents/invalid-schema-example/lib.yaml", | ||
"range": { | ||
"start": { | ||
"line": 20, | ||
"character": 15 | ||
}, | ||
"end": { | ||
"line": 20, | ||
"character": 19 | ||
} | ||
} | ||
} | ||
] |
32 changes: 32 additions & 0 deletions
32
src/cli/services/linter/utils/__tests__/deduplicateResults.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { deduplicateResults } from '../deduplicateResults'; | ||
|
||
import * as duplicateValidationResults from './__fixtures__/duplicate-validation-results.json'; | ||
|
||
describe('deduplicateResults util', () => { | ||
it('deduplicate exact validation results', () => { | ||
expect(deduplicateResults(duplicateValidationResults)).toEqual([ | ||
expect.objectContaining({ | ||
code: 'valid-schema-example-in-content', | ||
}), | ||
expect.objectContaining({ | ||
code: 'valid-example-in-schemas', | ||
}), | ||
]); | ||
}); | ||
|
||
it('deduplicate exact validation results with unknown source', () => { | ||
const duplicateValidationResultsWithNoSource = duplicateValidationResults.map(result => ({ | ||
...result, | ||
source: void 0, | ||
})); | ||
|
||
expect(deduplicateResults(duplicateValidationResultsWithNoSource)).toEqual([ | ||
expect.objectContaining({ | ||
code: 'valid-schema-example-in-content', | ||
}), | ||
expect.objectContaining({ | ||
code: 'valid-example-in-schemas', | ||
}), | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { IRange } from '@stoplight/types/dist'; | ||
import { IRuleResult } from '../../../../types'; | ||
|
||
type Dictionary<T, K extends PropertyKey> = { [key in K]: T }; | ||
|
||
const ARTIFICIAL_ROOT = Symbol('root'); | ||
|
||
const serializeRange = ({ start, end }: IRange) => `${start.line}:${start.character}:${end.line}:${end.character}`; | ||
const getIdentifier = (result: IRuleResult) => `${result.path.join('/')}${result.code}${serializeRange(result.range)}`; | ||
|
||
export const deduplicateResults = (results: IRuleResult[]) => { | ||
const seen: Dictionary<Dictionary<string, string>, symbol> = {}; | ||
|
||
return results.filter(result => { | ||
const source = result.source === void 0 ? ARTIFICIAL_ROOT : result.source; | ||
const identifier = getIdentifier(result); | ||
if (!(source in seen)) { | ||
seen[source] = {}; | ||
} else if (identifier in seen[source]) { | ||
return false; | ||
} | ||
|
||
seen[source][identifier] = true; | ||
return true; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './deduplicateResults'; | ||
export * from './getRuleset'; | ||
export * from './listFiles'; | ||
export * from './skipRules'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters