This document explains the basic styles and patterns used in the Shopware JavaScript codebase. New code should try to conform to these standards so that it is as easy to maintain as existing code. Of course every rule has an exception, but it's important to know the rules nonetheless.
The coding style is using the sharable eslint configuration "standard" with modifications.
Rule: indent
Level: error
We're always using 4 spaces and no tab characters. No whitespace at the end of a line. Unix-style linebreaks \n
, not Windows-style \r\n
.
// ✓ ok
function sayHello (name) {
console.log('hey', name);
}
// ✗ avoid
function sayHello (name) {
console.log('hey', name);
}
Rule: quotes
Level: error
Always use single quotes for string except to avoid escaping.
// ✓ ok
console.log('hello there');
// ✗ avoid
console.log("hello there");
Rule: keyword-spacing
Level: warn
// ✓ ok
if (condition) { ... }
// ✗ avoid
if(condition) { ... }
Rule: padded-blocks
Level: warn
// ✓ ok
if (condition) {
console.log('...');
} else {
console.log('...');
}
// ✗ avoid
if (condition) {
console.log('...');
} else {
console.log('...');
}
Rule: semi
Level: error
// ✓ ok
console.log('hello there');
// ✗ avoid
console.log('hello there')
Rule: no-unused-vars
Level: error
// ✗ avoid
function sayHello (name) {
var foo = 'bar';
console.log('hey', name);
}
Rule: space-infix-ops
Level: error
// ✓ ok
var x = 2;
var message = 'hello, ' + name + '!';
// ✗ avoid
var x=2;
var message = 'hello, '+name+'!';
Rule: comma-spacing
Level: error
// ✓ ok
var list = [1, 2, 3, 4];
function greet (name, options) { ... }
// ✗ avoid
var list = [1,2,3,4];
function greet (name,options) { ... }
Rule: brace-style
Level: error
// ✓ ok
if (options.quiet !== true) console.log('done');
// ✗ avoid
if (condition) {
// ...
}
else {
// ...
}
Rule: curly
Level: error
// ✓ ok
if (options.quiet !== true) console.log('done');
// ✓ ok
if (options.quiet !== true) {
console.log('done');
}
// ✗ avoid
if (options.quiet !== true)
console.log('done');
Rule: no-undef
Level: error
Exceptions are: document
, console
and navigator
.
// ✓ ok
window.alert('hi');
// ✗ avoid
alert('hi');
Rule: no-multiple-empty-lines
Level: error
// ✓ ok
var value = 'hello world';
console.log(value);
// ✗ avoid
var value = 'hello world';
console.log(value);
?
and :
on their own lines.Rule: operator-linebreak
Level: error
// ✓ ok
var location = env.development ? 'localhost' : 'www.api.com';
// ✓ ok
var location = env.development
? 'localhost'
: 'www.api.com';
// ✗ avoid
var location = env.development ?
'localhost' :
'www.api.com';
Rule: no-cond-assign
Level: error
This makes it clear that the expression is intentionally an assignment (=) rather than a typo for equality (===).
// ✓ ok
while ((m = text.match(expr))) {
// ...
}
// ✗ avoid
while (m = text.match(expr)) {
// ...
}