Skip to content

fix(typescript-estree): handle opt computed method with MemberExpression #1100

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

Closed
wants to merge 1 commit into from

Conversation

sosukesuzuki
Copy link
Contributor

Fix #1097

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @sosukesuzuki!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day.

@codecov
Copy link

codecov bot commented Oct 18, 2019

Codecov Report

Merging #1100 into master will not change coverage.
The diff coverage is 100%.

@@           Coverage Diff           @@
##           master    #1100   +/-   ##
=======================================
  Coverage   94.02%   94.02%           
=======================================
  Files         115      115           
  Lines        5123     5123           
  Branches     1434     1434           
=======================================
  Hits         4817     4817           
  Misses        177      177           
  Partials      129      129
Impacted Files Coverage Δ
packages/typescript-estree/src/convert.ts 98.99% <100%> (ø) ⬆️

@@ -1044,7 +1044,8 @@ export class Converter {
}

if (
result.key.type === AST_NODE_TYPES.Identifier &&
(result.key.type === AST_NODE_TYPES.Identifier ||
result.key.type === AST_NODE_TYPES.MemberExpression) &&
node.questionToken
) {
result.key.optional = true;
Copy link
Contributor

@thorn0 thorn0 Oct 18, 2019

Choose a reason for hiding this comment

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

Why add optional: true to key? Let's add it to the property node like Babel does. Or better, for backward compatibility, to both.

Copy link
Contributor

@thorn0 thorn0 Oct 18, 2019

Choose a reason for hiding this comment

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

Also the expression does not necessarily have to be a member expression. Syntactically, TS allows any expression there, it only puts some limitations on its type (which should not concern us here). E.g. check out this valid (albeit useless) TypeScript code:

declare function f(): string;

class Foo {
    [f()]?() {
        return 1;
    }
}

playground

Copy link
Contributor Author

@sosukesuzuki sosukesuzuki Oct 19, 2019

Choose a reason for hiding this comment

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

@thorn0 (/cc @bradzacher ) I also wondered about that. However, optional does not exist in MethodDefinitionBase. (https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/typescript-estree/src/ts-estree/ts-estree.ts#L510-L519). Can we update it?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm..
Tracing it back. This pattern goes back a long way.
Originally introduced in 2017 eslint/typescript-eslint-parser#187
Why did it get put there instead of on the property itself? I don't know.

Digging into this more - we don't do a good job of standardised support for optional members.
I'd be happy if you standardised things, without doing a breaking change.
We can file an issue for the 3.0.0 release to remove the old weird cases.

const a = 'aa';
const b = 'bb';
const c = 'cc';
const d = 'dd';
const e = 'ee';
const f = 'ff';

abstract class Foo {
    a?: string;                 // ClassProperty is marked optional
    [a]?: string;               // nothing is marked optional, should be ClassProperty
    ['aaa']?: string;           // nothing is marked optional, should be ClassProperty
    b?() {}                     // Identifier is marked optional, should be MethodDefinition
    [b]?() {}                   // Identifier is marked optional, should be MethodDefinition
    ['bbb']?() {}               // nothing is marked optional, should be MethodDefinition
    c?(): void                  // Identifier is marked optional, should be MethodDefinition
    [c]?(): void                // Identifier is marked optional, should be MethodDefinition
    ['ccc']?(): void            // nothing is marked optional, should be MethodDefinition
    
    abstract d?: string;        // TSAbstractClassProperty is marked optional
    abstract [d]?: string;      // nothing is marked optional, should be TSAbstractClassProperty
    abstract ['ddd']?: string;  // TSAbstractClassProperty is marked optional
    abstract e?(): void         // Identifier is marked optional, should be TSAbstractMethodDefinition
    abstract [e]?(): void       // Identifier is marked optional, should be TSAbstractMethodDefinition
    abstract ['eee']?(): void   // nothing is marked optional, should be TSAbstractMethodDefinition

    constructor(
        private f?: string,     // Identifier is marked optional, should be that AS WELL AS TSParameterProperty
    ) { }
}

interface Bar {
    a?: string;                 // TSPropertySignature is marked optional
    [a]?: string;               // TSPropertySignature is marked optional
    ['aaa']?: string;           // TSPropertySignature is marked optional
    b?(): void                  // TSMethodSignature is marked optional
    [b]?(): void                // TSMethodSignature is marked optional
    ['bbb']?(): void            // TSMethodSignature is marked optional
}

type Baz1 = {
    [k in string]?: string      // TSMappedType is marked optional
}
type Baz2 = {
    [k in string]+?: string     // TSMappedType is marked optional = "+"
}
type Baz3 = {
    [k in string]-?: string     // TSMappedType is marked optional = "-"
}

function Buzz(
  a?: string,                   // Identifier is marked optional
): void {}

astexplorer repl
ts playground repl

LMK if this makes sense

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, @bradzacher!! ( /cc @thorn0 )
Personally, I think that it is better to keep adding optional to key until releasing 3.0.
Because, in order for us to add optional to properties, we need to update some basic node type definitions(e.g. MethodDefinitionBase ). I am reluctant to do it with minor version upgrades ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can file an issue for the 3.0.0 release to remove the old weird cases.

👍

Copy link
Contributor

@thorn0 thorn0 Oct 22, 2019

Choose a reason for hiding this comment

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

in order for us to add optional to properties, we need to update some basic node type definitions

It's totally okay as long as the property you add is optional.

"Standardising things without doing a breaking change" means:

  • in cases where optional is added to the key in the current version, add it to both the key and the property/method
  • in other cases, add it only to the property/method

@bradzacher bradzacher added the bug Something isn't working label Oct 18, 2019
@bradzacher bradzacher added the awaiting response Issues waiting for a reply from the OP or another party label Nov 12, 2019
@bradzacher bradzacher added the stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period label Apr 13, 2020
@bradzacher bradzacher closed this Apr 13, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
awaiting response Issues waiting for a reply from the OP or another party bug Something isn't working stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing optional when the key is MemberExpression in a computed optional methods.
3 participants