Manual:Coding conventions/Vue/cs

Tato stránka popisuje kódovací konvence pro Vue v MediaWiki codebase. Viz také konvence JavaScript a CSS/LESS, které se vztahují na kód v souborech Vue.

Odkazování
Jako nástroj pro kvalitu kódu používáme ESLint. Vlastní konfigurace pro Wikimedii obsahuje obecná i specifická pravidla MediaWiki pro kód Vue.



Aby ESLint rozpoznával definice komponent
Aby ji ESLint jako takovou rozpoznal, musíte nad definicí komponenty uvést  (viz příklad níže). Pokud ESLint zjistí, že tento komentář chybí, upozorní vás a řekne vám, abyste jej přidali, ale dokud tak neučiníte, nebude schopen interpretovat váš kód jako kód Vue.



Nastavení ESLint
Pokud již ve svém  používáte přednastavení   nebo   z eslint-config-wikimedia balíčku, pravidla lint pro Vue se automaticky použijí na soubory. Počínaje verzí 0.23.0 eslint-config-wikimedia jsou výchozí pravidla ES5 Vue 2 a ES6 výchozí Vue 3. Nový kód by měl být napsán pomocí Vue 3 (a následně pomocí ES6). Pravidla ES5+Vue2 jsou určena pouze pro starší kód.

Single-file Components
Vue's single-file components format should be used wherever possible. This allows templates, logic, and (optionally) styles for a given component to live together in one file. ResourceLoader supports on-the-fly compilation of  files (see here for more info about using Vue with ResourceLoader). This allows developers to write  files without needing to rely on any new build tools (Rollup, Webpack, etc), while still benefiting from the various optimizations that ResourceLoader provides for front-end code (RTL styling via CSS Janus, JS minification, etc).

Where possible, Vue code should follow the Vue community's style guide. In particular, all recommendations in "Priority A: Essential" should be followed at all times. Any MediaWiki-specific exceptions will be called out below. If ESLint is set up correctly, it will enforce all rules from the Vue style guide (Priority A, B and C) as well as MediaWiki-specific rules and exceptions.

General Structure
Single-file components are broken into three sections:,  , and  ; components should follow this order, with the   block being optional. Each component file should be listed individually under the  property in the appropriate module definition in. Make sure that the module definition also includes the  module as a dependency.

Template

 * Component tags must not be self-closing: this is a departure from Vue's style guide recommendations based on current limitations in ResourceLoader. Regardless of whether a component uses slots, it should have a closing tag.


 * Component tags must use kebab-case, as opposed to PascalCase. This is also a departure from Vue's style guide recommendations due to limitations in ResourceLoader.


 * Use the directive short-hands ( instead of ,   instead of  ).
 * Elements with multiple attributes should break them out onto separate lines
 * Component templates should only include simple expressions; for anything more complex, define a computed property or method in the  section instead.
 * Message strings in templates must be internationalized just like in standard JS or PHP UI development. See the documentation on internationalization in Vue for more information.
 * Use  sparingly and carefully, because it can lead to XSS vulnerabilities if used incorrectly. If you must use , carefully audit the code that generates the HTML to ensure that all untrusted input is escaped.
 * For parsed i18n messages, using  is not necessary in most cases. Use   instead, if possible.
 * Because  is discouraged, using it causes an ESLint error. If you must use it, add   on the line above the tag that uses   to dismiss the ESLint warning.
 * Before adding this override, double-check your code to ensure your usage of  is safe.

Script

 * Single-file components delivered via ResourceLoader should follow the CommonJS module format and should use ResourceLoader's PackageFiles feature. This means that each component file should include a  statement, and should import other code using
 * Component options should be specified in the order defined here: https://github.com/wikimedia/eslint-config-wikimedia/blob/master/vue-common.json. Generally this means:,  ,  ,  ,  ,  ,  ,   properties,  , watchers, lifecycle hooks, and finally render functions (in the rare situations where those need to be defined manually).
 * Render functions should be avoided outside of special cases; HTML-style templates are preferred instead. The  module provided in MediaWiki core is the full version, which includes the template compiler.
 * Use prop definitions and consider specifying defaults or validating data where necessary. Boolean props should default to false.
 * Avoid implicit parent-child communication and the mutation of received props within a child component. Prefer the "props down / events up" approach
 * Avoid implicit parent-child communication and the mutation of received props within a child component. Prefer the "props down / events up" approach

Style

 * MediaWiki CSS and LESS conventions apply, styles are linted with custom Wikimedia stylelint config
 * Since each component should contain a single top-level component, styles should be nested under a single selector (if using LESS)
 * Conditional CSS classes should be used for dynamic styles. Object syntax is preferred for computed properties that are bound to class attributes.
 * Vue transition names should follow the same pattern as CSS class names (e.g. including an extension-specific prefix)

Progressive Enhancement
TBD: define a no-JS fallback for most features you build this way.