-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
runtime: add a position mapping function
- Loading branch information
1 parent
7293c22
commit fba35fa
Showing
6 changed files
with
210 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,13 +26,16 @@ | |
"author": "Open Function Group <[email protected]>", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@openfn/compiler": "workspace:^", | ||
"@openfn/language-common": "2.0.0-rc3", | ||
"@openfn/lexicon": "workspace:^", | ||
"@types/mock-fs": "^4.13.1", | ||
"@types/node": "^18.15.13", | ||
"@types/semver": "^7.5.0", | ||
"ava": "5.3.1", | ||
"mock-fs": "^5.4.1", | ||
"recast": "^0.21.5", | ||
"ts-node": "^10.9.1", | ||
"tslib": "^2.4.0", | ||
"tsup": "^7.2.0", | ||
"typescript": "^5.1.6" | ||
|
@@ -45,6 +48,7 @@ | |
"dependencies": { | ||
"@openfn/logger": "workspace:*", | ||
"fast-safe-stringify": "^2.1.1", | ||
"semver": "^7.5.4" | ||
"semver": "^7.5.4", | ||
"source-map": "^0.7.4" | ||
} | ||
} |
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,18 @@ | ||
// TODO rename the file to source mapping | ||
// 1 function to get the position | ||
// 1 function to extract the line from the source | ||
|
||
import { SourceMapConsumer } from 'source-map'; | ||
|
||
const getMappedPosition = async (map, line, column) => { | ||
const smc = await new SourceMapConsumer(map); | ||
const pos = smc.originalPositionFor({ | ||
line, | ||
column, | ||
}); | ||
|
||
//return { line, col, src }; | ||
return pos; | ||
}; | ||
|
||
export default getMappedPosition; |
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,43 @@ | ||
import test from 'ava'; | ||
import recast from 'recast'; | ||
import getMappedPosition from '../../src/util/get-mapped-position'; | ||
|
||
const b = recast.types.builders; | ||
|
||
// compile an expression into a function | ||
const compile = (src: string) => { | ||
const ast = recast.parse(src, { | ||
sourceFileName: 'src.js', | ||
}); | ||
|
||
// take the expression and wrap it in a function declaration | ||
const [{ expression }] = ast.program.body; | ||
const fn = b.functionDeclaration( | ||
b.identifier('fn'), | ||
[], | ||
b.blockStatement([b.returnStatement(expression)]) | ||
); | ||
|
||
ast.program.body.push(fn); | ||
|
||
return recast.print(fn, { | ||
sourceMapName: 'src.map.js', | ||
}); | ||
}; | ||
|
||
test('should return a sourcemapped position', async (t) => { | ||
// Compile this simple expression into a function | ||
const { code, map } = compile('x + 1'); | ||
|
||
// Now work out where x is | ||
const lines = code.split('\n'); | ||
const line = 2; // line is 1 based | ||
const column = lines[1].indexOf('x'); | ||
|
||
// now get the uncompiled position for x | ||
const result = await getMappedPosition(map, line, column); | ||
|
||
// now check the position | ||
t.is(result.line, 1); | ||
t.is(result.column, 0); | ||
}); |
Oops, something went wrong.