google_forms/node_modules/eslint/lib/rules/no-restricted-exports.js

194 lines
7.1 KiB
JavaScript
Raw Permalink Normal View History

2024-08-09 12:04:48 +00:00
/**
* @fileoverview Rule to disallow specified names in exports
* @author Milos Djermanovic
*/
"use strict";
2024-08-21 06:34:30 +00:00
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("./utils/ast-utils");
2024-08-09 12:04:48 +00:00
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
2024-08-21 06:34:30 +00:00
/** @type {import('../shared/types').Rule} */
2024-08-09 12:04:48 +00:00
module.exports = {
meta: {
type: "suggestion",
docs: {
2024-08-21 06:34:30 +00:00
description: "Disallow specified names in exports",
2024-08-09 12:04:48 +00:00
recommended: false,
2024-08-21 06:34:30 +00:00
url: "https://eslint.org/docs/latest/rules/no-restricted-exports"
2024-08-09 12:04:48 +00:00
},
schema: [{
2024-08-21 06:34:30 +00:00
anyOf: [
{
type: "object",
properties: {
restrictedNamedExports: {
type: "array",
items: {
type: "string"
},
uniqueItems: true
}
},
additionalProperties: false
},
{
type: "object",
properties: {
restrictedNamedExports: {
type: "array",
items: {
type: "string",
pattern: "^(?!default$)"
},
uniqueItems: true
},
restrictDefaultExports: {
type: "object",
properties: {
// Allow/Disallow `export default foo; export default 42; export default function foo() {}` format
direct: {
type: "boolean"
},
// Allow/Disallow `export { foo as default };` declarations
named: {
type: "boolean"
},
// Allow/Disallow `export { default } from "mod"; export { default as default } from "mod";` declarations
defaultFrom: {
type: "boolean"
},
// Allow/Disallow `export { foo as default } from "mod";` declarations
namedFrom: {
type: "boolean"
},
// Allow/Disallow `export * as default from "mod"`; declarations
namespaceFrom: {
type: "boolean"
}
},
additionalProperties: false
}
2024-08-09 12:04:48 +00:00
},
2024-08-21 06:34:30 +00:00
additionalProperties: false
2024-08-09 12:04:48 +00:00
}
2024-08-21 06:34:30 +00:00
]
2024-08-09 12:04:48 +00:00
}],
messages: {
2024-08-21 06:34:30 +00:00
restrictedNamed: "'{{name}}' is restricted from being used as an exported name.",
restrictedDefault: "Exporting 'default' is restricted."
2024-08-09 12:04:48 +00:00
}
},
create(context) {
const restrictedNames = new Set(context.options[0] && context.options[0].restrictedNamedExports);
2024-08-21 06:34:30 +00:00
const restrictDefaultExports = context.options[0] && context.options[0].restrictDefaultExports;
const sourceCode = context.sourceCode;
2024-08-09 12:04:48 +00:00
/**
2024-08-21 06:34:30 +00:00
* Checks and reports given exported name.
* @param {ASTNode} node exported `Identifier` or string `Literal` node to check.
2024-08-09 12:04:48 +00:00
* @returns {void}
*/
function checkExportedName(node) {
2024-08-21 06:34:30 +00:00
const name = astUtils.getModuleExportName(node);
2024-08-09 12:04:48 +00:00
if (restrictedNames.has(name)) {
context.report({
node,
messageId: "restrictedNamed",
data: { name }
});
2024-08-21 06:34:30 +00:00
return;
}
if (name === "default") {
if (node.parent.type === "ExportAllDeclaration") {
if (restrictDefaultExports && restrictDefaultExports.namespaceFrom) {
context.report({
node,
messageId: "restrictedDefault"
});
}
} else { // ExportSpecifier
const isSourceSpecified = !!node.parent.parent.source;
const specifierLocalName = astUtils.getModuleExportName(node.parent.local);
if (!isSourceSpecified && restrictDefaultExports && restrictDefaultExports.named) {
context.report({
node,
messageId: "restrictedDefault"
});
return;
}
if (isSourceSpecified && restrictDefaultExports) {
if (
(specifierLocalName === "default" && restrictDefaultExports.defaultFrom) ||
(specifierLocalName !== "default" && restrictDefaultExports.namedFrom)
) {
context.report({
node,
messageId: "restrictedDefault"
});
}
}
}
2024-08-09 12:04:48 +00:00
}
}
return {
ExportAllDeclaration(node) {
if (node.exported) {
checkExportedName(node.exported);
}
},
2024-08-21 06:34:30 +00:00
ExportDefaultDeclaration(node) {
if (restrictDefaultExports && restrictDefaultExports.direct) {
context.report({
node,
messageId: "restrictedDefault"
});
}
},
2024-08-09 12:04:48 +00:00
ExportNamedDeclaration(node) {
const declaration = node.declaration;
if (declaration) {
if (declaration.type === "FunctionDeclaration" || declaration.type === "ClassDeclaration") {
checkExportedName(declaration.id);
} else if (declaration.type === "VariableDeclaration") {
2024-08-21 06:34:30 +00:00
sourceCode.getDeclaredVariables(declaration)
2024-08-09 12:04:48 +00:00
.map(v => v.defs.find(d => d.parent === declaration))
.map(d => d.name) // Identifier nodes
.forEach(checkExportedName);
}
} else {
node.specifiers
.map(s => s.exported)
.forEach(checkExportedName);
}
}
};
}
};