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

feat: format lookup by source #1348

Merged
merged 4 commits into from
Sep 23, 2020
Merged

feat: format lookup by source #1348

merged 4 commits into from
Sep 23, 2020

Conversation

mallachari
Copy link

@mallachari mallachari commented Sep 22, 2020

Adds support for looking up formats by source (filename, uri) additionally to document content.

Checklist

  • Tests added / updated
  • Docs added / updated

Does this PR introduce a breaking change?

  • Yes
  • No

@mallachari mallachari requested a review from P0lip September 22, 2020 18:19
@mallachari mallachari self-assigned this Sep 22, 2020
Copy link
Contributor

@P0lip P0lip left a 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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const foundFormats = registeredFormats.filter(format => this.formats[format](inventory.resolved, opts.source));
const foundFormats = registeredFormats.filter(format => this.formats[format](inventory.resolved, document.source));

@@ -31,6 +31,7 @@ export interface IRunOpts {
resolve?: {
documentUri?: string;
};
source?: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;
Copy link
Contributor

@P0lip P0lip Sep 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export type FormatLookup = (document: unknown, source?: string) => boolean;
export type FormatLookup = (document: unknown, source: string | null) => boolean;

document.source is either null or string.

Copy link
Author

@mallachari mallachari Sep 22, 2020

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)

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;

Copy link
Contributor

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?

Copy link
Contributor

@P0lip P0lip Sep 23, 2020

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.

Comment on lines 220 to 226
{
'foo-bar': true,
x: false
},
{
source: '/foo/bar',
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{
'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 😆

Alternatively you may lookup for certain format by optional `source`, which could be passed in `run` options.

```js
const { Spectral } = require('@stoplight/spectral');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const { Spectral } = require('@stoplight/spectral');
const { Spectral, Document } = require('@stoplight/spectral');

});

const result = await spectral.run(
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above

@mallachari mallachari requested a review from P0lip September 22, 2020 23:42
P0lip
P0lip previously approved these changes Sep 22, 2020
@P0lip P0lip merged commit 9c8cc2c into develop Sep 23, 2020
@P0lip P0lip deleted the feat/source-format-lookup branch September 23, 2020 01:05
@nulltoken nulltoken mentioned this pull request Sep 26, 2020
4 tasks
P0lip pushed a commit that referenced this pull request Sep 28, 2020
* feat: format lookup by source

* chore: update docs

* chore: use source from document

* chore: todo comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants