This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 759
Copy function #3970
Merged
Merged
Copy function #3970
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,38 @@ | ||
import { findClosestScope } from "./breakpoint/astBreakpointLocation"; | ||
|
||
function getIndentation(lines) { | ||
const firstLine = lines[0]; | ||
const secondLine = lines[1]; | ||
const lastLine = lines[lines.length - 1]; | ||
|
||
const _getIndentation = line => line && line.match(/^\s*/)[0].length; | ||
|
||
const indentations = [ | ||
_getIndentation(firstLine), | ||
_getIndentation(secondLine), | ||
_getIndentation(lastLine) | ||
]; | ||
|
||
return Math.max(...indentations, 0); | ||
} | ||
|
||
export function findFunctionText(line, source, symbols) { | ||
const func = findClosestScope(symbols.functions, { line, column: Infinity }); | ||
if (!func) { | ||
return null; | ||
} | ||
|
||
const { location: { start, end } } = func; | ||
const lines = source.text.split("\n"); | ||
const firstLine = lines[start.line - 1].slice(start.column); | ||
const lastLine = lines[end.line - 1].slice(0, end.column); | ||
const middle = lines.slice(start.line, end.line - 1); | ||
const functionLines = [firstLine, ...middle, lastLine]; | ||
|
||
const indentation = getIndentation(functionLines); | ||
const formattedLines = functionLines.map(_line => | ||
_line.replace(new RegExp(`^\\s{0,${indentation - 1}}`), "") | ||
); | ||
|
||
return formattedLines.join("\n").trim(); | ||
} |
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,25 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`function findFunctionText finds class function 1`] = ` | ||
"bar() { | ||
2 + 2; | ||
}" | ||
`; | ||
|
||
exports[`function findFunctionText finds function 1`] = ` | ||
"async function exSlowFoo() { | ||
return \\"yay in a bit\\"; | ||
}" | ||
`; | ||
|
||
exports[`function findFunctionText finds function signature 1`] = ` | ||
"async function exSlowFoo() { | ||
return \\"yay in a bit\\"; | ||
}" | ||
`; | ||
|
||
exports[`function findFunctionText finds property function 1`] = ` | ||
"function name() { | ||
2 + 2; | ||
}" | ||
`; |
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,58 @@ | ||
import { findFunctionText } from "../function"; | ||
|
||
import getSymbols from "../parser/getSymbols"; | ||
import { getSource } from "../parser/tests/helpers"; | ||
|
||
describe("function", () => { | ||
describe("findFunctionText", () => { | ||
it("finds function", () => { | ||
const source = getSource("func"); | ||
const symbols = getSymbols(source); | ||
|
||
const text = findFunctionText(14, source, symbols); | ||
expect(text).toMatchSnapshot(); | ||
}); | ||
|
||
it("finds function signature", () => { | ||
const source = getSource("func"); | ||
const symbols = getSymbols(source); | ||
|
||
const text = findFunctionText(13, source, symbols); | ||
expect(text).toMatchSnapshot(); | ||
}); | ||
|
||
it("misses function closing brace", () => { | ||
const source = getSource("func"); | ||
const symbols = getSymbols(source); | ||
|
||
const text = findFunctionText(15, source, symbols); | ||
|
||
// TODO: we should try and match the closing bracket. | ||
expect(text).toEqual(null); | ||
}); | ||
|
||
it("finds property function", () => { | ||
const source = getSource("func"); | ||
const symbols = getSymbols(source); | ||
|
||
const text = findFunctionText(25, source, symbols); | ||
expect(text).toMatchSnapshot(); | ||
}); | ||
|
||
it("finds class function", () => { | ||
const source = getSource("func"); | ||
const symbols = getSymbols(source); | ||
|
||
const text = findFunctionText(29, source, symbols); | ||
expect(text).toMatchSnapshot(); | ||
}); | ||
|
||
it("cant find function", () => { | ||
const source = getSource("func"); | ||
const symbols = getSymbols(source); | ||
|
||
const text = findFunctionText(17, source, symbols); | ||
expect(text).toEqual(null); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@flodolo sorry, that's embarrassing :/