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

Fix custom-error-definition to support constructors without a body #442

Merged

Conversation

pat841
Copy link
Contributor

@pat841 pat841 commented Nov 26, 2019

Using this example:

export class CustomError extends Error {
  type: string;
  text: string;
  reply?: any;
  constructor(type: string, text: string, reply?: any);
}

The error TypeError: Cannot read property 'body' of null gets thrown for the line const constructorBody = constructorBodyNode.body;.

We can fix this by returning early if no constructor body is available.

@sindresorhus
Copy link
Owner

Can you add a test?

@pat841 pat841 force-pushed the feature/custom-error-definition-fix branch from b195ebc to be56292 Compare November 26, 2019 16:58
@mf-pherlihy
Copy link

Can you add a test?

I tried but unfortunately the tests dont support the Typescript syntax.

The primary issue is constructor(type: string, text: string, reply?: any); not having a constructor body. This is supported by Typescript but throws A fatal parsing error occurred: Parsing error: Unexpected token ; in a non-TS environment.

@futpib
Copy link
Collaborator

futpib commented Nov 26, 2019

@mf-pherlihy @sindresorhus I took the liberty of adding a typescript test, check it out.

@sindresorhus sindresorhus changed the title Fix the custom-error-definition rule to support constructors without a body. Fix custom-error-definition to support constructors without a body Nov 27, 2019
@sindresorhus sindresorhus merged commit a496e96 into sindresorhus:master Nov 27, 2019
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.

None yet

5 participants