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

[Bug]: sort-prop-types's autofix doesn't move the semicolon, producing broken code, on single-line Props types #3783

Closed
2 tasks done
tylerlaprade opened this issue Jul 16, 2024 · 9 comments · Fixed by #3784

Comments

@tylerlaprade
Copy link
Contributor

tylerlaprade commented Jul 16, 2024

Is there an existing issue for this?

  • I have searched the existing issues and my issue is unique
  • My issue appears in the command-line and not only in the text editor

Description Overview

sort-prop-types, with checkTypes: true, breaks the code when run on single-line Props with autofix due to the semicolon staying at the end instead of moving.

image

The props were rearranged without the semicolon dividing them

eslint --fix

Expected Behavior

Semicolon be moved along with the moved prop when running autofix

type CustomProps = { onChange: (event: { target: { name: string; value: string } }) => void; name: string };

should become

type CustomProps = { name: string; onChange: (event: { target: { name: string; value: string } }) => void };

Instead, it is fixing to

type CustomProps = { name: string onChange: (event: { target: { name: string; value: string } }) => void; };

eslint-plugin-react version

v7.34.4

eslint version

v8.57.0

node version

v20.14.10

@tylerlaprade tylerlaprade changed the title [Bug]: sort-prop-types's autofix is missing a semicolon, producing broken code [Bug]: sort-prop-types's autofix is missing a semicolon, producing broken code, on single-line Props types Jul 16, 2024
@tylerlaprade tylerlaprade changed the title [Bug]: sort-prop-types's autofix is missing a semicolon, producing broken code, on single-line Props types [Bug]: sort-prop-types's autofix doesn't move the semicolon, producing broken code, on single-line Props types Jul 16, 2024
@tylerlaprade
Copy link
Contributor Author

UPDATE: I found another case which produces an incorrect autofix, this one on a multi-line Props. It seems to be due to comments. Notice that the comments have been duplicated, three props have disappeared, and part of a typehint is on a line by itself.

Starting code:

type Props = {
  onClose: () => void;
  onSave?: () => void;
  initialContractInfo?: ContractInfo; // used to pre-populate the form just for our tests
  contractVersionTraceId?: TraceId; // used when editing an existing contract
  contractContainerId: TraceId;
  wizardStartIndex?: number;
  contractStatus?: BackendContractStatus;
  contractVersion?: BackendContractVersion;
};

Auto-fixed code:

type Props = {
  initialContractInfo?: ContractInfo; // used to pre-populate the form just for our tests
  // used to pre-populate the form just for our tests
  contractVersionTraceId?: TraceId; // used when editing an existing contract
  // used when editing an existing contract
  contractContainerId: TraceId;
  dContractVersion;
  onClose: () => void;
  onSave?: () => void;
};

Expected result:

type Props = {
  initialContractInfo?: ContractInfo; // used to pre-populate the form just for our tests
  contractVersionTraceId?: TraceId; // used when editing an existing contract
  contractContainerId: TraceId;
  wizardStartIndex?: number;
  contractStatus?: BackendContractStatus;
  contractVersion?: BackendContractVersion;
  onClose: () => void;
  onSave?: () => void;
};

@ljharb
Copy link
Member

ljharb commented Jul 16, 2024

@tylerlaprade can you provide the component code these are associated with? it won't repro without a component.

@tylerlaprade
Copy link
Contributor Author

Full code

type CustomProps = { onChange: (event: { target: { name: string; value: string } }) => void; name: string };

function PercentFormatCustom(props: CustomProps) {
  return (
    <div />
  );
}
type Props = {
  onClose: () => void;
  onSave?: () => void;
  initialContractInfo?: ContractInfo; // used to pre-populate the form just for our tests
  contractVersionTraceId?: TraceId; // used when editing an existing contract
  contractContainerId: TraceId;
  wizardStartIndex?: number;
  contractStatus?: BackendContractStatus;
  contractVersion?: BackendContractVersion;
};

function ContractVersionWizard(props: Props) {
  return <div />;
}

and the relevant rule config:

    "react/sort-prop-types": [
      "error",
      {
        "callbacksLast": true,
        "requiredFirst": true,
        "sortShapeProp": true,
        "noSortAlphabetically": true,
        "checkTypes": true
      }
    ]

@ljharb
Copy link
Member

ljharb commented Jul 17, 2024

Thanks, that definitely reproduces.

I'm not sure how to fix it, tbh, but i can certainly disable the autofix for types.

@tylerlaprade
Copy link
Contributor Author

The autofix has been quite helpful in other cases. I imagine this would just be an issue of including the semicolon and/or comment in the moved token.

@ljharb
Copy link
Member

ljharb commented Jul 17, 2024

right - it's not super clear to me how to DO that tho :-/

cc @akulsr0 from #3615, maybe you have an idea?

@tylerlaprade
Copy link
Contributor Author

tylerlaprade commented Jul 22, 2024

@ljharb, in the description of #3784, @akulsr0 says it only partially fixes this issue. Should we leave it open for the other half of the issue?

@akulsr0
Copy link
Contributor

akulsr0 commented Jul 22, 2024

I feel the second one can be a seperate issue, for better tracking.

@ljharb
Copy link
Member

ljharb commented Jul 22, 2024

Yeah it'd be very helpful to file a separate issue for that. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

3 participants