Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Add prettier + eslint #89

Merged
merged 3 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
jobs:
test:
docker:
- image: circleci/node:4-browsers
- image: circleci/node:8-browsers
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to upgrade to 8 for lint-staged, see CI Job#14

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this have any effect on the final functionality?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The final artifact is built by the analytics.js repo so it should only affect tests

steps:
- checkout
- run: npm config set "//registry.npmjs.org/:_authToken" $NPM_AUTH
Expand All @@ -18,7 +18,7 @@ jobs:
path: junit-reports
publish:
docker:
- image: circleci/node:4-browsers
- image: circleci/node:8-browsers
steps:
- checkout
- attach_workspace: { at: . }
Expand Down
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "@segment/eslint-config/browser/legacy"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we still need to keep the old config around?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should, browser/legacy ensures we are only using ES3 compatible code which we can't really guarantee otherwise given we don't use any transpilers

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, that makes sense - we definitely need that 🙏

"extends": ["@segment/eslint-config/browser/legacy", "prettier"]
}
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? What does it do? I see our internal app codebase using single quotes but not have this prettier rule.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah nvm, they pass it as an option in the binary instead: /prettier --write --single-quote --no-semi 'src/**/*.js'. I like putting it the config better 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we remove it prettier will use double quotes when formatting. It could also be in .eslintrc and package.json IIRC. I've put it in this file so editor plugins can use this setting.

}
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Binaries
##

ESLINT := node_modules/.bin/eslint
KARMA := node_modules/.bin/karma

##
Expand Down Expand Up @@ -58,12 +57,13 @@ distclean: clean

# Lint JavaScript source files.
lint: install
@$(ESLINT) $(ALL_FILES)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're not using ESLint anymore in the makefile, we could remove it from line 5 as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today's free tip: with newer versions of yarn you can directly invoke bin programs.
Example: yarn codecov instead of ./node_modules/.bin/codecov

yarn lint

.PHONY: lint

# Attempt to fix linting errors.
fmt: install
@$(ESLINT) --fix $(ALL_FILES)
yarn format
.PHONY: fmt

# Run browser unit tests in a browser.
Expand Down
115 changes: 76 additions & 39 deletions lib/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ Analytics.prototype.addIntegration = function(Integration) {
* @return {Analytics}
*/

Analytics.prototype.init = Analytics.prototype.initialize = function(settings, options) {
Analytics.prototype.init = Analytics.prototype.initialize = function(
settings,
options
) {
settings = settings || {};
options = options || {};

Expand All @@ -121,8 +124,8 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(settings, o
// Don't load disabled integrations
if (options.integrations) {
if (
options.integrations[name] === false
|| options.integrations.All === false && !options.integrations[name]
options.integrations[name] === false ||
(options.integrations.All === false && !options.integrations[name])
) {
return;
}
Expand Down Expand Up @@ -158,7 +161,10 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(settings, o
// create a list of any integrations that did not initialize - this will be passed with all events for replay support:
this.failedInitializations = [];
each(function(integration) {
if (options.initialPageview && integration.options.initialPageview === false) {
if (
options.initialPageview &&
integration.options.initialPageview === false
) {
integration.page = after(2, integration.page);
}

Expand Down Expand Up @@ -226,9 +232,9 @@ Analytics.prototype.add = function(integration) {
Analytics.prototype.identify = function(id, traits, options, fn) {
// Argument reshuffling.
/* eslint-disable no-unused-expressions, no-sequences */
if (is.fn(options)) fn = options, options = null;
if (is.fn(traits)) fn = traits, options = null, traits = null;
if (is.object(id)) options = traits, traits = id, id = user.id();
if (is.fn(options)) (fn = options), (options = null);
if (is.fn(traits)) (fn = traits), (options = null), (traits = null);
if (is.object(id)) (options = traits), (traits = id), (id = user.id());
/* eslint-enable no-unused-expressions, no-sequences */

// clone traits before we manipulate so we don't do anything uncouth, and take
Expand Down Expand Up @@ -278,12 +284,11 @@ Analytics.prototype.user = function() {
Analytics.prototype.group = function(id, traits, options, fn) {
/* eslint-disable no-unused-expressions, no-sequences */
if (!arguments.length) return group;
if (is.fn(options)) fn = options, options = null;
if (is.fn(traits)) fn = traits, options = null, traits = null;
if (is.object(id)) options = traits, traits = id, id = group.id();
if (is.fn(options)) (fn = options), (options = null);
if (is.fn(traits)) (fn = traits), (options = null), (traits = null);
if (is.object(id)) (options = traits), (traits = id), (id = group.id());
/* eslint-enable no-unused-expressions, no-sequences */


// grab from group again to make sure we're taking from the source
group.identify(id, traits);

Expand Down Expand Up @@ -318,8 +323,9 @@ Analytics.prototype.group = function(id, traits, options, fn) {
Analytics.prototype.track = function(event, properties, options, fn) {
// Argument reshuffling.
/* eslint-disable no-unused-expressions, no-sequences */
if (is.fn(options)) fn = options, options = null;
if (is.fn(properties)) fn = properties, options = null, properties = null;
if (is.fn(options)) (fn = options), (options = null);
if (is.fn(properties))
(fn = properties), (options = null), (properties = null);
/* eslint-enable no-unused-expressions, no-sequences */

// figure out if the event is archived.
Expand Down Expand Up @@ -353,7 +359,10 @@ Analytics.prototype.track = function(event, properties, options, fn) {
}

// Add the initialize integrations so the server-side ones can be disabled too
defaults(msg.integrations, this._mergeInitializeAndPlanIntegrations(planIntegrationOptions));
defaults(
msg.integrations,
this._mergeInitializeAndPlanIntegrations(planIntegrationOptions)
);

this._invoke('track', new Track(msg));

Expand All @@ -374,7 +383,11 @@ Analytics.prototype.track = function(event, properties, options, fn) {
* @return {Analytics}
*/

Analytics.prototype.trackClick = Analytics.prototype.trackLink = function(links, event, properties) {
Analytics.prototype.trackClick = Analytics.prototype.trackLink = function(
links,
event,
properties
) {
if (!links) return this;
// always arrays, handles jquery
if (type(links) === 'element') links = [links];
Expand All @@ -387,9 +400,10 @@ Analytics.prototype.trackClick = Analytics.prototype.trackLink = function(links,
on(el, 'click', function(e) {
var ev = is.fn(event) ? event(el) : event;
var props = is.fn(properties) ? properties(el) : properties;
var href = el.getAttribute('href')
|| el.getAttributeNS('http://www.w3.org/1999/xlink', 'href')
|| el.getAttribute('xlink:href');
var href =
el.getAttribute('href') ||
el.getAttributeNS('http://www.w3.org/1999/xlink', 'href') ||
el.getAttribute('xlink:href');

self.track(ev, props);

Expand Down Expand Up @@ -417,14 +431,19 @@ Analytics.prototype.trackClick = Analytics.prototype.trackLink = function(links,
* @return {Analytics}
*/

Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(forms, event, properties) {
Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
forms,
event,
properties
) {
if (!forms) return this;
// always arrays, handles jquery
if (type(forms) === 'element') forms = [forms];

var self = this;
each(function(el) {
if (type(el) !== 'element') throw new TypeError('Must pass HTMLElement to `analytics.trackForm`.');
if (type(el) !== 'element')
throw new TypeError('Must pass HTMLElement to `analytics.trackForm`.');
function handler(e) {
prevent(e);

Expand Down Expand Up @@ -465,12 +484,15 @@ Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(forms
Analytics.prototype.page = function(category, name, properties, options, fn) {
// Argument reshuffling.
/* eslint-disable no-unused-expressions, no-sequences */
if (is.fn(options)) fn = options, options = null;
if (is.fn(properties)) fn = properties, options = properties = null;
if (is.fn(name)) fn = name, options = properties = name = null;
if (type(category) === 'object') options = name, properties = category, name = category = null;
if (type(name) === 'object') options = properties, properties = name, name = null;
if (type(category) === 'string' && type(name) !== 'string') name = category, category = null;
if (is.fn(options)) (fn = options), (options = null);
if (is.fn(properties)) (fn = properties), (options = properties = null);
if (is.fn(name)) (fn = name), (options = properties = name = null);
if (type(category) === 'object')
(options = name), (properties = category), (name = category = null);
if (type(name) === 'object')
(options = properties), (properties = name), (name = null);
if (type(category) === 'string' && type(name) !== 'string')
(name = category), (category = null);
/* eslint-enable no-unused-expressions, no-sequences */

properties = clone(properties) || {};
Expand Down Expand Up @@ -539,9 +561,9 @@ Analytics.prototype.pageview = function(url) {
Analytics.prototype.alias = function(to, from, options, fn) {
// Argument reshuffling.
/* eslint-disable no-unused-expressions, no-sequences */
if (is.fn(options)) fn = options, options = null;
if (is.fn(from)) fn = from, options = null, from = null;
if (is.object(from)) options = from, from = null;
if (is.fn(options)) (fn = options), (options = null);
if (is.fn(from)) (fn = from), (options = null), (from = null);
if (is.object(from)) (options = from), (from = null);
/* eslint-enable no-unused-expressions, no-sequences */

var msg = this.normalize({
Expand Down Expand Up @@ -660,7 +682,11 @@ Analytics.prototype._invoke = function(method, facade) {
// Check if an integration failed to initialize.
// If so, do not process the message as the integration is in an unstable state.
if (failedInitializations.indexOf(name) >= 0) {
self.log('Skipping invokation of .%s method of %s integration. Integation failed to initialize properly.', method, name);
self.log(
'Skipping invokation of .%s method of %s integration. Integation failed to initialize properly.',
method,
name
);
} else {
try {
metrics.increment('analytics_js.integration.invoke', {
Expand All @@ -673,7 +699,12 @@ Analytics.prototype._invoke = function(method, facade) {
method: method,
integration_name: integration.name
});
self.log('Error invoking .%s method of %s integration: %o', method, name, e);
self.log(
'Error invoking .%s method of %s integration: %o',
method,
name,
e
);
}
}
}, this._integrations);
Expand Down Expand Up @@ -738,13 +769,17 @@ Analytics.prototype._parseQuery = function(query) {
function pickPrefix(prefix, object) {
var length = prefix.length;
var sub;
return foldl(function(acc, val, key) {
if (key.substr(0, length) === prefix) {
sub = key.substr(length);
acc[sub] = val;
}
return acc;
}, {}, object);
return foldl(
function(acc, val, key) {
if (key.substr(0, length) === prefix) {
sub = key.substr(length);
acc[sub] = val;
}
return acc;
},
{},
object
);
}
};

Expand Down Expand Up @@ -772,7 +807,9 @@ Analytics.prototype.normalize = function(msg) {
* @param {Object} planIntegrations Tracking plan integrations.
* @return {Object} The merged integrations.
*/
Analytics.prototype._mergeInitializeAndPlanIntegrations = function(planIntegrations) {
Analytics.prototype._mergeInitializeAndPlanIntegrations = function(
planIntegrations
) {
// Do nothing if there are no initialization integrations
if (!this.options.integrations) {
return planIntegrations;
Expand Down
6 changes: 0 additions & 6 deletions lib/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ function Cookie(options) {
this.options(options);
}


/**
* Get or set the cookie options.
*
Expand Down Expand Up @@ -63,7 +62,6 @@ Cookie.prototype.options = function(options) {
this.remove('ajs:test');
};


/**
* Set a `key` and `value` in our cookie.
*
Expand All @@ -82,7 +80,6 @@ Cookie.prototype.set = function(key, value) {
}
};


/**
* Get a value from our cookie by `key`.
*
Expand All @@ -100,7 +97,6 @@ Cookie.prototype.get = function(key) {
}
};


/**
* Remove a value from our cookie by `key`.
*
Expand All @@ -117,14 +113,12 @@ Cookie.prototype.remove = function(key) {
}
};


/**
* Expose the cookie singleton.
*/

module.exports = bindAll(new Cookie());


/**
* Expose the `Cookie` constructor.
*/
Expand Down
Loading