Skip to content

Commit

Permalink
ensure that child exports of export type are always tracked as types
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Sep 1, 2021
1 parent f784e12 commit 1b7a671
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
8 changes: 8 additions & 0 deletions packages/kbn-eslint-plugin-eslint/helpers/export_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class ExportSet {
get size() {
return this.values.size + this.types.size;
}

/**
* @param {'value'|'type'} type
* @param {string} value
*/
add(type, value) {
this[type + 's'].add(value);
}
}

module.exports = { ExportSet };
17 changes: 8 additions & 9 deletions packages/kbn-eslint-plugin-eslint/helpers/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,46 +126,45 @@ const getExportNamesDeep = (
for (const statement of sourceFile.statements) {
// export function xyz() ...
if (ts.isFunctionDeclaration(statement) && statement.name && hasExportMod(statement)) {
exportSet.values.add(statement.name.getText());
exportSet.add(assumeAllTypes ? 'type' : 'value', statement.name.getText());
continue;
}

// export const/let foo = ...
if (ts.isVariableStatement(statement) && hasExportMod(statement)) {
for (const dec of statement.declarationList.declarations) {
exportSet.values.add(dec.name.getText());
exportSet.add(assumeAllTypes ? 'type' : 'value', dec.name.getText());
}
continue;
}

// export class xyc
if (ts.isClassDeclaration(statement) && statement.name && hasExportMod(statement)) {
exportSet.values.add(statement.name.getText());
exportSet.add(assumeAllTypes ? 'type' : 'value', statement.name.getText());
continue;
}

// export interface Foo {...}
if (ts.isInterfaceDeclaration(statement) && hasExportMod(statement)) {
exportSet.types.add(statement.name.getText());
exportSet.add('type', statement.name.getText());
continue;
}

// export type Foo = ...
if (ts.isTypeAliasDeclaration(statement) && hasExportMod(statement)) {
exportSet.types.add(statement.name.getText());
exportSet.add('type', statement.name.getText());
continue;
}

// export enum ...
if (ts.isEnumDeclaration(statement) && hasExportMod(statement)) {
exportSet.values.add(statement.name.getText());
exportSet.add(assumeAllTypes ? 'type' : 'value', statement.name.getText());
continue;
}

if (ts.isExportDeclaration(statement)) {
const clause = statement.exportClause;
const types = assumeAllTypes || statement.isTypeOnly;
const set = types ? exportSet.types : exportSet.values;

// export * from '../foo';
if (!clause) {
Expand All @@ -187,15 +186,15 @@ const getExportNamesDeep = (

// export * as foo from './foo'
if (ts.isNamespaceExport(clause)) {
set.add(clause.name.getText());
exportSet.add(types ? 'type' : 'value', clause.name.getText());
continue;
}

// export { foo }
// export { foo as x } from 'other'
// export { default as foo } from 'other'
for (const e of clause.elements) {
set.add(e.name.getText());
exportSet.add(types ? 'type' : 'value', e.name.getText());
}
continue;
}
Expand Down

0 comments on commit 1b7a671

Please sign in to comment.