Skip to content

Commit 059abae

Browse files
committed
Add code frame util
1 parent 619c403 commit 059abae

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

packages/driver/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"lib"
1818
],
1919
"devDependencies": {
20+
"@babel/code-frame": "^7.0.0",
2021
"@cypress/bower-kendo-ui": "0.0.2",
2122
"@cypress/sinon-chai": "1.1.0",
2223
"@cypress/underscore.inflection": "1.0.1",

packages/driver/src/cypress/utils.coffee

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ _ = require("lodash")
33
methods = require("methods")
44
moment = require("moment")
55
Promise = require("bluebird")
6+
codeFrameColumns = require("@babel/code-frame").codeFrameColumns
67

78
$jquery = require("../dom/jquery")
89
$Location = require("./location")
@@ -184,6 +185,20 @@ module.exports = {
184185

185186
@formatErrorMessage(errMessage, args)
186187

188+
getCodeFrame: (source, path, lineNumber, columnNumber) ->
189+
location = { start: { line: lineNumber, column: columnNumber } }
190+
options = {
191+
highlightCode: true,
192+
forceColor: true
193+
}
194+
195+
return {
196+
frame: codeFrameColumns(source, location, options),
197+
path: path,
198+
lineNumber: lineNumber,
199+
columnNumber: columnNumber
200+
}
201+
187202
normalizeObjWithLength: (obj) ->
188203
## lodash shits the bed if our object has a 'length'
189204
## property so we have to normalize that

packages/driver/test/cypress/integration/cypress/utils_spec.coffee

+45
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,48 @@ describe "driver/src/cypress/utils", ->
7474
expect(err2.docs).to.eq("baz")
7575

7676
expect(err2.stack).to.eq("Error: foo\n\nbar\n" + stack)
77+
78+
context ".getCodeFrame", ->
79+
it "returns a code frame with syntax highlighting", ->
80+
path = "foo/bar/baz"
81+
line = 5
82+
column = 6
83+
src = """
84+
<!DOCTYPE html>
85+
<html>
86+
<body>
87+
<script type="text/javascript">
88+
foo.bar()
89+
</script>
90+
</body>
91+
</html>
92+
"""
93+
94+
{ frame, path, lineNumber, columnNumber } = $utils.getCodeFrame(src, path, line, column)
95+
96+
expect(frame).to.contain("foo")
97+
expect(frame).to.contain("bar()")
98+
expect(frame).to.contain("[0m")
99+
expect(path).to.eq("foo/bar/baz")
100+
expect(lineNumber).to.eq(5)
101+
expect(columnNumber).to.eq(6)
102+
103+
## TODO determine if we want more failure cases covered
104+
it "fails gracefully", ->
105+
path = "foo/bar/baz"
106+
line = 100
107+
column = 6
108+
src = """
109+
<!DOCTYPE html>
110+
<html>
111+
<body>
112+
<script type="text/javascript">
113+
foo.bar()
114+
</script>
115+
</body>
116+
</html>
117+
"""
118+
119+
{ frame } = $utils.getCodeFrame(src, path, line, column)
120+
121+
expect(frame).to.eq("")

0 commit comments

Comments
 (0)