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

Rule's Options Schema #700

Open
2 tasks
azu opened this issue Sep 9, 2020 · 1 comment
Open
2 tasks

Rule's Options Schema #700

azu opened this issue Sep 9, 2020 · 1 comment
Labels
Status: Proposal Request for comments Type: Feature New Feature

Comments

@azu
Copy link
Member

azu commented Sep 9, 2020

Currently, textlint does not check the actual option.

So, the user pass an invalid option and does not show any errors.

For example, textlint-rule-en-capitalization support folllowing options.

{
    "rules": {
        "en-capitalization": {
             // allow lower-case words in Header
             "allowHeading": true,
             // allow lower-case words in Image alt
             "allowFigures": true,
             // allow lower-case words in ListItem
             "allowLists": true,
             // allow lower-case words in anywhere
             "allowWords": []
         }
    }
}

However, the user passed an invalid option like "allowHead: true and textlint does not show any error.

{
    "rules": {
        "en-capitalization": {
             "allowHead": true
         }
    }
}

textlint does not know that the rule has what options.

Rule Options Schema

To resolve this, we need to introduce rule's options schema.
ESLint has similar one - Options Schemas.
Each rule defines JSON Schema and ESLint use it for validation.

As a various rule author, I do not want to write JSON Schema by handwriting 😞
Instead of it, We can write a rule in TypeScript and We can use Type for JSON Schema.

I've created babel-plugin-textlint-scripts.
This babel plugin converts TypeScript type to JSON Schema.

In

export type Options = {
    str_key: string;
};
const report = () => {
    return {};
};
export default report;

Out

const report = () => {
  return {};
};

export default report;
export const meta = {
  name: "@textlint/babel-plugin-textlint-scripts",
  description: "A babel plugin for textlint-scripts.",
  homepage: "https://github.com/textlint/babel-plugin-textlint-scripts",
  schema: {
    "type": "object",
    "properties": {
      "str_key": {
        "type": "string"
      }
    },
    "additionalProperties": false,
    "required": ["str_key"],
    "$schema": "http://json-schema.org/draft-07/schema#"
  }
};

This plugin will help us to integrate Options schema.

Of course, we can write JSON by handwriting.

TODO

  • Define spec for JSON schema and meta object
  • Implement validation logics into textlint
    • Should we implment this into @textlint/core?
@azu azu added Type: Feature New Feature Status: Proposal Request for comments labels Sep 9, 2020
@azu
Copy link
Member Author

azu commented Sep 9, 2020

Recently, I've created @textlint/config-loader package.
We create a new @textlint/config-validator package that validates the rule option with the config.

Pasado code

import { loadConfig } from "@textlint/config-loader";
import { validate } from "@textlint/config-validator";


(async function () {
    const configResult = await loadConfig({
        cwd: process.cwd()
    });
    if (!configResult.ok) {
        console.error(configResult.error.message, configResult.error.errors);
        return;
    }
    // config includes .textlint opsions and rule instance
    const validateResult = await validate(configResult.config);
    if(!result.ok) {
        console.error(validateResult.error.message, validateResult.error.errors);
        return;
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Proposal Request for comments Type: Feature New Feature
Projects
None yet
Development

No branches or pull requests

1 participant