Page MenuHomePhabricator

Provide API-powered linting of supported languages in CodeMirror
Open, MediumPublic

Description

Adjacent idea to T394964: Allow customization of linting rules in CodeMirror

Background

In-browser linting is great because it is fast. However for JavaScript, CSS, and of course wikitext, MediaWiki has an additional layer of linting for each of these content models. For sanitized-css specifically, violations would prevent the edit from being saved. It'd be great to surface these linting errors to users without requiring them to save or even preview.

In T190114 it was suggested to have the Web Workers poll the server instead of doing clientside processing, but it is believed this might be too slow for some users. Perhaps we could have both clientside and serverside linting.

Potential solutions

  • Allow API-powered linters to run in parallel with the existing JS worker linters
  • Add preferences to selectively toggle linters on/off
  • Add a button and/or keyboard shortcut to do a "full lint" of the code, if the API is too slow to query continuously

Checklist

For CSS, work needs to be done to have css-sanitizer and/or TemplateStyles return the violations in a structured format. See T190114. So proper server-side linting of sanitized CSS via an API is currently not possible. We can do some things with Stylelint, such as r1190686 which warns about vendor prefixes (which are disallowed in sanitized CSS).

Related Objects

Event Timeline

The only systems I know that will actually prevent a save of a wikitext content model page are associated with the edit filter or a blacklist (see also T279275), not the linter extension.

There are also definitely lints that are treated as annoying on en (contrast and duplicate IDs off the cuff, primarily caused by the likes of navboxes and references) and I'm not sure what to suggest about those.

Prior art for lint and editor is somewhere in the direction of LintHint.

The Linter extension doesn't prevent saves, no! I imagine that would be quite bad. Apologies for my poor wording. The "prevent saving" bit is referring to sanitized-css.

I'm thinking AbuseFilter will stay on Ace for a while, unless there's demand to bring CodeMirror there. It'd need a dedicated stream parser, similar to what they have now for Ace.

Anyway, the clientside linter is part of T373711, which we already have working. This task is about adding an additional means to lint by querying the server on-demand, as the "source of truth" so to speak.

Showing issues that prevent the save would be nice, too. I think the fundamental limitation here is that the linter / filter / whatever needs to be able to return errors associated with specific lines. That's not always straightforward (see e.g. T174554: AbuseFilter should expose matched text to warning messages for some related discussion), although for blacklists it should be easy to add.

Parse errors in a non-line-specific format (all errors squished together into a single string) are already returned on preview (T190120: Parse warnings not visible with live preview).

For JavaScript, the Peast PHP package is available in Core. We'll need to add an action API endpoint to make use of it (unless one already exists?). See ResourceLoader/Module.php#999

Not sure if it's necessary. We use Peast just to tell if a script is valid ES2017, which can be reliably determined by ESlint as well, and must faster at that. (If ESlint doesn't expose a suitable API, you could use Acorn which should already be in the bundle considering it's a dependency of Eslint).

We use Peast just to tell if a script is valid ES2017, which can be reliably determined by ESlint as well, and must faster at that. (If ESlint doesn't expose a suitable API, you could use Acorn which should already be in the bundle considering it's a dependency of Eslint).

The ecmaversion: 8 in ESLint config does that. But I don't think it is a good idea because many JS pages don't need to pass Peast so they may be written in more advanced syntax.

OK! Not having to create an API endpoint sounds ideal, hehe, though I do think it would be pretty neat :/ Probably better fit for Core, if it is to be used for validation of ResourceLoader-managed JS pages.

Let's focus on T394964: Allow customization of linting rules in CodeMirror for now.

The MediaWiki-extensions-Linter API is useless in this case because it only returns lint errors of existing pages. We need to use the REST API, which is still marked as "experimental".

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

[mediawiki/extensions/CodeMirror@master] CodeMirrorMediaWiki: lint using the REST API

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

MusikAnimal renamed this task from Add call to action to perform server-provided linting of JS, CSS and wikitext in CodeMirror to Provide API-powered linting of JS, CSS and wikitext in CodeMirror.Oct 13 2025, 5:58 PM
MusikAnimal renamed this task from Provide API-powered linting of JS, CSS and wikitext in CodeMirror to Provide API-powered linting of supported languages in CodeMirror.
MusikAnimal raised the priority of this task from Low to Medium.
MusikAnimal updated the task description. (Show Details)

For CSS, work needs to be done to have css-sanitizer and/or TemplateStyles return the violations in a structured format.

css-sanitizer does return violations in a structured format. This is used by TemplateStyles for showing validation errors in previews and during save. It's easy to expose an API in TemplateStyles which CodeMirror can invoke.

Change #1216635 had a related patch set uploaded (by SD0001; author: SD0001):

[mediawiki/extensions/TemplateStyles@master] API module for exposing sanitization errors in sanitized-css content

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

Change #1219938 had a related patch set uploaded (by SD0001; author: SD0001):

[mediawiki/extensions/CodeMirror@master] Add API module for server-side content validation

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

Change #1220388 had a related patch set uploaded (by SD0001; author: SD0001):

[integration/config@master] Zuul: [CodeMirror] Add Scribunto, TemplateStyles phan dependencies

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

Change #1216635 abandoned by SD0001:

[mediawiki/extensions/TemplateStyles@master] API module for exposing sanitization errors in sanitized-css content

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

Change #1220388 merged by jenkins-bot:

[integration/config@master] Zuul: [CodeMirror] Add Scribunto, TemplateStyles phan dependencies

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

Change #1219938 merged by jenkins-bot:

[mediawiki/extensions/CodeMirror@master] Add API module for server-side content validation

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

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

[mediawiki/extensions/CodeMirror@master] API-powered CSS/Lua linting

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

Change #1224044 merged by jenkins-bot:

[mediawiki/extensions/CodeMirror@master] API-powered CSS/Lua linting

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