Skip to content

Commit

Permalink
Use eslint for linting
Browse files Browse the repository at this point in the history
Summary: This PR adds dependencies on `eslint` and related tools (namely `babel-eslint` and `fbjs-scripts`) and fixes any problems reported by a run of `eslint src`.
Closes #574

Reviewed By: svcscm
  • Loading branch information
clentfort authored and cpojer committed Oct 27, 2015
1 parent 88963bf commit 24a6537
Show file tree
Hide file tree
Showing 32 changed files with 293 additions and 252 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/coverageTemplate.js
5 changes: 5 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
parser: babel-eslint

extends:
- ./node_modules/fbjs-scripts/eslint/.eslintrc
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"worker-farm": "^1.3.1"
},
"devDependencies": {
"babel-eslint": "^4.1.3",
"eslint": "^1.7.3",
"fbjs-scripts": "^0.3.0",
"jshint": "^2.8.0",
"node-find-files": "0.0.4"
},
Expand Down
4 changes: 2 additions & 2 deletions src/CoverageCollector.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ CoverageCollector.prototype.getInstrumentedSource = function(storageVarName) {
this._instrumentedSourceText = _getCoverageTemplate()({
instrumented: this._instrumentor,
coverageStorageVar: storageVarName,
source: this._instrumentor.instrument(this._origSourceText)
source: this._instrumentor.instrument(this._origSourceText),
});
}
return this._instrumentedSourceText;
Expand All @@ -49,7 +49,7 @@ CoverageCollector.prototype.extractRuntimeCoverageInfo = function() {
var coverageInfo = {
coveredSpans: [],
uncoveredSpans: [],
sourceText: this._origSourceText
sourceText: this._origSourceText,
};

var nodeIndex;
Expand Down
8 changes: 4 additions & 4 deletions src/DefaultTestReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function(config, testResult, aggregatedResults) {
}

var resultHeader = this._getResultHeader(allTestsPassed, pathStr, [
testRunTimeString
testRunTimeString,
]);

/*
Expand Down Expand Up @@ -103,7 +103,7 @@ function(config, testResult, aggregatedResults) {
};

DefaultTestReporter.prototype.onRunComplete =
function (config, aggregatedResults) {
function(config, aggregatedResults) {
var numFailedTests = aggregatedResults.numFailedTests;
var numPassedTests = aggregatedResults.numPassedTests;
var numTotalTests = aggregatedResults.numTotalTests;
Expand Down Expand Up @@ -157,14 +157,14 @@ DefaultTestReporter.prototype._formatMsg = function(msg, color) {
};

DefaultTestReporter.prototype._getResultHeader =
function (passed, testName, columns) {
function(passed, testName, columns) {
var passFailTag = passed
? this._formatMsg(' PASS ', PASS_COLOR)
: this._formatMsg(' FAIL ', FAIL_COLOR);

return [
passFailTag,
this._formatMsg(testName, TEST_NAME_COLOR)
this._formatMsg(testName, TEST_NAME_COLOR),
].concat(columns || []).join(' ');
};

Expand Down
44 changes: 22 additions & 22 deletions src/HasteModuleLoader/HasteModuleLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const NODE_CORE_MODULES = {
url: true,
util: true,
vm: true,
zlib: true
zlib: true,
};

const VENDOR_PATH = path.resolve(__dirname, '../../vendor');
Expand All @@ -86,7 +86,7 @@ function _buildLoadersList(config) {
new hasteLoaders.JSTestLoader(config.setupJSTestLoaderOptions),
new hasteLoaders.JSMockLoader(config.setupJSMockLoaderOptions),
new hasteLoaders.JSLoader(config.setupJSLoaderOptions),
new hasteLoaders.ResourceLoader()
new hasteLoaders.ResourceLoader(),
];
}

Expand Down Expand Up @@ -114,7 +114,7 @@ function _constructHasteInst(config, options) {
version: JSON.stringify(config),
useNativeFind: true,
maxProcesses: options.maxWorkers || os.cpus().length,
maxOpenFiles: options.maxOpenFiles || 100
maxOpenFiles: options.maxOpenFiles || 100,
}
);
}
Expand Down Expand Up @@ -230,7 +230,7 @@ Loader.prototype._execModule = function(moduleObj) {
__dirname: path.dirname(modulePath),
__filename: modulePath,
global: this._environment.global,
jest: this._builtInModules['jest-runtime'](modulePath).exports
jest: this._builtInModules['jest-runtime'](modulePath).exports,
};

var onlyCollectFrom = this._config.collectCoverageOnlyFrom;
Expand Down Expand Up @@ -315,7 +315,7 @@ Loader.prototype._getDependencyPathsFromResource = function(resource) {

try {
var moduleID = this._getNormalizedModuleID(resource.path, requiredModule);
} catch(e) {
} catch (e) {
continue;
}

Expand Down Expand Up @@ -506,20 +506,20 @@ Loader.prototype._nodeModuleNameToPath = function(currPath, moduleName) {

var resolveError = null;
var exts = this._config.moduleFileExtensions
.map(function(ext){
.map(function(ext) {
return '.' + ext;
});
try {
if (NODE_PATH) {
return resolve.sync(moduleName, {
paths: NODE_PATH.split(path.delimiter),
basedir: path.dirname(currPath),
extensions: exts
extensions: exts,
});
} else {
return resolve.sync(moduleName, {
basedir: path.dirname(currPath),
extensions: exts
extensions: exts,
});
}
} catch (e) {
Expand Down Expand Up @@ -599,7 +599,7 @@ Loader.prototype._shouldMock = function(currPath, moduleName) {
this._getResource('JSMock', moduleName);
try {
var modulePath = this._moduleNameToPath(currPath, moduleName);
} catch(e) {
} catch (e) {
// If there isn't a real module, we don't have a path to match
// against the unmockList regexps. If there is also not a manual
// mock, then we throw because this module doesn't exist anywhere.
Expand Down Expand Up @@ -813,7 +813,7 @@ Loader.prototype.requireMock = function(currPath, moduleName) {
if (manualMockResource) {
var moduleObj = {
exports: {},
__filename: modulePath
__filename: modulePath,
};
this._execModule(moduleObj);
this._mockRegistry[modulePath] = moduleObj.exports;
Expand Down Expand Up @@ -906,7 +906,7 @@ Loader.prototype.requireModule = function(currPath, moduleName,
// be satisfied.
moduleObj = {
__filename: modulePath,
exports: {}
exports: {},
};

if (!bypassRegistryCache) {
Expand All @@ -919,7 +919,7 @@ Loader.prototype.requireModule = function(currPath, moduleName,
modulePath,
'utf8'
));
} else if(path.extname(modulePath) === '.node') {
} else if (path.extname(modulePath) === '.node') {
// Just do a require if it is a native node module
moduleObj.exports = require(modulePath);
} else {
Expand All @@ -939,7 +939,7 @@ Loader.prototype.requireModule = function(currPath, moduleName,
*/
Loader.prototype._resolveStubModuleName = function(moduleName) {
var nameMapper = this._mappedModuleNames;
for (let mappedModuleName in nameMapper) {
for (const mappedModuleName in nameMapper) {
var regex = nameMapper[mappedModuleName];
if (regex.test(moduleName)) {
return mappedModuleName;
Expand All @@ -966,7 +966,7 @@ Loader.prototype.requireModuleOrMock = function(currPath, moduleName) {
};

Loader.prototype.getJestRuntime = function(dir) {
return this._builtInModules['jest-runtime'](dir).exports;
return this._builtInModules['jest-runtime'](dir).exports;
};

/**
Expand Down Expand Up @@ -1085,8 +1085,8 @@ Loader.prototype.resetModuleRegistry = function() {

useRealTimers: () => {
this._environment.fakeTimers.useRealTimers();
}
}
},
},
};

// This is a pretty common API to use in many tests, so this is just a
Expand All @@ -1101,8 +1101,8 @@ Loader.prototype.resetModuleRegistry = function() {
'node-haste': () => ({
exports: {
// Do not use this API -- it is deprecated and will go away very soon!
getResourceMap: () => this._resourceMap
}
getResourceMap: () => this._resourceMap,
},
}),

// This is a legacy API that will soon be deprecated.
Expand All @@ -1116,7 +1116,7 @@ Loader.prototype.resetModuleRegistry = function() {
currPath,
'jest-runtime'
).genMockFn(),
}
},
};
mocks.exports.getMockFn = mocks.exports.getMockFunction;
return mocks;
Expand Down Expand Up @@ -1221,11 +1221,11 @@ Loader.prototype.resetModuleRegistry = function() {
this._currentlyExecutingModulePath,
moduleName,
true // yay boolean args!
)
}
),
},
};
return mockModules;
}
},
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('HasteModuleLoader', function() {

var CONFIG = utils.normalizeConfig({
name: 'HasteModuleLoader-tests',
rootDir: path.resolve(__dirname, 'test_root')
rootDir: path.resolve(__dirname, 'test_root'),
});

function buildLoader() {
Expand Down Expand Up @@ -69,12 +69,15 @@ describe('HasteModuleLoader', function() {
initHasteModuleLoader(nodePath);
return buildLoader().then(function(loader) {
try {
var exports = loader.requireModuleOrMock(null,
'RegularModuleInNodePath');
expect(exports).toBeUndefined();
var exports = loader.requireModuleOrMock(
null,
'RegularModuleInNodePath'
);
expect(exports).toBeUndefined();
} catch (e) {
expect(
(e.message.indexOf('Cannot find module'))).toBeGreaterThan(-1);
(e.message.indexOf('Cannot find module'))
).toBeGreaterThan(-1);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
jest.autoMockOff();

describe('nodeHasteModuleLoader', function() {
describe('currentTestPath', function() {
it('makes the current test path available', function() {
expect(jest.currentTestPath()).toMatch(/currentTestPath-test/);
});
describe('currentTestPath', function() {
it('makes the current test path available', function() {
expect(jest.currentTestPath()).toMatch(/currentTestPath-test/);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('nodeHasteModuleLoader', function() {

var CONFIG = utils.normalizeConfig({
name: 'nodeHasteModuleLoader-tests',
rootDir: path.resolve(__dirname, 'test_root')
rootDir: path.resolve(__dirname, 'test_root'),
});

function buildLoader() {
Expand All @@ -43,21 +43,28 @@ describe('nodeHasteModuleLoader', function() {
});

describe('genMockFromModule', function() {
pit('does not cause side effects in the rest of the module system when ' +
'generating a mock', function() {
return buildLoader().then(function(loader) {
var testRequire = loader.requireModule.bind(loader, __filename);
pit(
'does not cause side effects in the rest of the module system when ' +
'generating a mock',
function() {
return buildLoader().then(function(loader) {
var testRequire = loader.requireModule.bind(loader, __filename);

var regularModule = testRequire('RegularModule');
var origModuleStateValue = regularModule.getModuleStateValue();
var regularModule = testRequire('RegularModule');
var origModuleStateValue = regularModule.getModuleStateValue();

testRequire('jest-runtime').dontMock('RegularModule');
testRequire('jest-runtime').dontMock('RegularModule');

// Generate a mock for a module with side effects
testRequire('jest-runtime').genMockFromModule('ModuleWithSideEffects');
// Generate a mock for a module with side effects
testRequire('jest-runtime').genMockFromModule(
'ModuleWithSideEffects'
);

expect(regularModule.getModuleStateValue()).toBe(origModuleStateValue);
});
});
expect(regularModule.getModuleStateValue()).toBe(
origModuleStateValue
);
});
}
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('nodeHasteModuleLoader', function() {
return undefined;
}
return resources[name];
}
},
};

function _generateResource(name, deps) {
Expand All @@ -35,7 +35,7 @@ describe('nodeHasteModuleLoader', function() {
id: name,
_requiredModuleMap: deps.reduce(function(prev, next) {
return prev[next] = true;
}, {})
}, {}),
};
if (deps.length) {
resource.requiredModules = deps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('HasteModuleLoader', function() {

var CONFIG = utils.normalizeConfig({
name: 'HasteModuleLoader-tests',
rootDir: path.resolve(__dirname, 'test_root')
rootDir: path.resolve(__dirname, 'test_root'),
});

function buildLoader() {
Expand Down
Loading

0 comments on commit 24a6537

Please sign in to comment.