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

Band-Aid fix for the bootleg eslint #1092

Merged
merged 1 commit into from
Jan 31, 2025

Conversation

lly-c232733
Copy link
Contributor

not sure why 'latest' is not working, but this fixes the annoying errors related to es5 being default for parsing.

Anything more recent than 2019 does not work unfortunately.

not sure why 'latest' is not working, but this fixes the annoying errors related to es5 being default
@lly-c232733
Copy link
Contributor Author

lly-c232733 commented Jan 31, 2025

Ah, I found out why.... in the eslint file at /libraries/eslint/eslint.js the normalizeEcmaVersion function is this:

var DEFAULT_ECMA_VERSION = 5;
var STATE = Symbol("espree's internal state");
var ESPRIMA_FINISH_NODE = Symbol("espree's esprimaFinishNode");
var tokTypes = Object.assign({}, acorn.tokTypes, jsx.tokTypes);

/**
 * Normalize ECMAScript version from the initial config
 * @param {number} ecmaVersion ECMAScript version from the initial config
 * @returns {number} normalized ECMAScript version
 */
function normalizeEcmaVersion(ecmaVersion) {
    if (typeof ecmaVersion === "number") {
        var version = ecmaVersion;

        // Calculate ECMAScript edition number from official year version starting with
        // ES2015, which corresponds with ES6 (or a difference of 2009).
        if (version >= 2015) {
            version -= 2009;
        }

        switch (version) {
            case 3:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
                return version;

            default:
                throw new Error("Invalid ecmaVersion.");
        }
    } else {
        return DEFAULT_ECMA_VERSION;
    }
}

@pano9000
Copy link
Member

Hi,

can you elaborate on what you mean with "annoying errors related to es5 being default for parsing." – just for my understanding what the issues are :-)

thanks

@lly-c232733
Copy link
Contributor Author

On all the JS frontend/backend (easy to see during widget development) eslint parses the code and gives "error Parsing error: The keyword 'const' is reserved" and therefore it is displayed as a code mistake. This also stops the further parsing of the rest of the code. This is because (as per https://www.w3schools.com/js/js_versions.asp) const was added with the es6 ecmascript edition.

For this project we use node JS 22 which technically is ES15, but unfortunately, this implementation of ESLint is old and therefore can only go up to 10. But 10 is better than 5!

The proper fix would be to use the ESLint npm package, but I guess that is difficult because in this case, eslint is running client side

@lly-c232733
Copy link
Contributor Author

This error was reported in the previous repo as well.

zadam/trilium#4696

@pano9000
Copy link
Member

thanks for the clarification!

The proper fix would be to use the ESLint npm package, but I guess that is difficult because in this case, eslint is running client side

this was brought up previously, I've now created a separate task to track this: #1094

@lly-c232733
Copy link
Contributor Author

Ah nice. Is someone working on it already? If not I can try for a sec

@pano9000
Copy link
Member

no, I don't think anyone is working on this, so if you are up for it, feel free and give it a shot

@Redpie16
Copy link

Redpie16 commented Feb 3, 2025

Any idea of why I still get the parser error even after adding this modification? I also get the parser error for class also being reserved if I change const to var.

here is the code

/* eslint-disable */
var template = '<div id="my-widget"><button>Click Me!</button></div>';  //this is where the const was

class MyWidget extends api.BasicWidget {
    get position() {return 1;}
    get parentWidget() {return "left-pane"}
    
    doRender() {
        this.$widget = $(template);
        return this.$widget;
    }
}

module.exports = new MyWidget();

@lly-c232733
Copy link
Contributor Author

Which one specifically do you get?

@lly-c232733
Copy link
Contributor Author

I suspect you are actually getting a parsing error that says 'Unexpected token api'

This is because for some reason trillium is not able to pass the api object to eslint... I have looked into it a little bit, but not figured out a solution yet

@eliandoran
Copy link
Contributor

On the latest develop, the note renders just fine without any linter warnings:

image

@Redpie16 , perhaps you could test on the latest nightly?

@Redpie16
Copy link

Redpie16 commented Feb 3, 2025

Which one specifically do you get?
If I look at the custom request handler example still has the parser error for const?
image
I get Parsing Error: keyword class is reserved
image

@Redpie16 , perhaps you could test on the latest nightly

I will

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

Successfully merging this pull request may close these issues.

4 participants