Skip to content

Commit 1b16c31

Browse files
committed
Add configuration for prettier code formatting
1. Add prettier and configuration files, but do not format code yet. 2. Configure it for all html|css|js|md code. 3. Fix a bug it found in `CZML Custom Properties.html` 4. Add a pre-commit hook via husky and pretty-quick which will automatically format changed files when they are staged on the client. 5. Run `prettier-check` during CI and fail the build of code is not formatted properly. 6. Install eslint config for prettier. This does not enable prettier checking in eslint (it's unusably slow) but instead just makes sure that no eslint rules conflict with prettier formatting. 7. Update .editorconfig to match (since prettier respects it) 8. Tweak Sandcastle to handle prettier formated examples
1 parent 74821eb commit 1b16c31

File tree

8 files changed

+31
-57
lines changed

8 files changed

+31
-57
lines changed

.editorconfig

+1-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ root = true
33

44
[*]
55
indent_style = space
6-
indent_size = 4
6+
indent_size = 2
77
charset = utf-8
88
trim_trailing_whitespace = true
99
insert_final_newline = true
10-
11-
[*.md]
12-
trim_trailing_whitespace = false
13-
14-
[package.json]
15-
indent_size = 2

.eslintrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "./Tools/eslint-config-cesium/browser.js",
2+
"extends": ["./Tools/eslint-config-cesium/browser.js", "prettier"],
33
"plugins": [
44
"html"
55
],

.prettierignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/node_modules
2+
/Build
3+
/ThirdParty
4+
/Apps/Sandcastle/ThirdParty
5+
/Source/Cesium.js
6+
/Source/Shaders/**/*.js
7+
/Source/ThirdParty/**
8+
/Source/Workers/**/*
9+
!/Source/Workers/cesiumWorkerBootstrapper.js
10+
!/Source/Workers/transferTypedArrayTest.js
11+
*.min.js

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ script:
99
- npm --silent run deploy-status -- --status pending --message 'Waiting for build'
1010

1111
- npm --silent run eslint
12+
- npm --silent run prettier-check
1213

1314
- npm --silent run build
1415
- npm --silent run coverage -- --browsers FirefoxHeadless --webgl-stub --failTaskOnError --suppressPassed

Apps/Sandcastle/CesiumSandcastle.js

+1
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ require({
758758
}
759759

760760
var scriptCode = scriptMatch[1];
761+
scriptCode = scriptCode.replace(/^ {8}/gm, ""); //Account for Prettier spacing
761762

762763
var htmlText = '';
763764
var childIndex = 0;

Apps/Sandcastle/gallery/CZML Custom Properties.html

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
<div id="toolbar">
2121
<div id="propertiesMenu"></div>
2222
</div>
23-
</div>
2423

2524
<script id="cesium_sandcastle_script">
2625
function startup(Cesium) {

Documentation/Contributors/CodingGuide/README.md

+3-47
Original file line numberDiff line numberDiff line change
@@ -90,53 +90,9 @@ A few more naming conventions are introduced below along with their design patte
9090

9191
## Formatting
9292

93-
In general, format new code in the same way as the existing code.
94-
95-
* Use four spaces for indentation. Do not use tab characters.
96-
* Do not include trailing whitespace.
97-
* Put `{` on the same line as the previous statement:
98-
```javascript
99-
function defaultValue(a, b) {
100-
// ...
101-
}
102-
103-
if (!defined(result)) {
104-
// ...
105-
}
106-
```
107-
* Use curly braces even for single line `if`, `for`, and `while` blocks, e.g.,
108-
```javascript
109-
if (!defined(result))
110-
result = new Cartesian3();
111-
```
112-
is better written as
113-
```javascript
114-
if (!defined(result)) {
115-
result = new Cartesian3();
116-
}
117-
```
118-
* Use parenthesis judiciously, e.g.,
119-
```javascript
120-
var foo = x > 0.0 && y !== 0.0;
121-
```
122-
is better written as
123-
```javascript
124-
var foo = (x > 0.0) && (y !== 0.0);
125-
```
126-
* Use vertical whitespace to separate functions and to group related statements within a function, e.g.,
127-
```javascript
128-
function Model(options) {
129-
// ...
130-
this._allowPicking = defaultValue(options.allowPicking, true);
131-
132-
this._ready = false;
133-
this._readyPromise = when.defer();
134-
// ...
135-
};
136-
```
137-
* In JavaScript code, use single quotes, `'`, instead of double quotes, `"`. In HTML, use double quotes.
138-
139-
* Text files, including JavaScript files, end with a newline to minimize the noise in diffs.
93+
* We use [prettier](https://prettier.io/) to automatically re-format all JS code at commit time, so all of the work is done for you. Code is automatically reformatted when you commit.
94+
* For HTML code, keep the existing style. Use double quotes.
95+
* Text files, end with a newline to minimize the noise in diffs.
14096

14197
## Linting
14298

package.json

+13-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"cloc": "^2.3.3",
4343
"compression": "^1.6.2",
4444
"eslint": "^6.4.0",
45+
"eslint-config-prettier": "^6.10.1",
4546
"eslint-plugin-html": "^6.0.0",
4647
"express": "^4.15.0",
4748
"globby": "^11.0.0",
@@ -54,6 +55,7 @@
5455
"gulp-tap": "^2.0.0",
5556
"gulp-uglify": "^3.0.0",
5657
"gulp-zip": "^5.0.0",
58+
"husky": "^4.2.5",
5759
"jasmine-core": "^3.3.0",
5860
"jsdoc": "^3.4.3",
5961
"karma": "^4.0.0",
@@ -72,6 +74,8 @@
7274
"mime": "^2.0.3",
7375
"mkdirp": "^1.0.0",
7476
"open": "^7.0.0",
77+
"prettier": "^2.0.4",
78+
"pretty-quick": "^2.0.1",
7579
"request": "^2.79.0",
7680
"rimraf": "^3.0.0",
7781
"rollup": "^1.21.4",
@@ -81,6 +85,11 @@
8185
"stream-to-promise": "^2.2.0",
8286
"yargs": "^15.0.1"
8387
},
88+
"husky": {
89+
"hooks": {
90+
"pre-commit": "pretty-quick --staged"
91+
}
92+
},
8493
"scripts": {
8594
"convertToModules": "gulp convertToModules",
8695
"start": "node server.cjs",
@@ -111,6 +120,9 @@
111120
"sortRequires": "gulp sortRequires",
112121
"deploy-s3": "gulp deploy-s3",
113122
"deploy-status": "gulp deploy-status",
114-
"deploy-set-version": "gulp deploy-set-version"
123+
"deploy-set-version": "gulp deploy-set-version",
124+
"prettier": "prettier --write \"**/*.(js|css|html|md)\"",
125+
"prettier-check": "prettier --check \"**/*.(js|css|html|md)\"",
126+
"pretty-quick": "pretty-quick"
115127
}
116128
}

0 commit comments

Comments
 (0)