Skip to content

Commit 79585d5

Browse files
authored
style(automatting and quality): Add prettier and eslint to run on commit and build, deprecate jshint (#336)
* style(autoformat and quality checks): Adding eslint and prettier, deprecating jshint * style(autoformat and quality checks):AMP-32165 update makefile * style(autoformat and quality checks): get all files to pass prettier and eslint * style(autoformat and quality checks): Adding husky pre-commit hooks * style(autoformatting): fix order of lint staged commands * ci: add eslint and prettier checks to test * build: remove eslint and prettier checks from makefile * build: added website directory to prettier and eslint checks
1 parent db964ed commit 79585d5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+14730
-11508
lines changed

.eslintrc.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
3+
"plugins": ["prettier", "@amplitude/eslint-plugin-amplitude"],
4+
"env": { "es6": true, "browser": true, "node": true, "mocha": true },
5+
"parserOptions": {
6+
"sourceType": "module",
7+
"ecmaVersion": 2018
8+
},
9+
"rules": {
10+
"prettier/prettier": "error",
11+
"no-prototype-builtins": "off"
12+
},
13+
"globals": {
14+
"BUILD_COMPAT_REACT_NATIVE": "readonly",
15+
"BUILD_COMPAT_LOCAL_STORAGE": "readonly",
16+
"BUILD_COMPAT_SNIPPET": "readonly",
17+
"BUILD_COMPAT_2_0": "readonly",
18+
"assert": "readonly",
19+
"expect": "readonly",
20+
"should": "readonly",
21+
"define": "readonly",
22+
"amplitude": "readonly",
23+
"opera": "readonly",
24+
"ActiveXObject": "readonly"
25+
}
26+
}

.github/workflows/test.yml

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ jobs:
3030
run: |
3131
yarn install --frozen-lockfile
3232
33+
- name: prettier check
34+
run: |
35+
yarn run lint:prettier
36+
37+
- name: eslint check
38+
run: |
39+
yarn run lint:eslint
40+
3341
- name: Build and run tests
3442
run: |
3543
make test

.jshintrc

-94
This file was deleted.

.npmignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ build
1414
karma.conf.js
1515
scripts
1616
.npmignore
17-
.jshintrc
17+
.prettierrc.json
18+
.eslintrc.json
1819
amplitude.min.js
1920
amplitude-snippet.min.js
2021
amplitude.nocompat.min.js

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.md

.prettierrc.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"printWidth": 120,
3+
"proseWrap": "always",
4+
"singleQuote": true,
5+
"trailingComma": "all"
6+
}

.vscode/settings.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"editor.codeActionsOnSave": {
3+
"source.fixAll": true
4+
},
5+
"editor.defaultFormatter": "esbenp.prettier-vscode",
6+
"editor.formatOnType": true,
7+
"editor.formatOnPaste": false,
8+
"editor.formatOnSave": true,
9+
"editor.rulers": [120],
10+
"editor.tabSize": 2,
11+
"files.autoSave": "onWindowChange",
12+
"files.trimTrailingWhitespace": true,
13+
"files.insertFinalNewline": true,
14+
"search.exclude": {
15+
"**/node_modules/": true,
16+
"**/build/": true,
17+
"**/dist/": true
18+
},
19+
"[json]": {
20+
"editor.formatOnType": false,
21+
"editor.formatOnPaste": false,
22+
"editor.formatOnSave": false
23+
}
24+
}

Makefile

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ TESTS = $(wildcard test/*.js)
44
BINS = node_modules/.bin
55
MINIFY = $(BINS)/uglifyjs
66
JSDOC = $(BINS)/jsdoc
7-
JSHINT = $(BINS)/jshint
87
BUILD_DIR = build
98
PROJECT = amplitude
109
OUT = $(PROJECT).js
@@ -65,9 +64,8 @@ README.md: $(SNIPPET_OUT) version
6564
#
6665

6766
$(OUT): node_modules $(SRC) package.json rollup.config.js rollup.min.js rollup.native.js rollup.esm.js rollup.umd.js rollup.umd.min.js
68-
@$(JSHINT) --verbose $(SRC)
6967
@NODE_ENV=production $(ROLLUP) --config rollup.config.js # is the snippet build config
70-
@NODE_ENV=production $(ROLLUP) --config rollup.esm.js # does not concat dependencies, only has module and dependencies
68+
@NODE_ENV=production $(ROLLUP) --config rollup.esm.js # does not concat dependencies, only has module and dependencies
7169
@NODE_ENV=production $(ROLLUP) --config rollup.umd.js # generates npm version, also usable in require js app
7270
@NODE_ENV=production $(ROLLUP) --config rollup.native.js # generates react native build
7371
@NODE_ENV=production $(ROLLUP) --config rollup.nocompat.js # may be able to remove
@@ -79,7 +77,6 @@ $(OUT): node_modules $(SRC) package.json rollup.config.js rollup.min.js rollup.n
7977
# Target for minified `amplitude-snippet.js` file.
8078
#
8179
$(SNIPPET_OUT): $(SRC) $(SNIPPET)
82-
@$(JSHINT) --verbose $(SNIPPET)
8380
@$(MINIFY) $(SNIPPET) -m -b max-line-len=80,beautify=false | awk 'NF' > $(SNIPPET_OUT)
8481

8582
$(SEGMENT_SNIPPET_OUT): $(SRC) $(SNIPPET)

package.json

+25-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"query-string": "5"
2222
},
2323
"devDependencies": {
24+
"@amplitude/eslint-plugin-amplitude": "^1.0.1",
2425
"@babel/core": "^7.3.4",
2526
"@babel/plugin-external-helpers": "^7.2.0",
2627
"@babel/plugin-proposal-object-rest-spread": "^7.3.4",
@@ -32,11 +33,14 @@
3233
"@semantic-release/git": "^9.0.0",
3334
"chai": "^4.1.2",
3435
"date-fns": "^1.30.1",
36+
"eslint": "^7.15.0",
37+
"eslint-config-prettier": "^7.0.0",
38+
"eslint-plugin-prettier": "^3.3.0",
3539
"express": "^4.16.2",
3640
"fs-extra": "^4.0.2",
41+
"husky": "^4.3.6",
3742
"jsdoc": "^3.6.3",
3843
"jsdoc-to-markdown": "^6.0.1",
39-
"jshint": "^2.9.6",
4044
"karma": "^4.0.0",
4145
"karma-chai": "^0.1.0",
4246
"karma-chrome-launcher": "^2.2.0",
@@ -46,8 +50,9 @@
4650
"karma-sauce-launcher": "^2.0.2",
4751
"karma-sinon": "^1.0.5",
4852
"karma-sourcemap-loader": "^0.3.7",
53+
"lint-staged": "^10.5.3",
4954
"mocha": "^4.0.1",
50-
"prettier": "^2.1.1",
55+
"prettier": "^2.2.1",
5156
"requirejs": "^2.3.6",
5257
"rollup": "^1.4.1",
5358
"rollup-plugin-babel": "^4.3.2",
@@ -67,13 +72,30 @@
6772
"docs:install": "cd website/ && yarn install",
6873
"docs:generate-jsdoc": "cd website && yarn generate-jsdoc",
6974
"docs:start": "cd website/ && yarn start",
70-
"docs:deploy": "cd website/ && yarn deploy"
75+
"docs:deploy": "cd website/ && yarn deploy",
76+
"lint": "yarn run lint:prettier && yarn run lint:eslint",
77+
"lint:prettier": "prettier --check \"{src,test,scripts,website}/**/*.js\"",
78+
"lint:eslint": "eslint \"{src,test,scripts,website}/**/*.js\"",
79+
"fix": "yarn run fix:eslint && yarn run fix:prettier",
80+
"fix:prettier": "prettier --write \"{src,test,scripts,website}/**/*.js\"",
81+
"fix:eslint": "eslint --fix \"{src,test,scripts,website}/**/*.js\""
7182
},
7283
"bugs": {
7384
"url": "https://github.com/amplitude/amplitude-javascript/issues"
7485
},
7586
"homepage": "https://github.com/amplitude/amplitude-javascript#readme",
7687
"directories": {
7788
"test": "test"
89+
},
90+
"husky": {
91+
"hooks": {
92+
"pre-commit": "lint-staged"
93+
}
94+
},
95+
"lint-staged": {
96+
"{src,test,scripts,website}/**/*.js": [
97+
"prettier --write",
98+
"eslint --fix"
99+
]
78100
}
79101
}

scripts/readme.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ var path = require('path');
33

44
// Update the README with the minified snippet.
55
var cwd = process.cwd();
6-
var readmeFilename = path.join(cwd, "README.md");
6+
var readmeFilename = path.join(cwd, 'README.md');
77
var readme = fs.readFileSync(readmeFilename, 'utf-8');
88

9-
var snippetFilename = path.join(cwd, "amplitude-snippet.min.js");
9+
var snippetFilename = path.join(cwd, 'amplitude-snippet.min.js');
1010
var snippet = fs.readFileSync(snippetFilename, 'utf-8');
1111
var script =
12-
' <script type="text/javascript">\n' +
13-
snippet.trim().replace(/^/gm, ' ') + '\n\n' +
14-
' amplitude.getInstance().init("YOUR_API_KEY_HERE");\n' +
15-
' </script>';
12+
' <script type="text/javascript">\n' +
13+
snippet.trim().replace(/^/gm, ' ') +
14+
'\n\n' +
15+
' amplitude.getInstance().init("YOUR_API_KEY_HERE");\n' +
16+
' </script>';
1617

1718
var updated = readme.replace(/ +<script[\s\S]+?script>/, script);
1819
fs.writeFileSync(readmeFilename, updated);

scripts/version.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
const fs = require('fs');
22
const path = require('path');
3-
const {version} = require('../package');
4-
const {format} = require('date-fns');
3+
const { version } = require('../package');
54
const crypto = require('crypto');
65

76
const cwd = process.cwd();
87

98
function replaceTextInFile(filepath, match, replacement) {
109
var filename = path.join(cwd, filepath);
1110

12-
const updatedText = fs
13-
.readFileSync(filename, 'utf-8')
14-
.replace(match, replacement);
11+
const updatedText = fs.readFileSync(filename, 'utf-8').replace(match, replacement);
1512

1613
if (updatedText.indexOf(replacement) === -1) {
1714
throw new Error(`Failed to update text in ${filepath}`);
@@ -35,7 +32,7 @@ const sdkText = fs.readFileSync(path.join('.', `amplitude.min.js`), 'utf-8');
3532
const hash = crypto.createHash('sha384').update(sdkText).digest('base64');
3633
replaceTextInFile(
3734
path.join('src', 'amplitude-snippet.js'),
38-
/as.integrity = 'sha384-[a-zA-Z0-9+\/]+';/,
35+
/as.integrity = 'sha384-[a-zA-Z0-9+/]+';/,
3936
`as.integrity = 'sha384-${hash}';`,
4037
);
4138

0 commit comments

Comments
 (0)