Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new rule: no restricted export names
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* @fileoverview Restrict usage of specified export names. | ||
* @author Bradley Farias | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const astUtils = require("../ast-utils"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "disallow usage of specified names when using `export`.", | ||
category: "ECMAScript 6", | ||
recommended: true | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
const names = new Set([ | ||
"toString", | ||
"valueOf", | ||
"constructor", | ||
"then" | ||
]); | ||
|
||
/** | ||
* Checks and reports if a name was restricted. | ||
* @param {string} name The binding name that is being exported. | ||
* @param {ASTNode} node The node that provided the name. | ||
* @returns {void} | ||
*/ | ||
function checkRestrictedName(name, node) { | ||
if (names.has(name)) { | ||
context.report({ | ||
node, | ||
message: "export '{{name}}' is restricted from being used.", | ||
data: { | ||
name | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Determines whether an export declaration is restricted | ||
* @param {ASTNode} node The export declaration node. | ||
* @returns {void} | ||
*/ | ||
function validate(node) { | ||
if (node.declaration) { | ||
node.declaration.declarations.forEach(function (declarator) { | ||
checkRestrictedName(declarator.id.name, node); | ||
}); | ||
} | ||
else { | ||
node.specifiers.forEach(function (specifier) { | ||
const name = specifier.exported ? specifier.exported.name : specifier.local.name; | ||
checkRestrictedName(name, specifier); | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
"ExportNamedDeclaration:exit": validate, | ||
}; | ||
} | ||
}; |
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,44 @@ | ||
/** | ||
* @fileoverview Tests for no-restricted-imports. | ||
* @author Guy Ellis | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/no-restricted-exports"), | ||
RuleTester = require("../../../lib/testers/rule-tester"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ parserOptions: { sourceType: "module" } }); | ||
|
||
ruleTester.run("no-restricted-exports", rule, { | ||
valid: [ | ||
{ code: "export let a;", options: [] }, | ||
{ code: "let a;export {then as a};", options: [] }, | ||
{ code: "export {then as a} from '';", options: [] }, | ||
], | ||
invalid: [{ | ||
code: "export let then;", | ||
options: [], | ||
errors: [{ message: "export 'then' is restricted from being used.", type: "ExportNamedDeclaration" }] | ||
},{ | ||
code: "let toString;export {toString};", | ||
options: [], | ||
errors: [{ message: "export 'toString' is restricted from being used.", type: "ExportSpecifier" }] | ||
},{ | ||
code: "let foo;export {foo as valueOf};", | ||
options: [], | ||
errors: [{ message: "export 'valueOf' is restricted from being used.", type: "ExportSpecifier" }] | ||
},{ | ||
code: "export {constructor} from '';", | ||
options: [], | ||
errors: [{ message: "export 'constructor' is restricted from being used.", type: "ExportSpecifier" }] | ||
}] | ||
}); |