Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fixId: tsAction.fixId
}
try {
const res = await this.client.execute('getCombinedCodeFix', args, CancellationToken.None)
if (res.type != 'response') {
return
}
let { body } = res
const edit = typeConverters.WorkspaceEdit.fromFileCodeEdits(
this.client,
body.changes
)
await workspace.applyEdit(edit)
const token = CancellationToken.None
const { commands } = body
if (commands && commands.length) {
for (const command of commands) {
await this.client.execute('applyCodeActionCommand', { command }, token)
}
}
} catch {
// noop
}
}
}
export async function applyCodeActionCommands(
client: ITypeScriptServiceClient,
action: Proto.CodeAction
): Promise {
// make sure there is command
if (action.commands && action.commands.length) {
for (const command of action.commands) {
const response = await client.execute('applyCodeActionCommand', { command }, CancellationToken.None)
if (!response || response.type != 'response' || !response.body) {
return false
}
}
}
return true
}
async function goToProjectConfig(clientHost: TypeScriptServiceClientHost, uri: string): Promise {
if (!clientHost.handles(uri)) {
workspace.showMessage('Could not determine TypeScript or JavaScript project. Unsupported file type', 'warning')
return
}
const client = clientHost.serviceClient
const file = client.toPath(uri)
let res: ProjectInfoResponse | undefined
try {
res = await client.execute('projectInfo', { file, needFileNameList: false }, CancellationToken.None)
} catch {
// noop
}
if (!res || !res.body) {
workspace.showMessage('Could not determine TypeScript or JavaScript project.', 'warning')
return
}
const { configFileName } = res.body
if (configFileName && !isImplicitProjectConfigFile(configFileName)) {
await workspace.openResource(URI.file(configFileName).toString())
return
}
workspace.showMessage('Config file not found', 'warning')
}
async function goToProjectConfig(clientHost: TypeScriptServiceClientHost, uri: string): Promise {
if (!clientHost.handles(uri)) {
workspace.showMessage('Could not determine TypeScript or JavaScript project. Unsupported file type', 'warning')
return
}
const client = clientHost.serviceClient
const file = client.toPath(uri)
let res
try {
res = await client.execute('projectInfo', { file, needFileNameList: false }, CancellationToken.None)
} catch {
// noop
}
if (!res || !res.body) {
workspace.showMessage('Could not determine TypeScript or JavaScript project.', 'warning')
return
}
const { configFileName } = res.body
if (configFileName && !isImplicitProjectConfigFile(configFileName)) {
await workspace.openResource(URI.file(configFileName).toString())
return
}
workspace.showMessage('Config file not found', 'warning')
}
tsAction: Proto.CodeFixAction
): Promise {
if (!tsAction.fixId) {
return
}
const args: Proto.GetCombinedCodeFixRequestArgs = {
scope: {
type: 'file',
args: { file }
},
fixId: tsAction.fixId
}
try {
const res = await this.client.execute('getCombinedCodeFix', args, CancellationToken.None)
if (res.type != 'response') {
return
}
let { body } = res
const edit = typeConverters.WorkspaceEdit.fromFileCodeEdits(
this.client,
body.changes
)
await workspace.applyEdit(edit)
const token = CancellationToken.None
const { commands } = body
if (commands && commands.length) {
for (const command of commands) {
await this.client.execute('applyCodeActionCommand', { command }, token)
public async execute(
document: TextDocument,
file: string,
refactor: string,
action: string,
range: Range
): Promise {
const args: Proto.GetEditsForRefactorRequestArgs = {
...typeConverters.Range.toFileRangeRequestArgs(file, range),
refactor,
action
}
const response = await this.client.execute('getEditsForRefactor', args, CancellationToken.None) as any
const body = response && response.body
if (!body || !body.edits.length) {
return false
}
const workspaceEdit = await this.toWorkspaceEdit(body)
if (!(await workspace.applyEdit(workspaceEdit))) {
return false
}
const renameLocation = body.renameLocation
if (renameLocation) {
commands.executeCommand('editor.action.rename',
document.uri,
typeConverters.Position.fromLocation(renameLocation)
)
}
const response = await this.client.interruptGetErr(() => this.client.execute('organizeImports', args, CancellationToken.None))
if (!response || response.type != 'response' || !response.success) {
const response = await this.client.interruptGetErr(() => {
const args: Proto.GetEditsForFileRenameRequestArgs = {
oldFilePath: oldFile,
newFilePath: newFile,
}
return this.client.execute('getEditsForFileRename', args, CancellationToken.None)
})
if (!response || response.type != 'response' || !response.body) {
async function ensureDependenciesForDocument(
textDocumentUri: URL,
{ tracer, span, token = CancellationToken.None }: { tracer: Tracer; span?: Span; token?: CancellationToken }
): Promise {
await tracePromise('Ensure dependencies', tracer, span, async span => {
throwIfCancelled(token)
const parentPackageRoots = findParentPackageRoots(textDocumentUri)
span.setTag('packageJsonLocations', parentPackageRoots.map(String))
logger.log(`Ensuring dependencies for text document ${textDocumentUri} defined in`, [
...parentPackageRoots.map(String),
])
await Promise.all(
parentPackageRoots.map(async packageRoot => {
await ensureDependenciesForPackageRoot(packageRoot, { tracer, span, token })
})
)
})
}
return new Promise>((resolve, reject) => {
this.client.execute('getSupportedCodeFixes', null, CancellationToken.None).then(res => {
if (res.type !== 'response') {
resolve(new Set())
return
}
let codes = res.body.map(code => +code).filter(code => !isNaN(code))
resolve(new Set(codes))
}, reject)
})
}