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

ast node span is incorrect when leading comments existed #7845

Closed
huozhi opened this issue Aug 21, 2023 · 4 comments
Closed

ast node span is incorrect when leading comments existed #7845

huozhi opened this issue Aug 21, 2023 · 4 comments
Assignees
Labels
Milestone

Comments

@huozhi
Copy link

huozhi commented Aug 21, 2023

Describe the bug

When there's comment at the beginning, the first span of AST node is always starting with 0, instead of actual position of first non-comment code position in the original code

Input code

/**
 * Copyright (c) 2013-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

'use strict';

Config

No response

Playground link

https://play.swc.rs/?version=1.3.74&code=H4sIAAAAAAAAA0WOyw6CQAxF93zF3aEExcfSJcGERN3ID2inyEQyJZ1hwd87k2hc9vTe01ZFkaFALdOi9jUErGiNw25%2F3EzKnl0ocX4QP0XeJVpH25hOhW6wHl5mJQaJYcRxtMTOs8HsDCvCwLi23Q%2Bjl8hhXVokxaWtm9u9QW9H%2FmKoSICxyhREF0gf6f9QUOb0QJVl%2BRyNPqilkJ%2ByDz%2Bh%2BQvGAAAA&config=H4sIAAAAAAAAA2VRsY7DMAjd%2BxURc4dTxttu7NCPsHykci82FhCpUZV%2FL6RJmugWC97j8QA%2FT00Dd4nw3TwttKQGFuQtN0TGouFhCOhYUSKnqnBeWRWnlAeckelNgAa%2BoboIpf1q20UAPZGgwV3oBddaDkU64rx3ZQxRd4BDQ9GUXQ1hUMpBU4SFng7uOZXUjft2kXJlFDlae2kot36b6NAFHxXZDG39ft%2FrD7FeciXWH7FbaaIi%2F24wP59BNlNIcqXfYbZ0xbtsRZZP8EP7mjZ1pnIX33I6TS9UUQp0rgEAAA%3D%3D

Expected behavior

for 1st non comment code "use strict" directive, the output AST is showing span from 0. It should be the position of that string literal from original code.

{
  "type": "Module",
  "span": {
    "start": 0,
    "end": 13,
    "ctxt": 0
  },
  "body": [
    {
      "type": "ExpressionStatement",
      "span": {
        "start": 0,
        "end": 13,
        "ctxt": 0
      },
      "expression": {
        "type": "StringLiteral",
        "span": {
          "start": 0,
          "end": 12,
          "ctxt": 0
        },
        "value": "use strict",
        "raw": "'use strict'"
      }
    }
  ],
  "interpreter": null
}

Actual behavior

No response

Version

1.3.74

Additional context

We're using span to get the position of the code, and delete them from the original code. For instance, A rollup plugin is checking "use client" directives and trying to delete it from the original code.

x-ref: huozhi/rollup-preserve-directives#9

@huozhi huozhi added the C-bug label Aug 21, 2023
@kdy1
Copy link
Member

kdy1 commented Aug 22, 2023

Actually lo and hi is BytePos so it's expected. There's no good way to use source location from JS plugin, and JS plugin is going to be deprecated anyway.

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 22, 2023
@kdy1 kdy1 added this to the Planned milestone Aug 22, 2023
@kdy1 kdy1 self-assigned this Aug 22, 2023
@huozhi
Copy link
Author

huozhi commented Aug 22, 2023

Gotcha, sounds like modifying the source based on the spam position could be very risky.

@magic-akari
Copy link
Member

But the node span works well in the rust side.
check it: swc ast viewer

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 12, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

4 participants