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

node.loc.source is not being handled correctly. #733

Open
citrizon opened this issue Dec 2, 2024 · 0 comments
Open

node.loc.source is not being handled correctly. #733

citrizon opened this issue Dec 2, 2024 · 0 comments

Comments

@citrizon
Copy link

citrizon commented Dec 2, 2024

Motivation

When i am stringifying an AST that is ES-Tree compliant, the locations (through "loc" property) works but the sources are not matching. The "loc" property also has a nested property called "source" which is a string. When the AST is being generated, astring uses the generated file name inside "file" property inside the sourcemap instead of checking the "source" property inside the "loc" object.

Expected behavior

Testing AST:

{
  "type": "Program",
  "sourceType": "script",
  "body": [
    {
      "type": "VariableDeclaration",
      "kind": "let",
      "declarations": [
        {
          "type": "VariableDeclarator",
          "id": {
            "type": "Identifier",
            "name": "hi",
            "loc": {
              "start": {
                "line": 1,
                "column": 4
              },
              "end": {
                "line": 1,
                "column": 6
              },
              "source": "test1.js"
            }
          },
          "init": {
            "type": "Literal",
            "value": 1,
            "loc": {
              "start": {
                "line": 1,
                "column": 9
              },
              "end": {
                "line": 1,
                "column": 10
              },
              "source": "test1.js"
            }
          },
          "loc": {
            "start": {
              "line": 1,
              "column": 4
            },
            "end": {
              "line": 1,
              "column": 10
            },
            "source": "test1.js"
          }
        }
      ],
      "loc": {
        "start": {
          "line": 1,
          "column": 0
        },
        "end": {
          "line": 1,
          "column": 11
        },
        "source": "test1.js"
      }
    },
    {
      "type": "VariableDeclaration",
      "kind": "let",
      "declarations": [
        {
          "type": "VariableDeclarator",
          "id": {
            "type": "Identifier",
            "name": "hey",
            "loc": {
              "start": {
                "line": 1,
                "column": 4
              },
              "end": {
                "line": 1,
                "column": 7
              },
              "source": "test2.js"
            }
          },
          "init": {
            "type": "Literal",
            "value": 2,
            "loc": {
              "start": {
                "line": 1,
                "column": 10
              },
              "end": {
                "line": 1,
                "column": 11
              },
              "source": "test2.js"
            }
          },
          "loc": {
            "start": {
              "line": 1,
              "column": 4
            },
            "end": {
              "line": 1,
              "column": 11
            },
            "source": "test2.js"
          }
        }
      ],
      "loc": {
        "start": {
          "line": 1,
          "column": 0
        },
        "end": {
          "line": 1,
          "column": 12
        },
        "source": "test2.js"
      }
    }
  ],
  "loc": {
    "start": {
      "line": 1,
      "column": 0
    },
    "end": {
      "line": 2,
      "column": 12
    }
  }
}

Expected SourceMap:

{
  version: 3,
  sources: [  'test1.js', 'test2.js' ],
  names: [ 'hi', 'hey' ],
  mappings: 'IAAIA,KAAK;ICALC,MAAM',
  file: 'generated.js',
  sourceRoot: './'
}

Actual behavior

astring does not use the source names inside the loc property and so it makes the sourcemap incorrect.
This is the result of actual behavior:

{
  version: 3,
  sources: [ 'generated.js' ],
  names: [ 'hi', 'hey' ],
  mappings: 'IAAIA,KAAK;IAALC,MAAM',
  file: 'generated.js',
  sourceRoot: './'
}

Fix:

Literally just add mapping.source = node.loc.source; to astring.js at new line 1164. I've already created a small pull request for this: #732

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant