Page MenuHomePhabricator

Support string literals in types in JSDoc WMF theme
Open, Needs TriagePublicFeature

Description

Feature summary (what you would like to be able to do and where):

Be able to use string literals in type definitions in the JSDoc WMF theme, e.g.

/**
 * @typedef {Object} Foo
 * @property {'bar'|'baz'} prop
 */
/**
 * @param {'a'|'b'} param
 * @return {'foo'|Foo|null}
 */
function fun(param) {}

(TypeScript’s JSDoc parser already supports them.)

Use case(s) (list the steps that you performed to discover that problem, and describe the actual underlying problem which you want to solve. Do not describe only a solution):

Cases in which a value is chosen from a set of strings, and any other strings are invalid, for example:

Benefits (why should this be implemented?):

  • They make the developer intent clearer.
  • When paired with type check powered by TypeScript, they allow the type checker to flag typos.

Implementation details:

Some keyword literals and literal-like global objects are already recognized by our JSDoc theme, see rJWTH publish.js:22 (at 23b42f16925f342ee43993ed44e26dc2204d85a9). That regex could be expanded to include string literals as well (/^'[^']*'$/).

Event Timeline

apaskulin subscribed.

Thanks for opening this, @Tacsipacsi! I think this is a good idea and would simply the theme code that allows some specific keyword literals.

Hi @Tacsipacsi, I'm moving this to Accepted Enhancement since there's been some times for folks to chime in.

Change #1024867 had a related patch set uploaded (by Tacsipacsi; author: Tacsipacsi):

[jsdoc/wmf-theme@master] Recognize string literals in types

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

Hi @Tacsipacsi, I'm moving this to Accepted Enhancement since there's been some times for folks to chime in.

Thanks! I’ve uploaded the patch, and showcased it right away in https://gerrit.wikimedia.org/r/c/mediawiki/core/+/1024868.