Skip to content

feat: Updated lint configuration for optimizely-sdk package #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 18, 2019
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
48 changes: 0 additions & 48 deletions .eslintrc.js

This file was deleted.

4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ after_success: npm run coveralls

# Integration tests need to run first to reset the PR build status to pending
stages:
- 'Lint'
- 'Integration tests'
- 'Benchmarking tests'
- 'Cross-browser and umd unit tests'
- 'Test'

jobs:
include:
- stage: 'Lint'
node_js: '12'
script: npm run lint
- &integrationtest
stage: 'Integration tests'
merge_mode: replace
Expand Down
3 changes: 3 additions & 0 deletions packages/optimizely-sdk/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.tests.js
*.umdtests.js
test_data.js
19 changes: 19 additions & 0 deletions packages/optimizely-sdk/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
env: {
browser: true,
commonjs: true,
node: true,
},
extends: 'eslint:recommended',
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
Promise: 'readonly',
},
parserOptions: {
ecmaVersion: 5,
},
rules: {
'no-prototype-builtins': 'off',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ var LOG_LEVEL = enums.LOG_LEVEL;
var LOG_MESSAGES = enums.LOG_MESSAGES;
var MODULE_NAME = 'CUSTOM_ATTRIBUTE_CONDITION_EVALUATOR';

var CUSTOM_ATTRIBUTE_CONDITION_TYPE = 'custom_attribute';

var EXACT_MATCH_TYPE = 'exact';
var EXISTS_MATCH_TYPE = 'exists';
var GREATER_THAN_MATCH_TYPE = 'gt';
Expand Down
10 changes: 5 additions & 5 deletions packages/optimizely-sdk/lib/core/decision_service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,24 @@ DecisionService.prototype.getVariation = function(configObj, experimentKey, user
// by default, the bucketing ID should be the user ID
var bucketingId = this._getBucketingId(userId, attributes);

if (!this.__checkIfExperimentIsActive(configObj, experimentKey, userId)) {
if (!this.__checkIfExperimentIsActive(configObj, experimentKey)) {
return null;
}
var experiment = configObj.experimentKeyMap[experimentKey];
var forcedVariationKey = this.getForcedVariation(configObj, experimentKey, userId);
if (!!forcedVariationKey) {
if (forcedVariationKey) {
return forcedVariationKey;
}

var variation = this.__getWhitelistedVariation(experiment, userId);
if (!!variation) {
if (variation) {
return variation.key;
}

// check for sticky bucketing
var experimentBucketMap = this.__resolveExperimentBucketMap(userId, attributes);
variation = this.__getStoredVariation(configObj, experiment, userId, experimentBucketMap);
if (!!variation) {
if (variation) {
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.RETURNING_STORED_VARIATION, MODULE_NAME, variation.key, experimentKey, userId));
return variation.key;
}
Expand Down Expand Up @@ -127,7 +127,7 @@ DecisionService.prototype.__resolveExperimentBucketMap = function(userId, attrib
* @param {string} userId ID of user
* @return {boolean} True if experiment is running
*/
DecisionService.prototype.__checkIfExperimentIsActive = function(configObj, experimentKey, userId) {
DecisionService.prototype.__checkIfExperimentIsActive = function(configObj, experimentKey) {
if (!projectConfig.isActive(configObj, experimentKey)) {
var experimentNotRunningLogMessage = sprintf(LOG_MESSAGES.EXPERIMENT_NOT_RUNNING, MODULE_NAME, experimentKey);
this.logger.log(LOG_LEVEL.INFO, experimentNotRunningLogMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,12 @@ describe('lib/core/decision_service', function() {

describe('__checkIfExperimentIsActive', function () {
it('should return true if experiment is running', function () {
assert.isTrue(decisionServiceInstance.__checkIfExperimentIsActive(configObj, 'testExperiment', 'testUser'));
assert.isTrue(decisionServiceInstance.__checkIfExperimentIsActive(configObj, 'testExperiment'));
sinon.assert.notCalled(mockLogger.log);
});

it('should return false when experiment is not running', function () {
assert.isFalse(decisionServiceInstance.__checkIfExperimentIsActive(configObj, 'testExperimentNotRunning', 'testUser'));
assert.isFalse(decisionServiceInstance.__checkIfExperimentIsActive(configObj, 'testExperimentNotRunning'));
sinon.assert.calledOnce(mockLogger.log);
var logMessage = mockLogger.log.args[0][1];
assert.strictEqual(logMessage, sprintf(LOG_MESSAGES.EXPERIMENT_NOT_RUNNING, 'DECISION_SERVICE', 'testExperimentNotRunning'));
Expand Down
6 changes: 3 additions & 3 deletions packages/optimizely-sdk/lib/core/optimizely_config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function getMergedVariablesMap(configObj, variation, experimentId, featureVariab
var featureId = configObj.experimentFeatureMap[experimentId];
var variablesObject = {};
if (featureId) {
experimentFeatureVariables = featureVariablesMap[featureId];
var experimentFeatureVariables = featureVariablesMap[featureId];
// Temporary variation variables map to get values to merge.
var tempVariablesIdMap = variation.variables.reduce(function(variablesMap, variable) {
variablesMap[variable.id] = {
Expand All @@ -67,8 +67,8 @@ function getMergedVariablesMap(configObj, variation, experimentId, featureVariab
return variablesMap;
}, {});
variablesObject = experimentFeatureVariables.reduce(function(variablesMap, featureVariable) {
variationVariable = tempVariablesIdMap[featureVariable.id];
variableValue = variation.featureEnabled && variationVariable ? variationVariable.value : featureVariable.defaultValue;
var variationVariable = tempVariablesIdMap[featureVariable.id];
var variableValue = variation.featureEnabled && variationVariable ? variationVariable.value : featureVariable.defaultValue;
variablesMap[featureVariable.key] = {
id: featureVariable.id,
key: featureVariable.key,
Expand Down
6 changes: 3 additions & 3 deletions packages/optimizely-sdk/lib/core/project_config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ module.exports = {
getExperimentFromKey: function(projectConfig, experimentKey) {
if (projectConfig.experimentKeyMap.hasOwnProperty(experimentKey)) {
var experiment = projectConfig.experimentKeyMap[experimentKey];
if (!!experiment) {
if (experiment) {
return experiment;
}
}
Expand Down Expand Up @@ -304,7 +304,7 @@ module.exports = {
getExperimentFromId: function(projectConfig, experimentId, logger) {
if (projectConfig.experimentIdMap.hasOwnProperty(experimentId)) {
var experiment = projectConfig.experimentIdMap[experimentId];
if (!!experiment) {
if (experiment) {
return experiment;
}
}
Expand All @@ -325,7 +325,7 @@ module.exports = {
getFeatureFromKey: function(projectConfig, featureKey, logger) {
if (projectConfig.featureKeyMap.hasOwnProperty(featureKey)) {
var feature = projectConfig.featureKeyMap[featureKey];
if (!!feature) {
if (feature) {
return feature;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/optimizely/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ Optimizely.prototype.isFeatureEnabled = function(featureKey, userId, attributes)
var variation = decision.variation;
var sourceInfo = {};

if (!!variation) {
if (variation) {
featureEnabled = variation.featureEnabled;
if (decision.decisionSource === DECISION_SOURCES.FEATURE_TEST) {
sourceInfo = {
Expand Down
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/plugins/error_handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
* Handle given exception
* @param {Object} exception An exception object
*/
handleError: function(exception) {
handleError: function() {
// no-op
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ module.exports = {
dispatchEvent: function(eventObj, callback) {
var url = eventObj.url;
var params = eventObj.params;
var req;
if (eventObj.httpVerb === POST_METHOD) {
var req = new XMLHttpRequest();
req = new XMLHttpRequest();
req.open(POST_METHOD, url, true);
req.setRequestHeader('Content-Type', 'application/json');
req.onreadystatechange = function() {
Expand All @@ -50,7 +51,7 @@ module.exports = {
url += '&' + toQueryString(params);
}

var req = new XMLHttpRequest();
req = new XMLHttpRequest();
req.open(GET_METHOD, url, true);
req.onreadystatechange = function() {
if (req.readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ module.exports = {
}

var parsedUrl = url.parse(eventObj.url);
var path = parsedUrl.path;
if (parsedUrl.query) {
path += '?' + parsedUrl.query;
}

var dataString = JSON.stringify(eventObj.params);

Expand Down
25 changes: 13 additions & 12 deletions packages/optimizely-sdk/lib/plugins/logger/index.react_native.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,30 @@
* limitations under the License.
*/
var LogLevel = require('@optimizely/js-sdk-logging').LogLevel;
var sprintf = require('@optimizely/js-sdk-utils').sprintf;

function getLogLevelName(level) {
switch(level) {
case LogLevel.INFO: return 'INFO';
case LogLevel.ERROR: return 'ERROR';
case LogLevel.WARNING: return 'WARNING';
case LogLevel.DEBUG: return 'DEBUG';
default: return 'NOTSET';
default: return 'NOTSET';
}
}

class ReactNativeLogger {
log(level, message) {
const formattedMessage = `[OPTIMIZELY] - ${getLogLevelName(level)} ${new Date().toISOString()} ${message}`;
switch (level) {
case LogLevel.INFO: console.info(formattedMessage); break;
case LogLevel.ERROR:
case LogLevel.WARNING: console.warn(formattedMessage); break;
case LogLevel.DEBUG:
case LogLevel.NOTSET: console.log(formattedMessage); break;
}
function ReactNativeLogger() {}

ReactNativeLogger.prototype.log = function(level, message) {
var formattedMessage = sprintf('[OPTIMIZELY] - %s %s %s', getLogLevelName(level), new Date().toISOString(), message);
switch (level) {
case LogLevel.INFO: console.info(formattedMessage); break;
case LogLevel.ERROR:
case LogLevel.WARNING: console.warn(formattedMessage); break;
case LogLevel.DEBUG:
case LogLevel.NOTSET: console.log(formattedMessage); break;
}
}
};

function NoOpLogger() {}

Expand Down
Loading