textlint-rule / textlint-rule-alex

textlint rule for alex
MIT License
11 stars 3 forks source link

Update alex version #17

Closed ryo-funaba closed 1 year ago

ryo-funaba commented 2 years ago

【issue】 While using this repository, I received a security notice from Dependabot regarding vulnerable version of trim.

The security notice is here.

スクリーンショット 2022-09-10 16 33 45

【solution】 I think updating alex version to 10.0.0 will solve this issue because latest version of alex depends on latest version of remark-parse

Thank you.

ryo-funaba commented 2 years ago

@azu PR is here

ryo-funaba commented 2 years ago

I found a same issue in this repo.

That issue was closed by ESM..?

https://github.com/textlint-rule/textlint-rule-alex/issues/13

azu commented 2 years ago

https://github.com/get-alex/alex/issues/319 Alex only fix this issue in 10.0.0. 10.0.0 use pure ESM. It disallow to require("alex").

textlint does not support Pure ESM rule yet. https://github.com/textlint/textlint/issues/868

Two options:

  1. Fix textlint to support Pure ESM
  2. Use Dynamic Import(import("alex")) in this ruls
    • use import(...) instead of import x from "..."

1's blocker is interop issue between CJS and ESM.

2 is just using Dynamic Import for alex@10.

Node.js disallow to import ESM from CJS using import x from "mod" statement . However, Node.js allow to import ESM from CJS using const x = await import("mod"). Probably, following changes will work in Node.js 12+?.

import {RuleHelper} from "textlint-rule-helper";
- import alex from "alex";
const defaultOptions = {
    allow: undefined,
    deny: undefined,
    noBinary: false,
    profanitySureness: 0
};
export default function textlintRuleAlex(context, options = {}) {
    const {Syntax, RuleError, report, getSource} = context;
    const helper = new RuleHelper(context);
    const opts = {...defaultOptions, ...options};
    /*
    { [1:5-1:14: `boogeyman` may be insensitive, use `boogey` instead]
    message: '`boogeyman` may be insensitive, use `boogey` instead',
    name: '1:5-1:14',
    file: '',
    reason: '`boogeyman` may be insensitive, use `boogey` instead',
    line: 1,
    column: 5,
    location: Position { start: [Object], end: [Object] },
    fatal: false,
    ruleId: 'boogeyman-boogeywoman',
    source: 'retext-equality' }
     */
    const reportError = (node, result) => {
        const ruleError = new RuleError(`[${result.ruleId}] ${result.message}`, {
            line: result.line - 1,
            column: result.column - 1
        });
        report(node, ruleError);
    };
    return {
-        [Syntax.Str](node){
+        async [Syntax.Str](node){
+            const alex = await import("alex").then(mod => mod.markdown); // markdown equals to default export https://github.com/get-alex/alex/blob/1b4c1be7991b30502cd56fb1d4769708df6853a5/index.js#L56
            if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
                return;
            }
            const text = getSource(node);
            const messages = alex(text, opts).messages;
            messages.forEach((result) => {
                reportError(node, result);
            });
        }
    }
}

2 is just workaround until textlint ESM support.

ryo-funaba commented 2 years ago

Thank you for your explanation.

ok, I'll wait until textlint ESM support.

tyler36 commented 1 year ago

Any progress on this?

azu commented 1 year ago

Can you create a PR?

tyler36 commented 1 year ago

I created a PR #25 but it has issues. If anyway can help me, it would be great.