-
-
Notifications
You must be signed in to change notification settings - Fork 248
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: format lookup by source #1348
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
source
is a bit redundant - we have this info stored on IDocument
already.
Other than this, looks good.
src/spectral.ts
Outdated
@@ -111,7 +111,7 @@ export class Spectral { | |||
|
|||
if (document.formats === void 0) { | |||
const registeredFormats = Object.keys(this.formats); | |||
const foundFormats = registeredFormats.filter(format => this.formats[format](inventory.resolved)); | |||
const foundFormats = registeredFormats.filter(format => this.formats[format](inventory.resolved, opts.source)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const foundFormats = registeredFormats.filter(format => this.formats[format](inventory.resolved, opts.source)); | |
const foundFormats = registeredFormats.filter(format => this.formats[format](inventory.resolved, document.source)); |
src/types/spectral.ts
Outdated
@@ -31,6 +31,7 @@ export interface IRunOpts { | |||
resolve?: { | |||
documentUri?: string; | |||
}; | |||
source?: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
source?: string; |
@@ -53,7 +54,7 @@ export interface IResolver { | |||
resolve(source: unknown, opts?: IResolveOpts): Promise<ResolveResult>; | |||
} | |||
|
|||
export type FormatLookup = (document: unknown) => boolean; | |||
export type FormatLookup = (document: unknown, source?: string) => boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export type FormatLookup = (document: unknown, source?: string) => boolean; | |
export type FormatLookup = (document: unknown, source: string | null) => boolean; |
document.source
is either null or string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would make it a breaking change, wouldn't it?
I'd keep it this way and just do this.formats[format](inventory.resolved, document.source ?? void 0)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aye, that'd be indeed a breaking change.
Sounds good. Let's do it this way. Perhaps leave a comment for me so that I revisit it when we release a major version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export type FormatLookup = (document: unknown, source?: string) => boolean; | |
// todo(@p0lip): make it string | null when working on 6.0 | |
export type FormatLookup = (document: unknown, source?: string) => boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@P0lip Just out of curiosity, why favoring null
when it can already be undefined
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cause document.source is either a null or string, so we won't need to do ?? void 0
thing.
docs/guides/3-javascript.md
Outdated
{ | ||
'foo-bar': true, | ||
x: false | ||
}, | ||
{ | ||
source: '/foo/bar', | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{ | |
'foo-bar': true, | |
x: false | |
}, | |
{ | |
source: '/foo/bar', | |
} | |
new Document(`foo-bar: true\nx: false`, Parsers.Yaml, '/foo/bar'), |
make sure to test it and format correctly. I didn't 😆
docs/guides/3-javascript.md
Outdated
Alternatively you may lookup for certain format by optional `source`, which could be passed in `run` options. | ||
|
||
```js | ||
const { Spectral } = require('@stoplight/spectral'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const { Spectral } = require('@stoplight/spectral'); | |
const { Spectral, Document } = require('@stoplight/spectral'); |
src/__tests__/linter.test.ts
Outdated
}); | ||
|
||
const result = await spectral.run( | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above
* feat: format lookup by source * chore: update docs * chore: use source from document * chore: todo comment
Adds support for looking up formats by source (filename, uri) additionally to document content.
Checklist
Does this PR introduce a breaking change?