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

style(spec): add out-of-line-enum rule APIC-416 #356

Merged
merged 7 commits into from
Apr 12, 2022
Merged

Conversation

millotp
Copy link
Collaborator

@millotp millotp commented Apr 8, 2022

🧭 What and Why

🎟 JIRA Ticket: APIC-416

Add a rule to enforce out of line enum, except in servers section (not required here).
Also disable this rule for the bundled specs, as some refs are deleted.

Also include the simple rule ref-single-quote that enforce single quote on all $ref

Changes included:

  • Add out-of-line-enum rule
  • Add ref-single-quote rule
  • add tests
  • Lint common specs with cache

#357 contains the modification to the specs

🧪 Test

yarn docker build specs

@millotp millotp self-assigned this Apr 8, 2022
@netlify
Copy link

netlify bot commented Apr 8, 2022

Deploy Preview for api-clients-automation ready!

Name Link
🔨 Latest commit b2a79a7
🔍 Latest deploy log https://app.netlify.com/sites/api-clients-automation/deploys/62556156b50d94000836bc10
😎 Deploy Preview https://deploy-preview-356--api-clients-automation.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@algolia-bot
Copy link
Collaborator

algolia-bot commented Apr 8, 2022

✗ The generated branch has been deleted.

If the PR has been merged, you can check the generated code on the main branch.

@millotp millotp requested review from a team, damcou and shortcuts and removed request for a team April 8, 2022 14:10
@millotp millotp force-pushed the style/eslint-enum branch from 87040bd to 9d77035 Compare April 8, 2022 15:14
@millotp millotp force-pushed the style/eslint-enum branch from 9d77035 to 2d36490 Compare April 12, 2022 07:53
Copy link
Contributor

@damcou damcou left a comment

Choose a reason for hiding this comment

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

Looks very good, I tested it locally and it seems to work fine !

}
if (
isPairWithKey(
node.parent.parent.parent.parent?.parent?.parent?.parent ?? null,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any way to prevent that kind of path ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it's a very specific edge case that we want to solve here, it won't be used anywhere else but if we need to we can have a function that checks N parents above, I will add it if I ever encounter a parent chain I will add it !

yield fixer.replaceTextRange([end - 1, end], "'");
} else {
yield fixer.insertTextBeforeRange([start, start], "'");
yield fixer.insertTextAfterRange([end, end], "'");
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd check if the last character is not ' , otherwise it will escape the last you add ;) . (in the case it doesn't start with a quote but it ends with it)

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if I saw it elsewhere but we also need to make sure the string does not contain a ', if so we should fallback to double quoted

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

pretty sure " and ' are forbidden characters for path and yaml key, this is not a concern for this particular rule.

Copy link
Member

Choose a reason for hiding this comment

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

Ah yes this is only for refs sorry I forgot I was thinking of description/summaries

Copy link
Member

@shortcuts shortcuts left a comment

Choose a reason for hiding this comment

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

:very_nice:!!!

Comment on lines 38 to 45
yarn specs:lint specs/ --fix
```

If you just want to check the format (not override the files), run:

```bash
yarn specs:lint <client>
yarn specs:lint search
yarn specs:lint specs/<client> --fix
yarn specs:lint specs/search --fix
Copy link
Member

Choose a reason for hiding this comment

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

The README does not mention the --fix, is that intended? Also, does it make sense to keep docs in a README since we have the same on the website?

package.json Outdated
@@ -25,8 +25,7 @@
"release": "yarn workspace scripts createReleaseIssue",
"scripts:lint": "eslint --ext=ts scripts/",
"scripts:test": "yarn workspace scripts test",
"specs:fix": "eslint --ext=yml specs/$0 --fix",
"specs:lint": "eslint --ext=yml specs/$0",
"specs:lint": "eslint --ext=yml",
Copy link
Member

Choose a reason for hiding this comment

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

much cleaner

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think those comments were made before the last commit, do you me to get back to only one specs:lint script ?

Copy link
Member

Choose a reason for hiding this comment

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

Ah true, I liked the idea so it's closer to the base eslint usage, but that's just my opinion.

}
if (
isPairWithKey(
node.parent.parent.parent.parent?.parent?.parent?.parent ?? null,
Copy link
Member

Choose a reason for hiding this comment

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

We can maybe have a function that will find the deepest parent in a given element, so it handles even deeper ones etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here it's not necessarily the deepest, it's just that's the 7th parent has be to a pair with key servers, not sure if it generalizable.

Copy link
Member

Choose a reason for hiding this comment

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

Yes but there should only be one servers in a spec so we can maybe assume that the deepest or first found is correct?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

we could, but that won't make the code simpler so I don't see why change

Copy link
Member

Choose a reason for hiding this comment

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

it was more to make sure if it's the 6th or 9th it won't require an other manual check, it's not necessary to change it right now, we can iterate on it if there's an issue one day

return;
}
if (!isScalar(node.value)) {
// not our problem
Copy link
Member

Choose a reason for hiding this comment

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

can you elaborate why it's not?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If the ref is not a scalar (not a string) then something else will fail, like the path resolution of redocly so we don't need to check here, I will comment.

Copy link
Member

Choose a reason for hiding this comment

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

Yes I meant elaborate the comment, I get the reason :D

return;
}
if (isBLockScalar(node.value)) {
// what to do here ?
Copy link
Member

Choose a reason for hiding this comment

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

Nothing

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

well it could maybe work with block scalar I haven't tried it, but it should be forbidden by another rule because it doesn't make sense.

Copy link
Member

Choose a reason for hiding this comment

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

yep definitely, I guess blocks can be as is it's fine

const hasDoubleQuote = node.value.style === 'double-quoted';
const [start, end] = node.value.range;
context.report({
node: node as any,
Copy link
Member

Choose a reason for hiding this comment

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

how come we need to cast 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is an issue between eslint type Node and yaml-eslint-parser type that inherits from the wrong Node, so type are not compatible but in practice they have the same properties.

yield fixer.replaceTextRange([end - 1, end], "'");
} else {
yield fixer.insertTextBeforeRange([start, start], "'");
yield fixer.insertTextAfterRange([end, end], "'");
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if I saw it elsewhere but we also need to make sure the string does not contain a ', if so we should fallback to double quoted

Comment on lines +66 to +67
const spinner = createSpinner('linting common spec', verbose).start();
await run(`yarn specs:lint common`, { verbose });
Copy link
Member

Choose a reason for hiding this comment

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

don't we want to fix when it's not the CI?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not a fan of scripts modifying hand written file when it's unclear, but here it should be fine

Copy link
Member

@shortcuts shortcuts Apr 12, 2022

Choose a reason for hiding this comment

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

Make sense but if it's only for dev purposes it's fine I guess

(as you want)

Copy link
Member

@shortcuts shortcuts left a comment

Choose a reason for hiding this comment

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

just so we remove bundled specs from the PR

@@ -53,7 +53,7 @@ components:
period to analyze.
Copy link
Member

Choose a reason for hiding this comment

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

bundled specs changes are pushed by the CI

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah yes sorry I forgot

@millotp millotp requested a review from shortcuts April 12, 2022 11:25
Copy link
Member

@shortcuts shortcuts left a comment

Choose a reason for hiding this comment

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

Pretty solid! Good addition ✍🏼

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.

4 participants