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

Drop unused code #5067

Merged
merged 7 commits into from
Jan 24, 2023
Merged

Drop unused code #5067

merged 7 commits into from
Jan 24, 2023

Conversation

fregante
Copy link
Contributor

What this PR does

This PR deletes unused code + drops exports that aren't imported anywhere (which helps with the automated detection of first part)

Discussion

I found a lot of surprisingly-unused code in the extension, some dating back to the original commit, replaced by modern alternatives or simply no longer useful. Some of this code requires maintenance via linting and missed "TODOs", so keeping it is counterproductive.

The tool also found more recent code (~6-7 months ago) which I did not delete.

Reviewer notes

To keep reviews speedy, if you see anything potentially useful again in the future, leave a comment with just "keep" and I'll restore with any of its dependencies. 👍

It's also possible that this PR highlights some issues, so please leave a comment if something doesn't look right.

One of the surprises is that we had two identical SelectorSelectorField components and neither one of them was used 🤔

❯ ts-prune > a
❯ cat a | rg -e '([^:]+).+ - (\w+) \(used in module\)' --only-matching --no-filename --replace 'sed -i "" "s/export type $2/type $2/" $1' | sh

also with const and function instead of type

❯ git checkout -- src/**/*.stories.*
ts-prune | grep -v 'used in module' | grep -v testHelper | grep -v mocks |grep -v stories | grep -v Default
@codecov
Copy link

codecov bot commented Jan 22, 2023

Codecov Report

Merging #5067 (485766a) into main (802dd08) will increase coverage by 0.60%.
The diff coverage is 68.85%.

@@            Coverage Diff             @@
##             main    #5067      +/-   ##
==========================================
+ Coverage   59.70%   60.30%   +0.60%     
==========================================
  Files         972      961      -11     
  Lines       29404    29064     -340     
  Branches     5636     5557      -79     
==========================================
- Hits        17555    17527      -28     
+ Misses      11849    11537     -312     
Impacted Files Coverage Δ
src/analysis/analysisVisitors/renderersAnalysis.ts 100.00% <ø> (+3.22%) ⬆️
...rc/analysis/analysisVisitors/varAnalysis/varMap.ts 97.87% <ø> (+2.03%) ⬆️
src/auth/authUtils.ts 45.83% <ø> (ø)
src/auth/useRequiredPartnerAuth.ts 90.00% <ø> (ø)
src/background/contextMenus.ts 56.57% <ø> (ø)
src/background/dataStore.ts 0.00% <0.00%> (ø)
src/background/externalProtocol.ts 0.00% <0.00%> (ø)
src/background/partnerIntegrations.ts 53.84% <ø> (ø)
src/blocks/PipelineExpressionVisitor.ts 100.00% <ø> (ø)
src/blocks/PipelineVisitor.ts 100.00% <ø> (ø)
... and 90 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@fregante fregante changed the title Don't export unless needed Drop unused code Jan 22, 2023
@@ -26,7 +26,7 @@ import { PipelineFlavor } from "@/pageEditor/pageEditorTypes";

export const MULTIPLE_RENDERERS_ERROR_MESSAGE =
"A panel can only have one renderer. There are one or more other renderers configured for this extension.";
export const RENDERER_MUST_BE_LAST_BLOCK_ERROR_MESSAGE =
const RENDERER_MUST_BE_LAST_BLOCK_ERROR_MESSAGE =
Copy link
Contributor Author

Choose a reason for hiding this comment

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

"This export could be useful!"

If/when it is, re-exporting it takes seconds

@@ -1,61 +0,0 @@
/*
Copy link
Contributor Author

@fregante fregante Jan 22, 2023

Choose a reason for hiding this comment

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

This module for example has a copy in /brickModalNoTags/

import SwitchButtonWidget from "@/components/form/widgets/switchButton/SwitchButtonWidget";
import { makeLabelForSchemaField } from "@/components/fields/schemaFields/schemaFieldUtils";

const BooleanField: React.FunctionComponent<SchemaFieldProps> = (props) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the last usage was dropped in #2915

@@ -1,38 +0,0 @@
/*
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Last usage dropped in #4071

@@ -239,86 +212,6 @@ export async function runRendererBlock(
}
}

export async function runReaderBlock({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an example of function that probably hasn't been used since 2021 but received several rounds maintenance

Screenshot 4

@@ -1,53 +0,0 @@
export function globalSearch(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A whole vendor dependency could now be dropped!

@twschiller
Copy link
Contributor

twschiller commented Jan 22, 2023

The tool also found more recent code

Which tool did you run? Is it eslint? Would be nice to mark which functions must be exported b/c they're used by the app. And then enforce no unnecessary exports/dead code in CI

@@ -54,7 +52,6 @@ export async function init(): Promise<void> {
registerBuiltinBlocks();
registerContribBlocks();

addListenerForUpdateSelectedElement();
Copy link
Contributor

Choose a reason for hiding this comment

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

@fregante Why is this removed?

Copy link
Contributor Author

@fregante fregante Jan 22, 2023

Choose a reason for hiding this comment

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

If you follow the imported file you'll reach one of the functions that was unused. First I deleted that one, then the file became unused, then this import did nothing. It's related to one of the pageScript WITH_WINDOW functions or something like that.

The tool is ts-prune

It can't be automated because it has a lot of false positives and it doesn't actually edit the files. I just clicked its list and looked at the inline blame on my IDE

Copy link
Contributor

Choose a reason for hiding this comment

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

It can't be automated because it has a lot of false positives

I ran the report, and it doesn't look like many false positives. There's way to ignore file patterns: --ignore '.test.tsx?|.stories.tsx?' and also ignore individual lines (which would be useful for API surface area called by extension)

I'll ping @mnholtz and @alanb43 as a potential tool to add to our CI

export const detectFrameworks = getMethod("DETECT_FRAMEWORKS");

export const runBlock = getMethod("RUN_SINGLE_BLOCK");
export const runRendererBlock = getMethod("RUN_RENDERER_BLOCK");
export const runReaderBlock = getMethod("RUN_READER_BLOCK");
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -40,7 +40,7 @@ export type RendererOptions = {
* Returns true if `literalOrTemplate` includes any template expressions that would be replaced by `context`.
* @param literalOrTemplate the string literal or Nunjucks/Handlebars template.
*/
export function containsTemplateExpression(literalOrTemplate: string): boolean {
function containsTemplateExpression(literalOrTemplate: string): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

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

This logic is is replicated in 3-4 places (search for .includes("{{")). We might want to consolidate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Feel free to open an issue or PR, I'm not familiar with this code. It's best to leave logic changes out of large "cleanup" PRs so they're easier to follow

@@ -437,7 +436,6 @@ export const {
useUpdatePackageMutation,
useDeletePackageMutation,
useListPackageVersionsQuery,
useUpdateScopeMutation,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is likely used by the app? Or is it repeated in the app?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That I don't know unfortunately. I can open a temporary App PR based on this branch to find out

Copy link
Contributor Author

@fregante fregante Jan 23, 2023

Choose a reason for hiding this comment

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

It seems that this export is used locally but due to the way it's being exported it's not being detected correctly by ts-prune.

useGetGroupsQuery is used by the app but it seems to be repeated there.

I reverted the changes made to this file anyway.

@twschiller
Copy link
Contributor

twschiller commented Jan 22, 2023

@fregante looks good overall. Which tool are you using for the analysis?

@fregante
Copy link
Contributor Author

All good, you can see the full test suite on https://github.com/pixiebrix/pixiebrix-app/actions/runs/3982507927/jobs/6827031840

There was only one missed export:

pixiebrix-app/src/auth/authUtils.ts:19:10 - error TS2459: Module '"@/auth/authUtils"' declares 'selectOrganizations' locally, but it is not exported.

 import { selectOrganizations } from "@/auth/authUtils";

        ~~~~~~~~~~~~~~~~~~~


pixiebrix-extension/src/auth/authUtils.ts:22:10

 function selectOrganizations(

            ~~~~~~~~~~~~~~~~~~~

'selectOrganizations' is declared here.

@github-actions
Copy link

No loom links were found in the first post. Please add one there if you'd like to it to appear on Slack.

Do not edit this comment manually.

@@ -19,6 +19,7 @@ import { type Me } from "@/types/contract";
import { type UserDataUpdate, type AuthState } from "@/auth/authTypes";
import { type UUID } from "@/core";

// Used by the app
Copy link
Contributor

Choose a reason for hiding this comment

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

@fregante it's better to use the ts-prune-ignore-next with a comment so that it doesn't show up in future reports

https://github.com/nadeesha/ts-prune#how-do-i-ignore-a-specific-identifier

@twschiller twschiller added this to the 1.7.18 milestone Jan 23, 2023
@twschiller twschiller merged commit e8321bb into main Jan 24, 2023
@twschiller twschiller deleted the F/lint/don-t-export-unused branch January 24, 2023 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants