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

Allow specs to be represented by a simple string #3

Merged
merged 4 commits into from
Jan 31, 2020

Conversation

tidoust
Copy link
Member

@tidoust tidoust commented Jan 21, 2020

In most cases, a URL should be all we need in that list. Getting rid of the wrapping { "url": ... } struct in these cases allows to keep the list more human readable.

This update allows entries to be just a string. Linting code updated to prefer the string format when there is only a URL (which is going to be the case as long as we don't create additional properties). Specifications remain sorted by alphabetic order of their URL.

The index.js file continues to export an array of objects with a url property to ease ingestion by external tools.

See discussion in:
#2 (review)

(Note that if we actually discover later on that a URL is not enough in most cases, we may want to undo this change...)

In most cases, a URL should be all we need in that list. Getting rid of the
wrapping `{ "url": ... }` struct in these cases allows to keep the list more
human readable.

This update allows entries to be just a string. Linting code updated to prefer
the string format when there is only a URL (which is going to be the case as
long as we don't create additional properties). Specifications remain sorted
by alphabetic order of their URL.

The `index.js` file continues to export an array of objects with a `url`
property to ease ingestion by external tools.

See discussion in:
w3c#2 (review)

(Note that if we actually discover later on that a URL is **not** enough in
most cases, we may want to undo this change...)
index.js Outdated Show resolved Hide resolved
lint.js Outdated Show resolved Hide resolved
lint.js Outdated

const sorted = specs
.map(spec => isString(spec) ? { url: spec } : spec)
.map(spec => Object.assign({}, spec, { url: new URL(spec.url).toString() }));
Copy link
Member

Choose a reason for hiding this comment

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

This allows for arbitrary additional properties, most likely in the form of typos if we add more properties. I'd prefer this code to normalize everything that is known and drop anything else. Since currently no additional properties are allowed, just require all entries to be a string.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was assuming that we would in any case add some kind of JSON schema to validate the structure before we pass it to the linter. I'll add some code logic to that effect.

Copy link
Member

Choose a reason for hiding this comment

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

I started out using ajv myself, but ripped it out since the schema was so trivial. Now when either a string or object are allowed it's less trivial, so I'm happy with using it.

I'll have a few days off for the lunar new year, but will review this when I'm back next week!

Copy link
Member Author

Choose a reason for hiding this comment

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

I just added a JSON Schema and schema validation. The schema is a bit dumb for now since it allows an object with a URL, but then that's useless as long as we don't have additional properties, especially since the linting process will convert objects to URL strings. At least we'll have the right skeleton and logic in place for when we add properties.

Also added a method to slightly improve error messages returned by the JSON
Schema validation to make them more human-friendly.
Make sure that the linting process accepts correct lists of specs, lints lists
of specs that can be linted, and throws when something is wrong.

Tests done with mocha. Run `npm test` to run the tests.
@tidoust
Copy link
Member Author

tidoust commented Jan 24, 2020

Note I added schema validation and created tests for the linter.

let specs = JSON.parse(specsBuffer);
// When an entry is invalid, the schema validator returns one error for each
// "oneOf" option and one error on overall "oneOf" problem. This is confusing
// for humans. the following function improves the error being returned.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// for humans. the following function improves the error being returned.
// for humans. The following function improves the error being returned.

// "oneOf" option and one error on overall "oneOf" problem. This is confusing
// for humans. the following function improves the error being returned.
const clarifyErrors = errors => {
if (!errors || (errors.length < 2)) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (!errors || (errors.length < 2)) {
if (!errors || errors.length < 2) {

}

// Otherwise, if second error is a type error for second oneOf choice,
// it means the itme is actually a string that represents an invalid URL,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// it means the itme is actually a string that represents an invalid URL,
// it means the item is actually a string that represents an invalid URL,


// Otherwise, if second error is a type error for second oneOf choice,
// it means the itme is actually a string that represents an invalid URL,
// which the first error should capture
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// which the first error should capture
// which the first error should capture.

{
"url": "https://compat.spec.whatwg.org/"
}
"http://compat.spec.whatwg.org/"
Copy link
Member

Choose a reason for hiding this comment

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

Should be http://compat.spec.whatwg.org/. I'll extend the lint to require this.

Copy link
Member

Choose a reason for hiding this comment

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

should be https://compat.spec.whatwg.org/ rather, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, I missed the s. I guess it shows it's a good idea to add HTTPS enforcement to the linter...

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