dev

For code review, see Board:JS Development, Board:CSS Development or Board:Scribunto Help.
For requesting a new script , see Board:Script Suggestions.

This page is a style guide for Fandom Developers Wiki code & documentation. It's a set of standards and best practices created to help developers write better code.

It encourages consistency between items, which makes them easier to read, test, and maintain. That said, you should always use your best judgement when coding, and avoid the cargo cult.

Naming conventions

The following table lists expected page titles for Dev Wiki items:

Item Location
Script/style/application documentation PAGENAME
Lua documentation Global Lua Modules/PAGENAME
JavaScript module MediaWiki:PAGENAME.js
JavaScript submodule MediaWiki:PAGENAME/<submodule>.js
CSS stylesheet MediaWiki:<PAGENAME>.css
CSS component for JS script MediaWiki:PAGENAME/<component>.css
Lua module Module:PAGENAME
Lua submodule Module:PAGENAME/<submodule>
Lua test cases Module:PAGENAME/testcases
A stylesheet related to a Lua module MediaWiki:Global Lua Modules/<PAGENAME>.css
MediaWiki/HTML dependency Template:PAGENAME

Note: Historically, Dev Wiki scripts and stylesheets didn't obey these conventions. They were named PAGENAME/code.<ext> and were stored as subpages of the documentation page. However, due to security concerns, these were moved to the MediaWiki namespace, which makes /code.<ext> unnecessary for new scripts.

Coding conventions

JavaScript

(function (window, $, mw) {
    // code
}(this, jQuery, mediaWiki));
var conf = mw.config.get([
    'wgAction',
    'wgContentLanguage',
    'wgUserLanguage',
    'wgVersion'
]);
console.log(conf.wgContentLanguage);
(window.dev = window.dev || {}).script = {};
if (
    window.PAGENAMELoaded ||
    otherLoadProtection
) {
    return;
}
window.PAGENAMELoaded = true;

CSS

/* BAD - affects *14* elements in this page */
.skin-oasis section {
    border-width: 1px;
}
/* GOOD - is scoped to a specific element */
.skin-oasis .activity-module {
    border-width: 1px;
}
/* BAD - affects multiple scripts */
.wds-global-navigation__user-menu .wds-list {
    font-weight: bold;
}
/* GOOD - targets the user menu list only */
.wds-global-navigation__user-menu > .wds-dropdown > .wds-list {
    font-weight: bold;
}

Lua

-- BAD - adds the module to Special:WhatLinksHere/Sandbox
local redlink = "[[Sandbox]]"
-- GOOD - sometimes a string is just a string
-- <nowiki>
local redlink = "[[Sandbox]]"
-- </nowiki>
-- BAD - module creates table at the end
function hello(frame)
    return 'Hello World!'
end
return {
    hello = hello
}
-- GOOD - module creates table at the start
local p = {}
function p.hello(frame)
    return 'Hello World!'
end
return p

Documentation best practice

A script's documentation likely includes a number of things. The most common are:

Much of the information you might want to include is in ready-to-use templates. Information and attribution is added using a infobox template. JavaScript, CSS, and Lua documentation should use an installation template.

Documentation i18n

For translation guidance, see Project:I18n#Translation.

To facilitate i18n in your script documentation, you can structure the main documentation page like so:

<noinclude>{{LangSelect}}</noinclude><includeonly>{{Languages}}
{{Infobox JavaScript
| Author =
| Description = {{{description|Description of your script.}}}
| Code = [[MediaWiki:{{BASEPAGENAME}}.js]]
| Updated = {{Updated|MediaWiki:{{BASEPAGENAME}}.js}}
| Scope =
| Type =
}}

{{{intro|'''{{subst:BASEPAGENAME}}''' is a ...}}}

== {{#invoke:I18n|getMsg|Documentation|installation}} ==
{{Script Install}}
</includeonly>

The main page contains the English translation, and is displayed to users using {{LangSelect}}.

Then, create language-specific subpages in the following format:

{{:{{subst:BASEPAGENAME}}
| description  = Description of your script.
| intro        = '''{{subst:BASEPAGENAME}}''' is a ...
}}

You can also use the documentation translation wizard to automatically create the transclusion for a translation.