From a0eb2c6b027e2bb4b9272e8af51ed23413278f9b Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Mon, 8 Jul 2019 22:33:32 +0200 Subject: [PATCH] ES6 and general updates --- Style-Guide.md | 51 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/Style-Guide.md b/Style-Guide.md index c2c1dce..a4e6ecf 100644 --- a/Style-Guide.md +++ b/Style-Guide.md @@ -1,51 +1,51 @@ This page outlines the style conventions that PDF.js follows to maintain a consistent codebase. We ask each contributor that creates a pull request to adhere to these conventions. -Many of these conventions will also be checked automatically by a linting tool after each push to a branch of a pull request. Please refer to https://github.com/mozilla/pdf.js/wiki/Contributing#4-run-lint-and-testing for information about running the linting tool locally. +Many of these conventions will be checked automatically by a linter after each push to a branch of a pull request. Please refer to https://github.com/mozilla/pdf.js/wiki/Contributing#4-run-lint-and-testing for information about running the linter locally. ## General * Indentation: 2 spaces * Line length: 80 characters -* Required license in header: see https://github.com/mozilla/pdf.js/blob/master/src/license_header.js +* License in the file header (required): see https://github.com/mozilla/pdf.js/blob/master/src/license_header.js ## Naming * Variables and functions: lowerCamelCase -* Constructor-like functions: UpperCamelCase -* Constants: ALL_UPPER_CASE_WITH_UNDERSCORES +* Classes: UpperCamelCase +* Constants: UPPER_CASE_WITH_UNDERSCORES ## Braces Always use braces and put them on same line, even for single line control statements: ```javascript if (someVar) { - return true; + ... } else { - return null; + ... } ``` -## White space -Keep one space after control statements (if, else, while, for, et cetera): +## Whitespace +Keep one space after control statements (`if`, `else`, `while`, `for`, et cetera): ```javascript if (someVar) { ``` ## Equalities -Use only strict equalities (`===`) and inequalities (`!==`): +Only use strict equalities (`===`) and inequalities (`!==`): ```javascript if (someVar === conditionA) { - return true; + ... } else if (someVar !== conditionB) { - return false; + ... } ``` ## Variables -Variables must be defined only once within a function scope, preferably at the top of the function. +Variables must be defined only once within a function scope, preferably at the top of the function. Use `const` if the value is not mutated and `let` if it is mutated. In new code we don't use `var` unless absolutely necessary. ## Classes -The standard way of creating classes in PDF.js, when adding *new* code, is the following. Please note that by class we mean an object that is class-like. +The standard way of creating classes in PDF.js used to be the following, so you might see this pattern in the current codebase. Please note that by class we mean an object that is class-like. ```javascript var ClassName = (function ClassNameClosure() { @@ -58,13 +58,30 @@ var ClassName = (function ClassNameClosure() { ... }, - aVeryVeryVeryLongFunctionName(arg1, - arg2, - ...) { + aVeryVeryVeryVeryVeryLongFunctionName + (arg1, arg2, ...) { ... }, }; return ClassName; })(); -``` \ No newline at end of file +``` + +However, we are in the process of converting the codebase to ES6 syntax since classes are generally available in browsers and well optimized nowadays, so for new code we prefer the following way of creating classes. + +```javascript +class ClassName { + constructor(...) { + ... + } + + functionName(arg1, arg2, ...) { + ... + } + + aVeryVeryVeryVeryVeryLongFunctionName + (arg1, arg2, ...) { + ... + } +} \ No newline at end of file