Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Codebase Services TextChanges

Nathan Shively-Sanders edited this pageSep 2, 2022 ·1 revision

Text Changes

The majority of this file is devoted to a class called theChangeTracker. This class is nearly alwayscreated viaChangeTracker.with where you would give it a context object.

Here is an example context object:

{  cancellationToken:CancellationTokenObject{cancellationToken:TestCancellationToken}  errorCode:2304  formatContext:Object{options:Object,getRule:}  host:NativeLanguageServiceHost{cancellationToken:TestCancellationToken,settings:Object,sys:System,}  preferences:Object{}  program:Object{getRootFileNames:,getSourceFile:,getSourceFileByPath:,}  sourceFile:SourceFileObject{pos:0,end:7,flags:65536,}  span:Object{start:0,length:6}}

You only really seeChangeTrack in use within the codefixes and refactors given that the other case whereTypeScript emits files is a single operation of emission.

The change tracker keeps track of individual changes to be applied to a file. There arecurrently four mainAPIs that it works with:type Change = ReplaceWithSingleNode | ReplaceWithMultipleNodes | RemoveNode | ChangeText;

TheChangeTrack class is then used to provide high level API to describe the sort of changes you might want tomake, which eventually fall into one of the four categories above.

Making Changes

The end result of using aChangeTrack object is an array ofFileTextChanges objects. TheChangeTrack.withfunction lets you work with a tracker instance elsewhere and passes back theChangeTrack objects.

The core work in generating changes occurs in:

Going from an AST node to text is done by creating aprinter ingetNonformattedText. The printerreturns an unformatted node, which is then ran through [a formatter][./formatting.md] and the raw stringsubstitution is done in [applyChanges][9].

Changes look like this:

[{fileName:"/b.js",textChanges:[{span:{start:0,length:0},newText:"// @ts-ignore\n"}]}];

Writing

newFileChanges handles passing the set of

Want to contribute to this Wiki?

Fork it and send a pull request.

News

Debugging TypeScript

Contributing to TypeScript

Building Tools for TypeScript

FAQs

The Main Repo

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp