2019-11-27 22:44:31 +01:00
|
|
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/experimental-utils";
|
|
|
|
import { createRule } from "./utils";
|
|
|
|
|
|
|
|
export = createRule({
|
|
|
|
name: "one-namespace-per-file",
|
|
|
|
meta: {
|
|
|
|
docs: {
|
|
|
|
description: `Limits each file to having at most one top-level namespace declaration`,
|
|
|
|
category: "Possible Errors",
|
|
|
|
recommended: "error",
|
|
|
|
},
|
|
|
|
messages: {
|
2020-01-30 22:39:54 +01:00
|
|
|
excessNamespaceError: `All but one of these namespaces should be moved into separate files.`,
|
2019-11-27 22:44:31 +01:00
|
|
|
},
|
|
|
|
schema: [],
|
|
|
|
type: "problem",
|
|
|
|
},
|
|
|
|
defaultOptions: [],
|
|
|
|
|
|
|
|
create(context) {
|
|
|
|
const isNamespaceDeclaration = (node: TSESTree.Node): node is TSESTree.TSModuleDeclaration => node.type === AST_NODE_TYPES.TSModuleDeclaration;
|
|
|
|
|
|
|
|
const checkSourceFile = (node: TSESTree.Program) => {
|
|
|
|
if (context.getFilename().endsWith(".d.ts")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const members = node.body;
|
|
|
|
const namespaces = members.filter(isNamespaceDeclaration);
|
|
|
|
if (namespaces.length <= 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespaces.forEach(n => {
|
|
|
|
context.report({
|
|
|
|
messageId: "excessNamespaceError", node: n
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
Program: checkSourceFile,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|