mirror of
https://github.com/elastic/kibana.git
synced 2025-06-28 19:13:14 -04:00
[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>
This commit is contained in:
parent
4f0a63f575
commit
fecdba7eba
89 changed files with 792 additions and 4 deletions
82
packages/kbn-eslint-plugin-eslint/helpers/codegen.js
Normal file
82
packages/kbn-eslint-plugin-eslint/helpers/codegen.js
Normal file
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* 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 t = require('@babel/types');
|
||||
const { default: generate } = require('@babel/generator');
|
||||
|
||||
/** @typedef {import('./export_set').ExportSet} ExportSet */
|
||||
|
||||
/**
|
||||
* Generate code for replacing a `export * from './path'`, ie.
|
||||
*
|
||||
* export type { foo } from './path'
|
||||
* export { bar } from './path'
|
||||
|
||||
* @param {ExportSet} exportSet
|
||||
* @param {string} source
|
||||
*/
|
||||
const getExportCode = (exportSet, source) => {
|
||||
const exportedTypes = exportSet.types.size
|
||||
? t.exportNamedDeclaration(
|
||||
undefined,
|
||||
Array.from(exportSet.types).map((n) => t.exportSpecifier(t.identifier(n), t.identifier(n))),
|
||||
t.stringLiteral(source)
|
||||
)
|
||||
: undefined;
|
||||
|
||||
if (exportedTypes) {
|
||||
exportedTypes.exportKind = 'type';
|
||||
}
|
||||
|
||||
const exportedValues = exportSet.values.size
|
||||
? t.exportNamedDeclaration(
|
||||
undefined,
|
||||
Array.from(exportSet.values).map((n) =>
|
||||
t.exportSpecifier(t.identifier(n), t.identifier(n))
|
||||
),
|
||||
t.stringLiteral(source)
|
||||
)
|
||||
: undefined;
|
||||
|
||||
return generate(t.program([exportedTypes, exportedValues].filter(Boolean))).code;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generate code for replacing a `export * as name from './path'`, ie.
|
||||
*
|
||||
* import { foo, bar } from './path'
|
||||
* export const name = { foo, bar }
|
||||
*
|
||||
* @param {string} nsName
|
||||
* @param {string[]} exportNames
|
||||
* @param {string} source
|
||||
*/
|
||||
const getExportNamedNamespaceCode = (nsName, exportNames, source) => {
|
||||
return generate(
|
||||
t.program([
|
||||
t.importDeclaration(
|
||||
exportNames.map((n) => t.importSpecifier(t.identifier(n), t.identifier(n))),
|
||||
t.stringLiteral(source)
|
||||
),
|
||||
t.exportNamedDeclaration(
|
||||
t.variableDeclaration('const', [
|
||||
t.variableDeclarator(
|
||||
t.identifier(nsName),
|
||||
t.objectExpression(
|
||||
exportNames.map((n) =>
|
||||
t.objectProperty(t.identifier(n), t.identifier(n), false, true)
|
||||
)
|
||||
)
|
||||
),
|
||||
])
|
||||
),
|
||||
])
|
||||
).code;
|
||||
};
|
||||
|
||||
module.exports = { getExportCode, getExportNamedNamespaceCode };
|
Loading…
Add table
Add a link
Reference in a new issue