-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b9e01a
commit 603de58
Showing
7 changed files
with
132 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import Project, { TextChange, SourceFile } from 'ts-simple-ast'; | ||
import * as ts from 'typescript' | ||
import { flat } from './misc'; | ||
|
||
export function createTextChanges(textChanges: ts.TextChange[]): TextChange[] { | ||
return textChanges.map(compilerNode => { | ||
return new (TextChange as any)(compilerNode) // Hack because this constructor in internal | ||
}) | ||
} | ||
|
||
export function applyTextChanges(code: string, textChanges: ts.TextChange[]): string { | ||
const simpleTextChanges = createTextChanges(textChanges) | ||
const sourceFile = applyTextChangesGetSourceFile() | ||
sourceFile.replaceWithText(code) | ||
sourceFile.applyTextChanges(simpleTextChanges) | ||
return sourceFile.getText() | ||
} | ||
|
||
export interface ApplyFileTextChangesResult {modified: SourceFile[], removed: SourceFile[], created: SourceFile[]} | ||
export function applyFileTextChanges(project: Project, fileTextChanges: ts.FileTextChanges[], removeEmpty: boolean = false): ApplyFileTextChangesResult { | ||
const result: ApplyFileTextChangesResult = { | ||
modified: [], | ||
removed: [], | ||
created: [] | ||
} | ||
fileTextChanges.forEach(ftc =>{ | ||
let file = project.getSourceFile(ftc.fileName) | ||
if(ftc.isNewFile && file) { | ||
throw new Error('FileTextChanges instructed to create file '+file+' but it already exists on the project. Aborting!') | ||
} | ||
if(!file && !ftc.isNewFile) { | ||
throw new Error('FileTextChanges instructed to modify existing file '+file+' but it doesn\'t exist. Refusing to create it. Aborting!') | ||
} | ||
let created | ||
if(!file){ | ||
file = project.createSourceFile(ftc.fileName) | ||
result.created.push(file) | ||
created=true | ||
} | ||
file.applyTextChanges(createTextChanges(ftc.textChanges)) | ||
if(!file.getText().trim() && removeEmpty) { | ||
project.removeSourceFile(file) | ||
result.removed.push(file) | ||
} | ||
else if(!created) { | ||
result.modified.push(file) | ||
} | ||
}) | ||
return result | ||
} | ||
|
||
|
||
export function applyCodeFixes(project: Project, codeFixes: ReadonlyArray<ts.CodeFixAction>){ | ||
return applyFileTextChanges(project, flat(codeFixes.map(f=>f.changes))) | ||
} | ||
|
||
|
||
let applyTextChangesSourceFile: SourceFile | ||
|
||
function applyTextChangesGetSourceFile() { | ||
if (!applyTextChangesSourceFile) { | ||
const project = new Project({ useVirtualFileSystem: true }) | ||
applyTextChangesSourceFile = project.createSourceFile('tmp.ts', '') | ||
} | ||
return applyTextChangesSourceFile | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
export function flat<T>(arr: T[][]): T[] { | ||
return arr.reduce((a, b) => a.concat(b)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters