Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): adding consistent-type-exports rule #3936

Merged

Conversation

dzearing
Copy link
Contributor

@dzearing dzearing commented Sep 28, 2021

This rule addresses issue #3597:

It will catch exports that are type-only, but don't use export type:

// Assuming ButtonProps is a type, this line should not be emitted in TS->JS transpilation.
export { ButtonProps } from 'component-library';

...and allow you to auto-fix them:

// Using export type, the transpiler can drop this line without parsing its type.
export type { ButtonProps } from 'component-library';

It will also auto separate types from non-types:

// Before:
export { Button, ButtonProps } from 'component-library';

// After:
export type { ButtonProps } from 'component-library';
export { Button } from 'component-library';

This is particularly important when using esbuild to transpile a library into a browser-consumable bundle.

Open to feedback! I named it something symmetrical with the current consistent-type-imports rule so that it would be easy to find, but would be happy to rename to something better.

I did not add any options; if there are compelling reasons to add options, please let me know.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @dzearing!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day.

@codecov
Copy link

codecov bot commented Sep 28, 2021

Codecov Report

Merging #3936 (13422d8) into master (600d413) will increase coverage by 0.64%.
The diff coverage is 95.00%.

@@            Coverage Diff             @@
##           master    #3936      +/-   ##
==========================================
+ Coverage   92.69%   93.33%   +0.64%     
==========================================
  Files         333      152     -181     
  Lines       11400     8014    -3386     
  Branches     3242     2568     -674     
==========================================
- Hits        10567     7480    -3087     
+ Misses        363      180     -183     
+ Partials      470      354     -116     
Flag Coverage Δ
unittest 93.33% <95.00%> (+0.64%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
packages/eslint-plugin/src/configs/all.ts 100.00% <ø> (ø)
packages/eslint-plugin/src/util/misc.ts 93.33% <83.33%> (-1.54%) ⬇️
...eslint-plugin/src/rules/consistent-type-exports.ts 95.83% <95.83%> (ø)
...eslint-plugin/src/rules/consistent-type-imports.ts 93.47% <100.00%> (-0.03%) ⬇️
packages/eslint-plugin/src/rules/typedef.ts 94.52% <100.00%> (+2.73%) ⬆️
packages/scope-manager/src/lib/es2020.string.ts
...kages/experimental-utils/src/eslint-utils/index.ts
packages/scope-manager/src/lib/esnext.intl.ts
packages/scope-manager/src/referencer/Visitor.ts
packages/scope-manager/src/scope/TSEnumScope.ts
... and 178 more

@bradzacher bradzacher added the enhancement: new plugin rule New rule request for eslint-plugin label Oct 3, 2021
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fantastic, thanks so much! 👏

Just left a few nits. I'll mark as approved for now, but would be great to address them.

@bradzacher bradzacher added the awaiting response Issues waiting for a reply from the OP or another party label Oct 18, 2021
@dzearing
Copy link
Contributor Author

Thank you @JoshuaKGoldberg ! I will address.

@dzearing
Copy link
Contributor Author

All feedback has been addressed. Thanks again @JoshuaKGoldberg

@JoshuaKGoldberg JoshuaKGoldberg removed the awaiting response Issues waiting for a reply from the OP or another party label Oct 20, 2021
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fantastic to me, thanks @dzearing!

I don't think either of my comments are blocking, but would be nice-to-haves IMO!

packages/eslint-plugin/src/util/misc.ts Outdated Show resolved Hide resolved
packages/eslint-plugin/src/util/misc.ts Show resolved Hide resolved
@dzearing
Copy link
Contributor Author

dzearing commented Oct 21, 2021

Ready for merge, unless there is any other feedback. :)

Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quick eyeball looking at some of the type stuff.
Some queries that I'm not sure how your code will handle.


could you please add the following test cases:

const variable = 1;
class Class {}
enum Enum {}
function Func() {}
namespace ValueNS { export const x = 1 }

export {
    variable,
    Class,
    Enum,
    Func,
    ValueNS,
}
const variable = 1;
class Class {}
enum Enum {}
function Func() {}
namespace ValueNS { export const x = 1 }

export type { // type-export the values
    variable,
    Class,
    Enum,
    Func,
    ValueNS,
}
type Alias = 1;
interface IFace {}
namespace TypeNS { export type x = 1 }

export {
  Alias,
  IFace,
  TypeNS,
}
type Alias = 1;
interface IFace {}
namespace TypeNS { export type x = 1 }

export type {
  Alias,
  IFace,
  TypeNS,
}

packages/eslint-plugin/tests/util/misc.test.ts Outdated Show resolved Hide resolved
@dzearing
Copy link
Contributor Author

could you please add the following test cases:

All added.

Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect - this LGTM!

Just as a note - TS4.5 will add a new feature which allows you to specify the type declarator as part of the specifier!

type T = 1;

export { type T }

repl

We don't have support for this syntax in our parser yet (we only support RC versions and it's still 4.5-beta), but once we release support we'll have to update this rule to match! I have noted this in the 4.5 tracking issue - #3950

@bradzacher bradzacher changed the title feat(eslint-plugin): adding consistent-type-exports rule feat(eslint-plugin): adding consistent-type-exports rule Oct 23, 2021
@bradzacher bradzacher merged commit 1971a3f into typescript-eslint:master Oct 23, 2021
@dzearing dzearing deleted the feat/consistent-type-exports branch October 25, 2021 16:57
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants