Skip to content

Commit c95e5b3

Browse files
bz2steren
authored andcommitted
Add style lint rules and fix code (#60)
Based off the Google style guide: https://google.github.io/styleguide/jsguide.html As encoded in the eslint-config-google package, with some tweaks suitable for an ES5 only project. For now, the max-len rule has not been added.
1 parent 3b069de commit c95e5b3

7 files changed

Lines changed: 148 additions & 117 deletions

File tree

.eslintrc.json

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,40 @@
1212
"Promise": false
1313
},
1414
"rules": {
15+
"array-bracket-spacing": "error",
1516
"block-scoped-var": "error",
17+
"brace-style": "error",
18+
"camelcase": ["error", {"properties": "never"}],
19+
"comma-dangle": ["error", "always-multiline"],
20+
"comma-spacing": "error",
1621
"complexity": ["error", 15],
22+
"computed-property-spacing": "error",
1723
"curly": "error",
1824
"eqeqeq": ["error", "smart"],
19-
"linebreak-style": ["error", "unix"],
25+
"eol-last": "error",
26+
"func-call-spacing": "error",
27+
"indent": ["error", 2],
28+
"key-spacing": "error",
29+
"keyword-spacing": "error",
30+
"linebreak-style": "error",
31+
"new-cap": "error",
32+
"no-array-constructor": "error",
33+
"no-multiple-empty-lines": ["error", {"max": 2}],
34+
"no-tabs": "error",
35+
"no-trailing-spaces": "error",
36+
"object-curly-spacing": "error",
37+
"padded-blocks": ["error", "never"],
2038
"quote-props": ["error", "as-needed"],
2139
"quotes": ["error", "single"],
2240
"semi": "error",
41+
"semi-spacing": "error",
42+
"space-before-blocks": "error",
43+
"space-before-function-paren": ["error", {
44+
"anonymous": "never",
45+
"named": "never"
46+
}],
47+
"spaced-comment": ["error", "always"],
48+
"switch-colon-spacing": "error",
2349
"unicode-bom": ["error", "never"]
2450
},
2551
"overrides": [{

demo/demo-angular.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ angular.module('demoApp', []) // eslint-disable-line no-undef
44
var StackdriverErrors = new $window.StackdriverErrorReporter();
55
StackdriverErrors.start({
66
projectId: '<your-project-id>',
7-
key: '<your-api-key>'
7+
key: '<your-api-key>',
88
});
99

1010
return function(exception, cause) {

demo/demo.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function updateConfig() {
2727
key: key,
2828
projectId: projectId,
2929
service: 'webapp',
30-
version: 'demo'
30+
version: 'demo',
3131
});
3232
}
3333

@@ -37,7 +37,7 @@ function loadFromLocalStorage() {
3737
document.getElementById('input-apikey').value = key;
3838
document.getElementById('input-projectid').value = projectId;
3939

40-
if(key && projectId) {
40+
if (key && projectId) {
4141
updateConfig();
4242
} else {
4343
// eslint-disable-next-line no-console
@@ -53,12 +53,12 @@ function vanillaCrash() { // eslint-disable-line no-unused-vars
5353
starUsers();
5454
}
5555
function starUsers() {
56-
for(var i = 0; i < users.length; i++) {
56+
for (var i = 0; i < users.length; i++) {
5757
users.starred = true;
5858
}
5959
}
6060
function displayUserInfo() {
61-
if(!users) {
61+
if (!users) {
6262
StackdriverErrors.report('No user information to display');
6363
}
6464
}

gulpfile.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ var SRC_FILE = 'stackdriver-errors.js';
2525
var DEST = 'dist/';
2626

2727
var dependencies = [
28-
'./node_modules/stacktrace-js/dist/stacktrace-with-promises-and-json-polyfills.js',
28+
'./node_modules/stacktrace-js/dist/stacktrace-with-promises-and-json-polyfills.js',
2929
];
3030

3131
gulp.task('dist', function() {
3232
return gulp.src(dependencies.concat(SRC_FILE))
33-
.pipe(sourcemaps.init({ loadMaps: true }))
33+
.pipe(sourcemaps.init({loadMaps: true}))
3434
.pipe(concat(SRC_FILE.replace('.js', '-concat.js')))
3535
// This will output the non-minified version
3636
.pipe(gulp.dest(DEST))
3737
// This will minify and rename to stackdriver-errors.min.js
3838
.pipe(uglify())
39-
.pipe(rename({ extname: '.min.js' }))
39+
.pipe(rename({extname: '.min.js'}))
4040
.pipe(sourcemaps.write('maps'))
4141
.pipe(gulp.dest(DEST));
4242
});
@@ -53,7 +53,7 @@ gulp.task('demo-js', function() {
5353
return gulp.src('demo/demo.js')
5454
.pipe(sourcemaps.init())
5555
.pipe(uglify())
56-
.pipe(rename({ extname: '.min.js' }))
56+
.pipe(rename({extname: '.min.js'}))
5757
.pipe(sourcemaps.write('maps'))
5858
.pipe(gulp.dest('dist'));
5959
});

stackdriver-errors.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@
4040
* @param {Boolean} [config.disabled=false] - Set to true to not report errors when calling report(), this can be used when developping locally.
4141
*/
4242
StackdriverErrorReporter.prototype.start = function(config) {
43-
if(!config.key && !config.targetUrl) {
43+
if (!config.key && !config.targetUrl) {
4444
throw new Error('Cannot initialize: No API key or target url provided.');
4545
}
46-
if(!config.projectId && !config.targetUrl) {
46+
if (!config.projectId && !config.targetUrl) {
4747
throw new Error('Cannot initialize: No project ID or target url provided.');
4848
}
49-
if(typeof StackTrace === 'undefined') {
49+
if (typeof StackTrace === 'undefined') {
5050
// Inform about missing dependency
5151
throw new Error('make sure you loaded “dist/stackdriver-errors-concat.js” or “dist/stackdriver-errors-concat.min.js”, or that you imported the “stacktrace-js” module');
5252
}
@@ -56,7 +56,7 @@
5656
this.targetUrl = config.targetUrl;
5757
this.context = config.context || {};
5858
this.serviceContext = {service: config.service || 'web'};
59-
if(config.version) {
59+
if (config.version) {
6060
this.serviceContext.version = config.version;
6161
}
6262
this.reportUncaughtExceptions = config.reportUncaughtExceptions !== false;
@@ -66,22 +66,22 @@
6666
// Register as global error handler if requested
6767
var noop = function() {};
6868
var that = this;
69-
if(this.reportUncaughtExceptions) {
69+
if (this.reportUncaughtExceptions) {
7070
var oldErrorHandler = window.onerror || noop;
7171

7272
window.onerror = function(message, source, lineno, colno, error) {
73-
if(error){
73+
if (error) {
7474
that.report(error).catch(noop);
7575
}
7676
oldErrorHandler(message, source, lineno, colno, error);
7777
return true;
7878
};
7979
}
80-
if(this.reportUnhandledPromiseRejections) {
80+
if (this.reportUnhandledPromiseRejections) {
8181
var oldPromiseRejectionHandler = window.onunhandledrejection || noop;
8282

8383
window.onunhandledrejection = function(promiseRejectionEvent) {
84-
if(promiseRejectionEvent){
84+
if (promiseRejectionEvent) {
8585
that.report(promiseRejectionEvent.reason).catch(noop);
8686
}
8787
oldPromiseRejectionHandler(promiseRejectionEvent.reason);
@@ -96,10 +96,10 @@
9696
* @returns {Promise} A promise that completes when the report has been sent.
9797
*/
9898
StackdriverErrorReporter.prototype.report = function(err) {
99-
if(this.disabled) {
99+
if (this.disabled) {
100100
return Promise.resolve(null);
101101
}
102-
if(!err) {
102+
if (!err) {
103103
return Promise.reject(new Error('no error to report'));
104104
}
105105

@@ -108,15 +108,15 @@
108108
payload.context = this.context;
109109
payload.context.httpRequest = {
110110
userAgent: window.navigator.userAgent,
111-
url: window.location.href
111+
url: window.location.href,
112112
};
113113

114114
var firstFrameIndex = 0;
115-
if(typeof err == 'string' || err instanceof String) {
115+
if (typeof err == 'string' || err instanceof String) {
116116
// Transform the message in an error, use try/catch to make sure the stacktrace is populated.
117117
try {
118118
throw new Error(err);
119-
} catch(e) {
119+
} catch (e) {
120120
err = e;
121121
}
122122
// the first frame when using report() is always this library
@@ -125,15 +125,15 @@
125125
var that = this;
126126
// This will use sourcemaps and normalize the stack frames
127127
// eslint-disable-next-line no-undef
128-
return StackTrace.fromError(err).then(function(stack){
128+
return StackTrace.fromError(err).then(function(stack) {
129129
payload.message = err.toString();
130-
for(var s = firstFrameIndex; s < stack.length; s++) {
130+
for (var s = firstFrameIndex; s < stack.length; s++) {
131131
payload.message += '\n';
132132
// Reconstruct the stackframe to a JS stackframe as expected by Error Reporting parsers.
133133
// stack[s].source should not be used because not populated when created from source map.
134-
//
134+
//
135135
// If functionName or methodName isn't available <anonymous> will be used as the name.
136-
payload.message += [' at ', stack[s].getFunctionName() || '<anonymous>', ' (', stack[s].getFileName(), ':', stack[s].getLineNumber() ,':', stack[s].getColumnNumber() , ')'].join('');
136+
payload.message += [' at ', stack[s].getFunctionName() || '<anonymous>', ' (', stack[s].getFileName(), ':', stack[s].getLineNumber(), ':', stack[s].getColumnNumber(), ')'].join('');
137137
}
138138
return that.sendErrorPayload(payload);
139139
}, function(reason) {

test/setup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var stackdriverErrors = require('../stackdriver-errors');
99
global.StackdriverErrorReporter = stackdriverErrors.StackdriverErrorReporter;
1010

1111
global.window = window = {
12-
location: { href: 'http://stackdriver-errors.test/' },
13-
navigator: { userAgent: 'FakeAgent' },
12+
location: {href: 'http://stackdriver-errors.test/'},
13+
navigator: {userAgent: 'FakeAgent'},
1414
};
1515
global.XMLHttpRequest = sinon.FakeXMLHttpRequest;

0 commit comments

Comments
 (0)