kibana/packages/kbn-eslint-plugin-eslint/rules/no_export_all.js
Spencer fecdba7eba
[eslint] add rule to prevent export* in plugin index files (#109357)
* [eslint] add rule to prevent export* in plugin index files

* deduplicate export names for types/instances with the same name

* attempt to auto-fix duplicate exports too

* capture exported enums too

* enforce no_export_all for core too

* disable rule by default, allow opting-in for help fixing

* update tests

* reduce yarn.lock duplication

* add rule but no fixes

* disable all existing violations

* update api docs with new line numbers

* revert unnecessary changes to yarn.lock which only had drawbacks

* remove unnecessary eslint-disable

* rework codegen to split type exports and use babel to generate valid code

* check for "export types" deeply

* improve test by using fixtures

* add comments to some helper functions

* disable fix for namespace exports including types

* label all eslint-disable comments with related team-specific issue

* ensure that child exports of `export type` are always tracked as types

Co-authored-by: spalger <spalger@users.noreply.github.com>
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
2021-09-01 18:05:45 -07:00

84 lines
3.1 KiB
JavaScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
const Fs = require('fs');
const ts = require('typescript');
const { getExportCode, getExportNamedNamespaceCode } = require('../helpers/codegen');
const tsEstree = require('@typescript-eslint/typescript-estree');
const { getExportNamesDeep } = require('../helpers/exports');
/** @typedef {import("eslint").Rule.RuleModule} Rule */
/** @typedef {import("@typescript-eslint/parser").ParserServices} ParserServices */
/** @typedef {import("@typescript-eslint/typescript-estree").TSESTree.ExportAllDeclaration} EsTreeExportAllDeclaration */
/** @typedef {import("@typescript-eslint/typescript-estree").TSESTree.StringLiteral} EsTreeStringLiteral */
/** @typedef {import("typescript").ExportDeclaration} ExportDeclaration */
/** @typedef {import("../helpers/exports").Parser} Parser */
/** @typedef {import("eslint").Rule.RuleFixer} Fixer */
const ERROR_MSG =
'`export *` is not allowed in the index files of plugins to prevent accidentally exporting too many APIs';
/** @type {Rule} */
module.exports = {
meta: {
fixable: 'code',
schema: [],
},
create: (context) => {
return {
ExportAllDeclaration(node) {
const services = /** @type ParserServices */ (context.parserServices);
const esNode = /** @type EsTreeExportAllDeclaration */ (node);
const tsnode = /** @type ExportDeclaration */ (services.esTreeNodeToTSNodeMap.get(esNode));
/** @type Parser */
const parser = (path) => {
const code = Fs.readFileSync(path, 'utf-8');
const result = tsEstree.parseAndGenerateServices(code, {
...context.parserOptions,
comment: false,
filePath: path,
});
return result.services.program.getSourceFile(path);
};
const exportSet = getExportNamesDeep(parser, context.getFilename(), tsnode);
const isTypeExport = esNode.exportKind === 'type';
const isNamespaceExportWithTypes =
tsnode.exportClause &&
ts.isNamespaceExport(tsnode.exportClause) &&
(isTypeExport || exportSet.types.size);
/** @param {Fixer} fixer */
const fix = (fixer) => {
const source = /** @type EsTreeStringLiteral */ (esNode.source);
if (tsnode.exportClause && ts.isNamespaceExport(tsnode.exportClause)) {
return fixer.replaceText(
node,
getExportNamedNamespaceCode(
tsnode.exportClause.name.getText(),
Array.from(exportSet.values),
source.value
)
);
}
return fixer.replaceText(node, getExportCode(exportSet, source.value));
};
context.report({
message: ERROR_MSG,
loc: node.loc,
fix: exportSet?.size && !isNamespaceExportWithTypes ? fix : undefined,
});
},
};
},
};