Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
}
if (!inGemSpecsSection) {
continue
}
const DEP_INDENT = ' '
const TRANSITIVE_DEP_INDENT = ' '
if (line.startsWith(DEP_INDENT) && !line.startsWith(TRANSITIVE_DEP_INDENT)) {
// Dependency.
const m = line.match(/^\s*(.+) \((.+)\)$/)
if (!m) {
throw new Error(`invalid Gemfile.lock dependency line ${i}: ${JSON.stringify(line)}`)
}
const gemName = m[1]
const gemVersion = m[2]
const range = new sourcegraph.Range(
new sourcegraph.Position(i, ' '.length),
new sourcegraph.Position(i, line.length)
)
// A gem might be listed as an indirect dep before being listed as a direct dep.
let gem = byName.get(gemName)
if (gem) {
gem.direct = true
gem.range = range
gem.version = gemVersion
} else {
gem = {
name: gemName,
version: gemVersion,
direct: true,
range,
function createWorkspaceEditFromESLintFix(
doc: sourcegraph.TextDocument,
fix: Rule.Fix,
edit = new sourcegraph.WorkspaceEdit()
): sourcegraph.WorkspaceEdit {
edit.replace(
new URL(doc.uri),
new sourcegraph.Range(doc.positionAt(fix.range[0]), doc.positionAt(fix.range[1])),
fix.text
)
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
}
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 findTokenRanges(text: string, pattern: RegExp): { range: sourcegraph.Range; token: string }[] {
const results: { range: sourcegraph.Range; token: string }[] = []
for (const [i, line] of text.split('\n').entries()) {
const match = pattern.exec(line)
if (match && match[2]) {
const startCharacter = match.index + match[1].length
results.push({
range: new sourcegraph.Range(i, startCharacter, i, startCharacter + match[2].length),
token: match[2],
})
}
}
return results
}
function nodeToLocation(node: LocationConnectionNode): sourcegraph.Location {
return {
uri: new sourcegraph.URI(
`git://${node.resource.repository.name}?${node.resource.commit.oid}#${node.resource.path}`
),
range: new sourcegraph.Range(
node.range.start.line,
node.range.start.character,
node.range.end.line,
node.range.end.character
),
}
}
function rangeForLintMessage(doc: sourcegraph.TextDocument, m: Linter.LintMessage): sourcegraph.Range {
if (m.line === undefined && m.column === undefined) {
return new sourcegraph.Range(0, 0, 1, 0)
}
const start = new sourcegraph.Position(m.line - 1, m.column - 1)
let end: sourcegraph.Position
if (m.endLine === undefined && m.endColumn === undefined) {
const wordRange = doc.getWordRangeAtPosition(start)
end = wordRange ? wordRange.end : start
} else {
end = new sourcegraph.Position(m.endLine - 1, m.endColumn - 1)
}
return new sourcegraph.Range(start, end)
}
function duplicationRange({ start, end }: { start: ITokenLocation; end: ITokenLocation }): sourcegraph.Range {
return new sourcegraph.Range(
new sourcegraph.Position(start.line, start.column || 0),
new sourcegraph.Position(end.line, end.column || 0)
)
}
function rangeForLintMessage(doc: sourcegraph.TextDocument, m: Linter.LintMessage): sourcegraph.Range {
if (m.line === undefined && m.column === undefined) {
return new sourcegraph.Range(0, 0, 1, 0)
}
const start = new sourcegraph.Position(m.line - 1, m.column - 1)
let end: sourcegraph.Position
if (m.endLine === undefined && m.endColumn === undefined) {
const wordRange = doc.getWordRangeAtPosition(start)
end = wordRange ? wordRange.end : start
} else {
end = new sourcegraph.Position(m.endLine - 1, m.endColumn - 1)
}
return new sourcegraph.Range(start, end)
}