Page MenuHomePhabricator

Allow customization of linting rules in CodeMirror
Open, Needs TriagePublic

Description

Adjacent to T394965: Provide API-powered linting of supported languages in CodeMirror

Background

T373711: Add support for Scribunto, JavaScript, CSS, JSON and Vue to CodeMirror 6 adds support for JS/CSS/JSON and Scribuntu. It also brought linting into the CodeMirror 6 ecosystem.

At the Wikimedia-Hackathon-2025, we spoke with the Editing team who asked us not to impose any stylistic preferences in our linters. So, for CSS we went with stylelint-config-recommended which primarily only prevents errors.

We are free however to allow users to opt-in to a coding style. The CodeMirror preferences system allows for this without adding more user options to the db, so I think the idea is worth exploring.

This idea applies to both Stylelint and the upcoming ESLint integration.

A collection of ideas

  • Provide three options: "recommended" (default, only reports about violations prone to cause errors), "Wikimedia" (eslint-config-wikimedia and stylelint-config-wikimedia), and "custom" where you could provide a custom config via a JSON page.
  • Perhaps introduce a config setting or MediaWiki JSON page so that entire wikis can impose a "default" linting style for each content model
  • Add comment-based annotation to tell CodeMirror which style to use. This would allow folks to edit pages and not be given warnings just because their own personal code style preferences differ from what the script author desires.
    • Comment-based inline configuration is already supported, but it'd be better to be able to specify a style or link to a JSON page
    • For action=edit requests (not standalone CodeMirror), we could look for an expected subpage such as /eslint.json or at MediaWiki:Eslint-wikimedia.json etc. (and likewise for stylelint)

Event Timeline

Add some sort of comment-based annotation to tell CodeMirror which style to use.

CodeEditor's JSHint allows in-page customization of linter rules using comments like /* jshint esversion:10 */ and similar. I think the same will be possible with ESlint?

Alternatively, it could be stored as page prop

Page props are an artifact of parsing. So you'll need some kind of annotation in the text to begin with.

CodeEditor's JSHint allows in-page customization of linter rules using comments like /* jshint esversion:10 */ and similar. I think the same will be possible with ESlint?

Unfortunately, it is not possible with ESLint: https://eslint.org/docs/latest/use/configure/language-options-deprecated#specifying-parser-options

CodeEditor's JSHint allows in-page customization of linter rules using comments like /* jshint esversion:10 */ and similar. I think the same will be possible with ESlint?

Unfortunately, it is not possible with ESLint: https://eslint.org/docs/latest/use/configure/language-options-deprecated#specifying-parser-options

Customizing esversion specifically is not possible, yes, but in general it can be done for other options: https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments, assuming we've not optimized away this functionality in the browserfied build.

Customizing esversion specifically is not possible, yes, but in general it can be done for other options: https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments, assuming we've not optimized away this functionality in the browserfied build.

Broadly speaking, yes, inline configuration is supported. However, the es-x plugin is not available from the browserified version.

MusikAnimal renamed this task from Allow customization of Stylelint and ESLint rules in CodeMirror to Allow customization of linting rules in CodeMirror.Sep 16 2025, 2:20 AM

Change #1273213 had a related patch set uploaded (by Bhsd; author: Bhsd):

[mediawiki/extensions/CodeMirror@master] CodeMirrorWorker: getLintConfig

https://gerrit.wikimedia.org/r/1273213

Change #1273213 merged by jenkins-bot:

[mediawiki/extensions/CodeMirror@master] CodeMirrorWorker: getLintConfig

https://gerrit.wikimedia.org/r/1273213

At the current stage (after the deployment next week), a user can customize the client-side linting rules like this:

mw.hook( 'ext.CodeMirror.ready' ).add( ( cm ) => {
    const { worker } = cm.langExtension;
    switch ( cm.mode ) {
        // WikiLint for Wikitext
        case 'mediawiki':
            worker.onload( async () => {
                const lintConfig = await worker.getLintConfig();
                $.extend( true, lintConfig, {
                    rules: {
                        ruleName: severity,
                        // e.g.
                        'tag-like': 0
                    }
                } );
                worker.setLintConfig( lintConfig );
            } );
            break;

        // ESLint for JavaScript
        case 'javascript':
            worker.onload( () => {
                worker.setConfig( {
                    rules: {
                        ruleName: severity,
                        // e.g.
                        semi: 2
                    }
                } );
            } );
            break;

        // Stylelint for CSS
        case 'css':
            worker.onload( () => {
                worker.setConfig( {
                    ruleName: option,
                    // e.g.
                    'color-named': 'never'
                } );
            } );
    }
} );

Change #1273489 had a related patch set uploaded (by Bhsd; author: Bhsd):

[mediawiki/extensions/CodeMirror@master] mediawiki worker: convention of rule customization

https://gerrit.wikimedia.org/r/1273489