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

[prefer-readonly-type] - Add an option to opt out of function parameters check #91

Closed
avalanche1 opened this issue Jan 21, 2020 · 5 comments
Labels
Resolution: By Design The behavior reported in the issue is actually correct.

Comments

@avalanche1
Copy link

The readonly word in fn parameters when declaring a function takes up really much editor space. With my max-len settings my editor breaks up a simple functional component declaration into many lines.
I have another es-lint rule that checks that fn arguments are not mutated.

I like:

export function UserCanSeeStoryboardDashboard({text}: {text: string}): JSX.Element {
  // ...
}

I don't like:

export function UserCanSeeStoryboardDashboard({
  text,
}: {
  readonly text: string;
}): JSX.Element {
  // ...
}
@RebeccaStevens RebeccaStevens added the Type: Enhancement Enhancement of the code, not introducing new features. label Jan 21, 2020
@tomsdev
Copy link

tomsdev commented Apr 13, 2020

I have another es-lint rule that checks that fn arguments are not mutated.

Are you referring to the rule prefer-readonly-parameters ?

@RebeccaStevens
Copy link
Collaborator

RebeccaStevens commented Apr 16, 2020

@avalanche1, I assume you are creating a React Functional Component in the given example. Wouldn't it be best to pull out the prop's type into it's own declared type like this:

type Props = {
  readonly text: string;
}

export function UserCanSeeStoryboardDashboard({ text }: Props): JSX.Element {
  // ...
}

Also, the return type of such an function should be of type FunctionalComponentElement (rather than JSX.Element):

type Props = {
  readonly text: string;
}

type Element = React.FunctionalComponentElement<Props>;

export function UserCanSeeStoryboardDashboard({ text }: Props): Element {
  // ...
}

If you use an arrow function expression instead of a function declaration, you can just declare the function as a whole to be a FunctionalComponent. The parameter types and return type will then be inferred from this:

type Props = {
  readonly text: string;
}

type Component = React.FunctionalComponent<Props>;

export const UserCanSeeStoryboardDashboard: Component = ({ text }) => {
  // ...
};

@avalanche1
Copy link
Author

avalanche1 commented Apr 20, 2020

Are you referring to the rule prefer-readonly-parameters ?

no-param-reassign

Wouldn't it be best to pull out the prop's type into it's own declared type like this

It depends. If there are multiple params - then yes. If there is one-two params - why the bloat?

Also, the return type of such an function should be of type FunctionalComponentElement (rather than JSX.Element):

JSX.Element has been working just fine for us so far

If you use an arrow function expression instead of a function declaration,

Our code style guide prohibits usage of function expressions. We like top-down approach to code which Makes heavy use of function hoisting (which is why we ❤️ JS).

@RebeccaStevens
Copy link
Collaborator

no-param-reassign doesn't prevent changing sub-properties of a parameter.

why the bloat?

In my opinion this wouldn't be bloat; instead it would be a separation of type and executable code.
As a side note, I'd also recommended exporting the props type (as well as the component type).

JSX.Element has been working just fine for us so far

JSX.Element vs React.FunctionalComponentElement<Props> is like Array<any> vs Array<SomeDataStructure>. It works but it's more generic.

@RebeccaStevens
Copy link
Collaborator

RebeccaStevens commented Apr 21, 2020

I think I'm going to close this issue as parameters should be readonly.
#51 will make inline readonly parameter less ugly once it's implemented.

@RebeccaStevens RebeccaStevens added Resolution: Won't Fix A real bug or issue, but the issue is not impactful enough to spend time on. Resolution: By Design The behavior reported in the issue is actually correct. and removed Type: Enhancement Enhancement of the code, not introducing new features. Resolution: Won't Fix A real bug or issue, but the issue is not impactful enough to spend time on. labels Apr 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: By Design The behavior reported in the issue is actually correct.
Projects
None yet
Development

No branches or pull requests

3 participants