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

textlint: allow to modify message for re-use rules #549

Open
azu opened this issue Aug 8, 2018 · 2 comments
Open

textlint: allow to modify message for re-use rules #549

azu opened this issue Aug 8, 2018 · 2 comments
Labels
Status: Proposal Request for comments

Comments

@azu
Copy link
Member

azu commented Aug 8, 2018

We have changed context to frozen object in textlint 11

This change make following hack error.

Issue: textlint-rule/textlint-rule-preset-google#35

✖ Error
Error while loading rule '@textlint-rule/preset-google/capitalization': Cannot assign to read only property 'report' of object '#<RuleContext>'

✖ Stack trace
TypeError: Error while loading rule '@textlint-rule/preset-google/capitalization': Cannot assign to read only property 'report' of object '#<RuleContext>'
    at Function.assign (<anonymous>)
        // overwrite report function
        return capitalizationReport(
            Object.assign(context, {
                report: (node, error) => {
                    error.message += "\n" + DocumentURL;
                    report(node, error);
                }
            }),
            options
        );

I think that it is corner case, but it is reasonable.

Purpose

A rule can re-use existing rule, but modify error message.

This is related with "Localize messages support #252"
Custom message support will resolve this issue.

Or, Should we provide overlay/proxy for context object?

const Proxy = require("textlint-rule-proxy");
const existingRule = require("textlint-rule-existing-rule");
export function rule(context){
   const proxyContext = Proxy.create(context, {
     report(node, error){
       error.message = "HI"
       // modify message and report it
       context.report(node, error);
     }
   });
   return existingRule(proxyContext);
}

Workaround

javascript - Creating new objects from frozen parent objects - Stack Overflow

Use Obejct.defineProperty for overwrite report function.

    const overlayContext = Object.create(context);
    Object.defineProperty(overlayContext, "report", {
        value: (node, error) => {
            error.message += "\nhttps://developers.google.com/style/exclamation-points";
            report(node, error);
        },
        enumerable: true,
        configurable: true,
        writable: true
    });

Related

@azu azu added the Status: Proposal Request for comments label Aug 8, 2018
@azu
Copy link
Member Author

azu commented Oct 29, 2018

Related Note: I want to get file list that are linted from RuleContext.
A rule lint the reference of the document.
Currently, a rule does not get the reference of the document.
The file list will resolve this issue.

@azu
Copy link
Member Author

azu commented Oct 29, 2018

Dead simple implementation: Use Proxy.

kernel.lintText(text, {
            ext: path.extname(filePath),
            filePath,
            rules: [
                {
                    ruleId: "file-size",
                    rule: fileSizeRule
                }
            ],
            plugins: [
                {
                    pluginId: "markdown",
                    plugin: markdown
                }
            ],
            contextProxyHandler: {
               // proxy handler
            }
        })

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

No branches or pull requests

1 participant