TypeScript Compiler API

LanguageService.findRenameLocations()

Returns spans for a symbol rename and, when requested and relevant, prefixText and suffixText needed to preserve syntax such as shorthand property names. It does not modify documents. Apply validated edits per file in descending offset order against the matching snapshot; check the new identifier and conflicts, then recheck the edited project. The simple fixture has no prefix/suffix and avoids unrelated shadowed symbols. Baseline: TypeScript 5.8.3 on Node.js. This is a tooling script, not an editor extension or a ByteMind CodeRunner example. Host documents are in memory with filesystem fallback for compiler libraries; the host is not a production project manager.

Formal Syntax & Structure

typescript syntaxtypescript
service.findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean, preferences: ts.UserPreferences): readonly ts.RenameLocation[] | undefined

Parameters & Attributes

NameTypeDescription
fileNamestringAbsolute path identifying the document in the host.
positionnumberZero-based UTF-16 code-unit offset in that document snapshot.
findInStringsbooleanInclude supported string occurrences.
findInCommentsbooleanInclude supported comment occurrences.
preferencests.UserPreferencesRename preferences.

Working Example

Example Snippettypescript
// Use a new empty directory. Install: npm install --save-exact typescript@5.8.3// File: example.cjs (omit this label)const ts = require("typescript");const assert = require("node:assert/strict");const path = require("node:path");// Project documents live in memory; compiler libraries use the local filesystem.function workspace(initial) {  const files = new Map(Object.entries(initial).map(([name, text]) =>    [path.resolve(name), { text, version: 0 }]));  const options = { strict: true, target: ts.ScriptTarget.ES2022,    module: ts.ModuleKind.CommonJS, types: [], noEmit: true };  const key = name => path.resolve(name);  const read = name => files.get(key(name))?.text ?? ts.sys.readFile(name);  const host = {    getCompilationSettings: () => options,    getScriptFileNames: () => [...files.keys()],    getScriptVersion: name => String(files.get(key(name))?.version ?? 0),    getScriptSnapshot: name => { const text = read(name);      return text === undefined ? undefined : ts.ScriptSnapshot.fromString(text); },    getCurrentDirectory: () => process.cwd(),    getDefaultLibFileName: opts => ts.getDefaultLibFilePath(opts),    fileExists: name => files.has(key(name)) || ts.sys.fileExists(name),    readFile: read,    readDirectory: ts.sys.readDirectory,    directoryExists: ts.sys.directoryExists,    getDirectories: ts.sys.getDirectories,    useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,    getNewLine: () => "\n"  };  function edit(name, text) {    const file = files.get(key(name));    assert(file, "edit requires an existing file");    files.set(key(name), { text, version: file.version + 1 });  }  return { host, edit, read, key };}const w = workspace({"main.ts": "const count = 1;\nconst total = count + count;\nfunction other() { const count = 9; return count; }"});const file = w.key("main.ts");const service = ts.createLanguageService(w.host);try {  const locations = service.findRenameLocations(file, w.read(file).indexOf("count"), false, false, { providePrefixAndSuffixTextForRename: true });  assert.equal(locations.length, 3);  let text = w.read(file);  for (const loc of [...locations].sort((a,b)=>b.textSpan.start-a.textSpan.start)) {    assert.equal(loc.fileName, file);    const {start,length} = loc.textSpan;    text = text.slice(0,start) + (loc.prefixText ?? "") + "amount" + (loc.suffixText ?? "") + text.slice(start+length);  }  assert(text.includes("const total = amount + amount;"));  assert(text.includes("const count = 9; return count;"));  w.edit("main.ts", text);  assert.deepEqual(service.getSemanticDiagnostics(file), []);  console.log("3 edits; shadow preserved; clean");} finally { service.dispose(); }
// Run: node example.cjs// Expected stdout: 3 edits; shadow preserved; clean
TypeScript Reference