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

(ts) Raise syntax error for an abstract method that has body #12687

Merged
merged 2 commits into from Mar 2, 2021

Conversation

sosukesuzuki
Copy link
Member

@sosukesuzuki sosukesuzuki commented Jan 24, 2021

Q                       A
Fixed Issues? Fixes #12682
Patch: Bug Fix? Y
Tests Added + Pass? Yes
License MIT

@sosukesuzuki sosukesuzuki added area: errors area: typescript PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: parser labels Jan 24, 2021
@babel-bot
Copy link
Collaborator

babel-bot commented Jan 24, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/43259/

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 24, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit f6067d8:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

Comment on lines 2831 to 2837
node: T,
isGenerator: boolean,
isAsync: boolean,
isConstructor: boolean,
allowDirectSuper: boolean,
type: string,
inClassScope: boolean = false,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: We can just use ...args if none of them are used in the post processing logic. So we would not forget to sync signatures when we add even more arguments to parseMethod.

Copy link

Choose a reason for hiding this comment

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

Use of ...args is one of the slowest alternatives. Just mention it in case perf is relevant here

Copy link
Member Author

Choose a reason for hiding this comment

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

There are already some ...args in our codebase. I don't know if this is a performance bottleneck, but at least I don't think we need to worry in this PR.

Copy link

Choose a reason for hiding this comment

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

In general. Everything that is a perf regression should be improved, but this is just a TS plugin so maybe no point because too much perf issues in this plugin.

Comment on lines 2835 to 2836
// For estree plugin
method.type === "MethodDefinition"
Copy link
Member

Choose a reason for hiding this comment

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

We can do this.hasPlugin("estree") in this case, so that it's self-explanatory.

@KFlash
Copy link

KFlash commented Jan 27, 2021

Use of 7 properties like that consumes lots of memory. Each property takes 3 bytes each. So 21 bytes total. Plus the size of the object will give e perf regression in V8 etc.

'...' operator is slower than simply iterate through and push to an array (search on google).

Direct string comparison is also slow.

Just mention in case perf and memory is a concern.

const method = super.parseMethod(...args);
if ((method: any).abstract) {
const hasBody = this.hasPlugin("estree")
? !!method.value.body
Copy link

Choose a reason for hiding this comment

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

2x member access is a perf regression

this.raise(
method.start,
TSErrors.AbstractMethodHasImplementation,
(method: any).key.name,
Copy link
Contributor

Choose a reason for hiding this comment

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

The method key is not always Identifier, and that's why Flow complains here.

abstract class C {
  abstract [M]();
}

I think we can just throw

An abstract method cannot have an implementation.

Copy link

Choose a reason for hiding this comment

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

Is this correct error message to be used here? For example TS never throws on
abstract class C { abstract a(): string; }, but throws on abstract class C { abstract a(); } because it's lack return type.

Regarding the example above. TS throws

A computed property name in a method overload must refer to an expression whose type is a literal type or a 'unique symbol' type.ts(1168

'[M]', which lacks return-type annotation, implicitly has an 'any' return type.ts(7010)

Copy link
Member

Choose a reason for hiding this comment

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

If you disable the (opt-in, but enabled by default in the playground) noImplicitAny option, that code doesn't generate any error but just a lint suggestion: https://www.typescriptlang.org/play?noImplicitAny=false#code/MYewdgzgLgBAsjAvDARAawKYE8UwIYQyiRQDcAsAFBV4BG0ATnsLMADYGEDCMA3lTHz0oTFjADacALoAKAJQVKAXypA

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for the confusion here, I was meant to say that an abstract method can be computed, so that example is valid. And the TS error can be suppressed by specifying M. To be clear

abstract class C {
  abstract [M]() {}
}

will throw.

I don't think we have to extract computed key here, since it can be very verbose.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we have to extract computed key here, since it can be very verbose.

I ended up doing it since I noticed it's exactly what TS does.

This was referenced Mar 17, 2021
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 2, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: errors area: typescript outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No syntax error for abstract methods that have an implementation
6 participants