Skip to content

Enable noImplicitOverride in our codebase #120675

Closed

Description

@mjbvz

--noImplicitOverride

TS 4.3 adds a new strictness option: --noImplicitOverride. This requires that you use the override keyword when overriding and method:

class Foo {
   foo() {...}
}

class SubFoo extends Foo {
    foo() { ... } // ERROR: missing override specifier
}

To fix this, simply add the override keyword:

class SubFoo extends Foo {
    override foo() { ... } 
}

The override keyword does not change runtime behavior but can help programmers in a few ways:

  • When reading code, it alerts you that a method is overriding one from the base class

  • It is an error to try to override a method that does not exist on the base class. This can help catch errors caused by renaming a method in a base class but forgetting to update the method name in the subclasses.

Enabling

I propose enabling this new flag for both VS Code code and extensions.

Enabling it currently causes ~2200 compile errors, however unlike the previous strictness checks we enabled, I believe should simply use a script to automatically fix the vast majority of them (by inserting override in the necessary location)

Activity

  1. added a commit that references this issue on Apr 7, 2021
  2. mjbvz commented on Apr 7, 2021

    @mjbvz
    ContributorAuthor

    Here's the simple python script I used to automate most of the rewrites:

    from os import path
    import re
    
    
    def fixThisMember(file, line, col):
        with open(file, 'r') as f:
            lines = f.readlines()
    
        lineContent = lines[line - 1]
    
        # public? async? method(
        lineContent = re.sub(
            r'^(\s+)((?:public|private|protected) )?(async )?([\w_$]+[\(<])',
            r'\1\2\3override \4',
            lineContent)
    
        # public? get|set prop(
        lineContent = re.sub(
            r'^(\s+)((?:public|private|protected) )?(get|set) ([\w_$]+)\(',
            r'\1\2override \3 \4(',
            lineContent)
    
        # public? readonly? prop =
        lineContent = re.sub(
            r'^(\s+)((?:public|private|protected) )?(readonly )?([\w_$]+) =',
            r'\1\2\3override \4 =',
            lineContent)
    
        # public? readonly? prop:
        lineContent = re.sub(
            r'^(\s+)((?:public|private|protected) )?(readonly )?([\w_$]+):',
            r'\1\2\3override \4:',
            lineContent)
    
        if lineContent != lines[line - 1]:
            print(file)
            lines[line - 1] = lineContent
            with open(file, 'w') as f:
                f.writelines(lines)
    
    
    # file containing `yarn watch` error text
    with open('override-errors.text') as f:
        for line in f:
            pMatch = re.match(r'.+Error: (.+)\((\d+),(\d+)\): (This member must have an)',
                              line, flags=re.I)
            if pMatch:
                file = pMatch.group(1)
                line = int(pMatch.group(2))
                col = int(pMatch.group(3))
                fixThisMember(file, line, col)

    This is a pretty simple script but takes us from 2200 errors to 200 or so.

    The remaining errors fall into a few buckets:

  3. Kingwl commented on Apr 7, 2021

    @Kingwl
    Contributor

    Cool! But... Are there any tools could batch apply quickfix?

  4. mjbvz commented on Apr 7, 2021

    @mjbvz
    ContributorAuthor

    Wenlu Wang (@Kingwl) Not that I know off. For source actions, you can use https://marketplace.visualstudio.com/items?itemName=bierner.folder-source-actions

    Hooking up individual quick fixes would be a little more difficult but should be doable (writing a simple python find/replace script was a lot quicker in this case)

  5. 32 remaining items

  6. mjbvz commented on Apr 20, 2021

    @mjbvz
    ContributorAuthor

    Alexandru Dima (@alexdima) SteVen Batten (@sbatten) Just 6 errors remaining! Please take a look when you have a chance

    Johannes Rieken (@jrieken) Alexandru Dima (@alexdima) Do you have any idea about why MirrorModel.version is causing issues with the esm build? After adding override to version, yarn gulp editor-esm-bundle results in:

    Launching the TS compiler at /Users/matb/projects/vscode/out-editor-esm...
    vs/editor/common/services/editorSimpleWorker.ts(94,22): error TS4113: This member cannot have an 'override' modifier because it is not declared in the base class 'MirrorTextModel'.
    

    In our source, MirrorTextModel has a property:

    	get version(): number {
    		return this._versionId;
    	}

    However in the esm build, this property is missing from MirrorTextModel . It may be getting removed by our tree shaking

  7. alexdima commented on Apr 22, 2021

    @alexdima
    Member

    Do you have any idea about why MirrorModel.version is causing issues with the esm build?

    Yes, TypeScript is sometimes incorrect when invoking Find all references and this was one of those cases, where finding all references on ICommonModel.version would not return MirrorTextModel.version. As far as I am concerned, this is a TypeScript bug that also manifests itself when doing a rename. For example, renaming ICommonModel.version to ICommonModel.versionX will introduce a compilation error. I have reported the problem to TS, but the issue was closed microsoft/TypeScript#25831

    To resolve the problem, I added another interface and an explicit implements ... heritage clause to help TS find all references correctly.

  8. added a commit that references this issue on Apr 22, 2021
  9. mjbvz commented on Apr 24, 2021

    @mjbvz
    ContributorAuthor

    Done with 66fd0cb

    Thanks everyone!

  10. locked and limited conversation to collaborators on Jun 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

debt

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    ⌂ Proxy