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

ts.createSourceFile trims leading newlines, causing stored line numbers to mismatch #58517

Closed
Artur- opened this issue May 13, 2024 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@Artur-
Copy link

Artur- commented May 13, 2024

πŸ”Ž Search Terms

createSourceFile newlines trim

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about createSourceFile

⏯ Playground Link

No response

πŸ’» Code

import React from 'react';

export default function HelloWorldView() {
  return <span>Hello</span>;
}

πŸ™ Actual behavior

Given this TSX file, React tooling will store a source reference on the <span> element as e.g.

$0.__reactFiber$wzqrbgzp6o._debugSource

and it will contain

{
columnNumber: 10,
fileName: ".../something.tsx",
lineNumber: 7
}

However, if you feed the TSX file into ts.createSourceFile and look for an element on line 7, you will not find anything because the file has been changed to

import React from 'react';

export default function HelloWorldView() {
  return <span>Hello</span>;
}

and it only has 5 lines

πŸ™‚ Expected behavior

ts.createSourceFile does no transformations whatsoever on the given input

Additional information about the issue

No response

@RyanCavanaugh
Copy link
Member

TS obviously preserves the line information enough to e.g. give you correct line numbers on the commandline when showing errors. Can you clarify which API you're calling and how?

@Artur-
Copy link
Author

Artur- commented May 13, 2024

Not sure this is a meaningful test, reproducing what is shown in a larger case is always difficult. I will dig more into this tomorrow and ensure the problem is not elsewhere. Anyway, given a.ts





const ts = require("typescript");
const { readFileSync } = require("fs");

const sourceText = readFileSync("a.ts", {encoding: 'utf-8'});
const sourceFile = ts.createSourceFile('test.tsx', sourceText, ts.ScriptTarget.Latest, true);
console.log(sourceFile.getText());





and package.json

{
  "name": "tstest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "typescript": "^5.4.5"
  }
}

then running

node_modules/.bin/tsc a.ts && node a

outputs

const ts = require("typescript");
const { readFileSync } = require("fs");

const sourceText = readFileSync("a.ts", {encoding: 'utf-8'});
const sourceFile = ts.createSourceFile('test.tsx', sourceText, ts.ScriptTarget.Latest, true);
console.log(sourceFile.getText());




which includes trailing newlines but not leading ones.

@RyanCavanaugh
Copy link
Member

Use getFullText() for that operation

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label May 13, 2024
@Artur-
Copy link
Author

Artur- commented May 14, 2024

Ok, so now that I am aware of getFullText() I am guessing that the main problem is the use of getStart() and getEnd() instead of getFullStart() and getFullWidth() in our code. Where should I have been able to find out about full vs non-full versions of the methods?

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants