Juris-M / citeproc-js

A JavaScript implementation of the Citation Style Language (CSL) https://citeproc-js.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[notice] Next.js 13.3.1 fails to build citeproc (npm) 2.4.63

larsgw opened this issue · comments

This is not a bug with citeproc-js (I think), but in citation-js/citation-js#194 an issue was reported where Next.js (or its code-optimizing dependencies) produces invalid JS output when building the citeproc npm package, specifically this snippet:

    // Build skip-word regexp
    function makeRegExp(lst) {
        var lst = lst.slice();
        var ret = new RegExp( "(?:(?:[?!:]*\\s+|-|^)(?:" + lst.join("|") + ")(?=[!?:]*\\s+|-|$))", "g");
        return ret;
    }
    this.locale[this.opt.lang].opts["skip-words-regexp"] = makeRegExp(this.locale[this.opt.lang].opts["skip-words"]);

I'm not sure what to do with that information, apart from reducing the code to an MCVE (Edit: vercel/next.js#48731), but I thought I'd share the information here in case others are encountering it.

The JS code of citeproc-js looks completely fine to me, unless I am missing something. Perhaps the var lst when there's already an argument named lst is problematic but if the spec accepts it I don't think the compiler should produce invalid output.

makeRegExp is equivalent to the following, which in my mind shouldn't work, but it does so I'm not sure. I understand why Next.js/their optimizer fails though.

function makeRegExp(lst) {
    var lst; // what does this do if it does not even overwrite the lst parameter?
    lst = lst.slice();
    var ret = new RegExp( "(?:(?:[?!:]*\\s+|-|^)(?:" + lst.join("|") + ")(?=[!?:]*\\s+|-|$))", "g");
    return ret;
}

Normal behavior of var apparently.

Duplicate variable declarations using var will not trigger an error, even in strict mode, and the variable will not lose its value, unless another assignment is performed.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

(emphasis mine)

This was fixed upstream.