Skip to content

Commit

Permalink
add eslint plugin for checking imports (prebid#3976)
Browse files Browse the repository at this point in the history
  • Loading branch information
snapwich authored and Alex committed Aug 1, 2019
1 parent bf28c08 commit 0ea491b
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 4 deletions.
34 changes: 33 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@

const sharedWhiteList = [
"core-js/library/fn/array/find", // no ie11
"core-js/library/fn/array/includes", // no ie11
"core-js/library/fn/set", // ie11 supports Set but not Set#values
"core-js/library/fn/string/includes", // no ie11
"core-js/library/fn/number/is-integer", // no ie11,
"core-js/library/fn/array/from" // no ie11
];

module.exports = {
"env": {
"browser": true,
Expand All @@ -11,6 +21,9 @@ module.exports = {
}
},
"extends": "standard",
"plugins": [
"prebid"
],
"globals": {
"$$PREBID_GLOBAL$$": false
},
Expand All @@ -31,5 +44,24 @@ module.exports = {
"no-throw-literal": "off",
"no-undef": "off",
"no-useless-escape": "off",
}
},
"overrides": [{
"files": "modules/**/*.js",
"rules": {
"prebid/validate-imports": ["error", [
...sharedWhiteList,
"jsencrypt",
"crypto-js"
]]
}
}, {
"files": "src/**/*.js",
"rules": {
"prebid/validate-imports": ["error", [
...sharedWhiteList,
"fun-hooks/no-eval",
"just-clone"
]]
}
}]
};
3 changes: 1 addition & 2 deletions modules/ixBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as utils from '../src/utils';
import { BANNER } from '../src/mediaTypes';
import { config } from '../src/config';
import isArray from 'core-js/library/fn/array/is-array';
import isInteger from 'core-js/library/fn/number/is-integer';
import { registerBidder } from '../src/adapters/bidderFactory';

Expand Down Expand Up @@ -92,7 +91,7 @@ function parseBid(rawBid, currency) {
* @return {boolean} True if this is a valid size format, and false otherwise.
*/
function isValidSize(size) {
return isArray(size) && size.length === 2 && isInteger(size[0]) && isInteger(size[1]);
return Array.isArray(size) && size.length === 2 && isInteger(size[0]) && isInteger(size[1]);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"eslint-plugin-node": "^5.1.0",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1",
"eslint-plugin-prebid": "file:./plugins/eslint",
"execa": "^1.0.0",
"faker": "^3.1.0",
"fs.extra": "^1.3.2",
Expand Down Expand Up @@ -79,6 +80,7 @@
"mocha": "^5.0.0",
"opn": "^5.4.0",
"querystringify": "0.0.3",
"resolve-from": "^5.0.0",
"sinon": "^4.1.3",
"through2": "^2.0.3",
"url-parse": "^1.0.5",
Expand Down
8 changes: 8 additions & 0 deletions plugins/eslint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "eslint-plugin-prebid",
"version": "1.0.0",
"description": "validates module imports can be found without custom webpack resolvers, are in module whitelist, and not module entry points",
"main": "validateImports.js",
"author": "the prebid.js contributors",
"license": "Apache-2.0"
}
61 changes: 61 additions & 0 deletions plugins/eslint/validateImports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

let path = require('path');
let _ = require('lodash');
let resolveFrom = require('resolve-from');

function flagErrors(context, node, importPath) {
let absFileDir = path.dirname(context.getFilename());
let absImportPath = path.resolve(absFileDir, importPath);

try {
resolveFrom(absFileDir, importPath);
} catch (e) {
return context.report(node, `import "${importPath}" cannot be resolved`);
}

if (
Array.isArray(_.get(context, ['options', 0])) &&
importPath.match(/^\w+/) &&
!context.options[0].some(name => importPath.startsWith(name))
) {
context.report(node, `import "${importPath}" not in import whitelist`);
} else {
let absModulePath = path.resolve(__dirname, '../../modules');

// don't allow import of any files directly within modules folder or index.js files within modules' sub-folders
if (
path.dirname(absImportPath) === absModulePath || (
absImportPath.startsWith(absModulePath) &&
path.basename(absImportPath) === 'index.js'
)
) {
context.report(node, `import "${importPath}" cannot require module entry point`);
}
}
}

module.exports = {
rules: {
'validate-imports': {
meta: {
docs: {
description: 'validates module imports can be found without custom webpack resolvers, are in module whitelist, and not module entry points'
}
},
create: function(context) {
return {
"CallExpression[callee.name='require']"(node) {
let importPath = _.get(node, ['arguments', 0, 'value']);
if (importPath) {
flagErrors(context, node, importPath);
}
},
ImportDeclaration(node) {
let importPath = node.source.value.trim();
flagErrors(context, node, importPath);
}
}
}
}
}
};
2 changes: 1 addition & 1 deletion src/adapters/analytics/example2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ajax } from 'src/ajax';
import { ajax } from '../../../src/ajax';

/**
* example2.js - analytics adapter for Example2 Analytics Endpoint example
Expand Down

0 comments on commit 0ea491b

Please sign in to comment.