-
-
Notifications
You must be signed in to change notification settings - Fork 687
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
Implement TypeScript type generation for the configuration #1014
Conversation
{ | ||
"files": [ | ||
"schema/*.js" | ||
], | ||
"rules": { | ||
"unicorn/prefer-top-level-await": "off" | ||
} | ||
}, |
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.
Because these are commonjs files, we cannot use top-level imports in them.
/** | ||
* Rule configuration object. | ||
* | ||
* @typedef {boolean | Object} RuleConfiguration Rule configuration. | ||
*/ | ||
|
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.
Since we are no longer using this type as part of the Configuration
, I moved it next to the only type definition that uses it.
Thanks for sharing this! I like what it does for the user and the way the new types are referenced is clean, but I don't think I'd suggest spending time on this before I do, but if you're bored I can say after a first pass that |
Let me know when you think we can move forward with this approach, and I will update the readme and the type checks^^ Regarding the changes in the |
I spent a bit of time this evening and pushed the branch I only need to write a few lines of code to work around a quirk/bug of TypeScript index types. All the type information comes directly from the JSON schema. From what I can tell experimenting quickly in The only thing I see missing vs. what's proposed in this PR is deprecations - but that's part of the 2019-09 JSON specification and may carry over automatically once used/added. |
There are a few key differences with these approaches. JSDoc qualityEven though the generation in this PR is longer, most of the lines are for making the JSDoc comments more readable and neatly organized. For example, here is the The upper one has the description, the most important part first, has a paragraph for the aliases and a link to the rule's documentation. Readability of the
|
JSDoc quality: I agree that what you show is richer. At the same time, the JSON schema is seen by more people and if there are readability improvements to be had there, I’d prefer to make them so everyone benefits. The rule link is a nice touch. Good news is that it seems VS Code automatically linkifies bare URLs in the description field, so I’ve made myself a note to add that to each rule’s description in the JSON schema. Readability of the .d.ts file: This is not a goal in my mind; that file is not something users are expected to read. I agree it’s nice if it’s more readable, but I would not base a decision on this unless it was the only difference. Errors: This seems like a VS Code problem? It’s worth looking into on my part. FYI, I’m iterating on a different PR at the moment and will come back to this when that’s done. |
Note to self Fix for the problem raised above in the section "Errors". As it happens, this also largely addresses (and improves upon?) the section "Readability of the d.ts file" by defining everything inline. diff --git a/schema/build-config-schema.js b/schema/build-config-schema.js
index 9a13fd7..a6290b5 100644
--- a/schema/build-config-schema.js
+++ b/schema/build-config-schema.js
@@ -530,9 +530,7 @@ for (const rule of rules) {
scheme.additionalProperties = false;
}
for (const [ index, name ] of rule.names.entries()) {
- schema.properties[name] = (index === 0) ? scheme : {
- "$ref": `#/properties/${rule.names[0]}`
- };
+ schema.properties[name] = scheme;
}
} |
I spent about an hour playing with
Overall, because PS - |
The vite documentation linked above (https://vitejs.dev/config/#config-intellisense) shows a simpler alternative to |
Resolves #1004
📚Description
This PR adds a new script to the project:
schema/build-config-declaration.js
. The script takes the existing configuration schema, and generated a new filemarkdownlint-config.d.ts
with type declaration for the config. This script now runs as part of thebuild-declaration
npm script.The generated type is then used in the
libs/markdownlint.js
file to enrich the existingConfiguration
type:With this change, the functions that already used this type now has proper suggestions:
A new
defineConfig()
function was added as well to make it easier to define configurations injs
files, like vite, astro or nuxt does.