Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function createWorkspaceEditForIgnore(
doc: sourcegraph.TextDocument,
diagnostic: sourcegraph.Diagnostic,
edit = new sourcegraph.WorkspaceEdit()
): sourcegraph.WorkspaceEdit {
// TODO!(sqs): get indent of previous line - in vscode this is inserted on the client
// automatically, check out how they do it because that seems neat
// (https://sourcegraph.com/github.com/microsoft/vscode-tslint@30d1a7ae25b0331466f1a54b4f7d23d60fa2da30/-/blob/tslint-server/src/tslintServer.ts#L618)
const range = diagnostic.range // TODO!(sqs): get other duplication instance too
const startIndent = doc.text.slice(doc.offsetAt(range.start.with(undefined, 0))).match(/[ \t]*/)
edit.insert(
new URL(doc.uri),
range.start.with(undefined, 0),
`${startIndent ? startIndent[0] : ''}// jscpd:ignore-start\n`
)
const endIndent = doc.text.slice(doc.offsetAt(range.end.with(undefined, 0))).match(/[ \t]*/)
edit.insert(
function computeIgnoreEdit(
diag: sourcegraph.Diagnostic,
doc: sourcegraph.TextDocument,
edit = new sourcegraph.WorkspaceEdit()
): sourcegraph.WorkspaceEdit {
const { binding, module } = getDiagnosticData(diag)
for (const range of findMatchRanges(doc.text, binding, module)) {
edit.insert(
new URL(doc.uri),
range.end,
' // sourcegraph:ignore-line React lint https://sourcegraph.example.com/ofYRz6NFzj'
)
}
return edit
}
vars: {
repositoryNames: context.repositoryNames || ['github.com/sd9/guava19to21-sample'],
matchTemplate: context.matchTemplate,
rule: context.rule,
rewriteTemplate: context.rewriteTemplate,
},
})
if (errors && errors.length > 0) {
throw new Error(`GraphQL response error: ${errors[0].message}`)
}
const canonicalURLs: string[] = data.comby.results.map(
(r: any) => `git://${r.file.commit.repository.name}?${r.file.commit.oid}#${r.file.path}`
)
const docs = await Promise.all(canonicalURLs.map(url => sourcegraph.workspace.openTextDocument(new URL(url))))
const edit = new sourcegraph.WorkspaceEdit()
for (const [i, doc] of docs.entries()) {
if (doc.text!.length > 15000) {
continue // TODO!(sqs): skip too large
}
edit.set(new URL(doc.uri), [sourcegraph.TextEdit.patch(data.comby.results[i].rawDiff)])
}
return (edit as any).toJSON()
}
function computeRemoveDependencyEdit(
diag: sourcegraph.Diagnostic,
doc: sourcegraph.TextDocument,
edit = new sourcegraph.WorkspaceEdit()
): sourcegraph.WorkspaceEdit {
const dep = getDiagnosticData(diag)
const range = findDependencyMatchRange(doc.text, dep.name)
// TODO!(sqs): assumes dependency key-value is all on one line and only appears once
edit.delete(
new URL(doc.uri),
new sourcegraph.Range(
range.start.with({ character: 0 }),
range.end.with({ line: range.end.line + 1, character: 0 })
)
)
return edit
}
function computeRemoveDependencyEdit(
diag: sourcegraph.Diagnostic,
doc: sourcegraph.TextDocument,
edit = new sourcegraph.WorkspaceEdit()
): sourcegraph.WorkspaceEdit {
const dep = getDiagnosticData(diag)
const range = findDependencyMatchRange(doc.text, dep.name)
// TODO!(sqs): assumes dependency key-value is all on one line and only appears once
edit.delete(
new URL(doc.uri),
new sourcegraph.Range(
range.start.with({ character: 0 }),
range.end.with({ line: range.end.line + 1, character: 0 })
)
)
return edit
}
export const editPackageJson = (
doc: sourcegraph.TextDocument,
operations: { path: Segment[]; value: any }[],
workspaceEdit = new sourcegraph.WorkspaceEdit()
): sourcegraph.WorkspaceEdit => {
for (const op of operations) {
const edits = setProperty(doc.text!, op.path, op.value, PACKAGE_JSON_FORMATTING_OPTIONS)
for (const edit of edits) {
workspaceEdit.replace(
new URL(doc.uri),
new sourcegraph.Range(doc.positionAt(edit.offset), doc.positionAt(edit.offset + edit.length)),
edit.content
)
}
}
return workspaceEdit
}
function computeFixEdit(
diag: sourcegraph.Diagnostic,
doc: sourcegraph.TextDocument,
edit = new sourcegraph.WorkspaceEdit()
): sourcegraph.WorkspaceEdit {
if (!doc.text.includes('1.13.x')) {
const ranges = findMatchRanges(doc.text, /^go:/g)
if (ranges.length > 0) {
for (const range of ranges) {
edit.insert(new URL(doc.uri), range.end, `\n - "1.13.x"`)
}
} else {
edit.insert(new URL(doc.uri), doc.positionAt(doc.text.length), `\n\ngo:\n - "1.13.x"\n`)
}
}
return edit
}
function computeFixEdit(
diag: sourcegraph.Diagnostic,
doc: sourcegraph.TextDocument,
edit = new sourcegraph.WorkspaceEdit()
): sourcegraph.WorkspaceEdit {
const { binding, module } = getDiagnosticData(diag)
for (const range of findMatchRanges(doc.text, binding, module)) {
edit.replace(new URL(doc.uri), range, `import ${binding} from '${module}'`)
}
return edit
}
async function computeFixAllActionsFromDiagnostics(
diagnostics: sourcegraph.Diagnostic[]
): Promise> {
const edit = new sourcegraph.WorkspaceEdit()
for (const diag of diagnostics) {
const doc = await sourcegraph.workspace.openTextDocument(diag.resource)
computeFixEdit(diag, doc, edit)
}
return { edit, diagnostics: diagnostics }
}
catchError(err => {
const edit = new WorkspaceEdit()
edit.addDiagnostic({
resource: decl.location.uri,
range: new Range(0, 0, 0, 0),
severity: DiagnosticSeverity.Warning,
message: 'Error regenerating dependencies.lock (`gradle generateLock saveLock`)',
detail:
'```text\n' +
(err.message as string).replace(/^editForCommands.*$/m, '').trim() +
'\n```',
})
return [edit]
})
)