forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add eslint plugin for checking imports (prebid#3976)
- Loading branch information
Showing
6 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters