Skip to content

Stop using default exports from packages/cursorless-engine/src/processTargets. #2011

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

Merged
merged 6 commits into from
Nov 9, 2023

Conversation

bjaspan
Copy link
Contributor

@bjaspan bjaspan commented Nov 8, 2023

This removes all default exports from packages/cursorless-engine/src/processTargets.

I'm not yet sure if this will actually help me achieve my larger goal but it is a small net improvement in the codebase in any case.

@bjaspan
Copy link
Contributor Author

bjaspan commented Nov 8, 2023

I think the eslint rule we want prevent regression is

'no-restricted-syntax': [
  'error',
  'Literal[value=/export default/]',
],

but it has to be in an .eslintrc file in packages/cursorless-engine/src/processTargets and I haven't figured out what other boilerplate it needs yet.

Copy link
Member

@pokey pokey left a comment

Choose a reason for hiding this comment

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

🙌

import { fitRangeToLineContent } from "./LineScopeHandler";
import { TargetScope } from "./scope.types";

export default class TokenScopeHandler extends BaseScopeHandler {
export class ParagraphScopeHandler extends BaseScopeHandler {
Copy link
Member

Choose a reason for hiding this comment

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

Whoops nice catch 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, the compiler caught it for me. :-)

@bjaspan
Copy link
Contributor Author

bjaspan commented Nov 8, 2023

Here is the quick-hack shell script I wrote to make this PR. We can probably adapt it to remove default exports from everywhere in cursorless:

#!/bin/bash -x

cd /Users/bjaspan/cursorless/packages/cursorless-engine

git reset --hard

# "export default class <name>" is invalid syntax but gets found for
# $default_classes and cleaned up in a subsequent command.
find src/processTargets -name '*.ts' | xargs perl -MFile::Basename -pi -e 'my $f = basename($ARGV, ".ts"); s/export default class implements/export default class $f implements/'

default_classes=`find src/processTargets/ -name '*.ts' | xargs perl -lne '/export default(?: abstract)? (?:class|interface) (\w+)/ && print $1' | sort | uniq | perl -le 'print join("|", grep(chomp, <>))'`

find src/processTargets -name '*.ts' | xargs perl -pi -e 's/export default/export/'
find . -name '*.ts' | xargs perl -pi -e 's/import\s+('${default_classes}')\s+from/import { \1 } from/'

sed -i '' '/default as/d' src/processTargets/targets/index.ts
sed -i '' '/default as/d' src/processTargets/modifiers/scopeHandlers/index.ts
sed -i '' 's/TokenScopeHandler/ParagraphScopeHandler/' src/processTargets/modifiers/scopeHandlers/ParagraphScopeHandler.ts

cat >src/processTargets/modifiers/ItemStage/index.ts <<EOF
export * from "./ItemStage";
EOF

@pokey
Copy link
Member

pokey commented Nov 8, 2023

I think the eslint rule we want prevent regression is


'no-restricted-syntax': [

  'error',

  'Literal[value=/export default/]',

],

but it has to be in an .eslintrc file in packages/cursorless-engine/src/processTargets and I haven't figured out what other boilerplate it needs yet.

No we want https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-default-export.md

@pokey
Copy link
Member

pokey commented Nov 8, 2023

Did you want to add rule in this PR or follow up? I don't feel strongly

@bjaspan
Copy link
Contributor Author

bjaspan commented Nov 8, 2023

Followup since I don't know how to create the appropriate .eslintrc.json file yet.

@pokey pokey enabled auto-merge November 8, 2023 22:34
auto-merge was automatically disabled November 9, 2023 15:26

Head branch was pushed to by a user without write access

@bjaspan
Copy link
Contributor Author

bjaspan commented Nov 9, 2023

Added lint rule.

@pokey
Copy link
Member

pokey commented Nov 9, 2023

Nice! Have you checked that it's actually biting?

@pokey pokey added this pull request to the merge queue Nov 9, 2023
Merged via the queue into cursorless-dev:main with commit a79254c Nov 9, 2023
@bjaspan
Copy link
Contributor Author

bjaspan commented Nov 9, 2023

Yes, I confirmed that the lint rule works.

@bjaspan bjaspan deleted the no-default branch November 9, 2023 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants