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

Incorrect location for ClassMethod with Decorator #9949

Open
RoniSegal opened this issue May 6, 2019 · 5 comments

Comments

Projects
None yet
4 participants
@RoniSegal
Copy link

commented May 6, 2019

Bug Report

Current Behavior
Typescript class method with decorator gets an incorrect line number.
In the sample bellow function bar is at line 11 with leading decorator in line 10, the method line number considered to be 10.
But function baz (without decorator) gets the right line number (15)

Input Code:

export function logMethod(
    target: Object,
    propertyName: string,
    propertyDesciptor: PropertyDescriptor): PropertyDescriptor {
    console.log("Logged");
    return propertyDesciptor;
};

class Foo {
    @logMethod
    private bar(): void {
        console.log( `bar`);
    }

    private baz(): void {
        console.log( `baz`);
    }
}

Parsing Output:
Bar;

"type": "ClassMethod",
      "start": 224,
      "end": 298,
      "loc": {
        "start": {
          "line": 10,
          "column": 4
        },
        "end": {
          "line": 13,
          "column": 5
        }
      }
}

Baz;

{
      "type": "ClassMethod",
      "start": 306,
      "end": 364,
      "loc": {
        "start": {
          "line": 15,
          "column": 4
        },
        "end": {
          "line": 17,
          "column": 5
        }
      }
}

Expected behavior/code
Function foo line number should be 11

Babel Configuration (.babelrc, package.json, cli command)

{
    // parse in strict mode and allow module declarations
    sourceType: 'module',
    plugins: [["decorators", {decoratorsBeforeExport: true}], "*"]
}

Environment

  • Babel version(s): 7.3.2
  • Node/npm version: Node 10.11.0/npm 6.4.1
  • OS: Windows 10
  • Monorepo: no
  • How you are using Babel: api (require "@babel/parser" and call "parse")
@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented May 6, 2019

Hey @RoniSegal! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community
that typically always has someone willing to help. You can sign-up here
for an invite.

@RoniSegal

This comment has been minimized.

Copy link
Author

commented May 12, 2019

Any updates on this?

@loganfsmyth

This comment has been minimized.

Copy link
Member

commented May 12, 2019

I would consider this expected (though understandably surprising) behavior. AST node start/end locations generally fully enclose all of the ranges of their child nodes, and since the decorators of a class method are child nodes, the start location of the method needs to be before the decorators.

@RoniSegal

This comment has been minimized.

Copy link
Author

commented May 12, 2019

I wouldn't consider this as expected behavior, if you transpile the code into js with source maps, the source location of the method will be one line after the decorators

@loganfsmyth

This comment has been minimized.

Copy link
Member

commented May 12, 2019

If you'd like to create a specific example where the sourcemap is off, we can certainly see if there is a way to fix it, but the AST position is unlikely to change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.