Skip to content

Commit

Permalink
chore(gatsby): Convert stack-trace-utils to TypeScript (#22251)
Browse files Browse the repository at this point in the history
* add @types

* migrate stack-trace-utils to typescript

* fix error type in IConstructError

Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
  • Loading branch information
sasurau4 and pieh authored Mar 14, 2020
1 parent 4d25f4a commit 1513f35
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"@babel/plugin-transform-typescript": "^7.7.4",
"@babel/runtime": "^7.7.7",
"@lerna/prompt": "3.18.5",
"@types/babel__code-frame": "^7.0.1",
"@types/bluebird": "^3.5.29",
"@types/cache-manager": "^2.10.2",
"@types/express": "^4.17.2",
Expand All @@ -14,6 +15,7 @@
"@types/joi": "^14.3.4",
"@types/lodash": "^4.14.149",
"@types/node": "^12.12.11",
"@types/stack-trace": "^0.0.29",
"@types/webpack": "^4.41.0",
"@typescript-eslint/eslint-plugin": "^2.23.0",
"@typescript-eslint/parser": "^2.23.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface IConstructError {
details: {
id?: ErrorId
context?: Record<string, string>
error?: string
error?: Error
[key: string]: unknown
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const stackTrace = require(`stack-trace`)
const { codeFrameColumns } = require(`@babel/code-frame`)
import stackTrace, { StackFrame } from "stack-trace"
import { codeFrameColumns } from "@babel/code-frame"
const fs = require(`fs-extra`)
const path = require(`path`)
const chalk = require(`chalk`)
Expand All @@ -11,7 +11,7 @@ const reduxThunkLocation = path.dirname(
)
const reduxLocation = path.dirname(require.resolve(`redux/package.json`))

const getNonGatsbyCallSite = () =>
const getNonGatsbyCallSite = (): StackFrame | undefined =>
stackTrace
.get()
.find(
Expand All @@ -24,7 +24,16 @@ const getNonGatsbyCallSite = () =>
!isNodeInternalModulePath(callSite.getFileName())
)

const getNonGatsbyCodeFrame = ({ highlightCode = true } = {}) => {
interface ICodeFrame {
fileName: string
line: number
column: number
codeFrame: string
}

export const getNonGatsbyCodeFrame = ({
highlightCode = true,
} = {}): null | ICodeFrame => {
const callSite = getNonGatsbyCallSite()
if (!callSite) {
return null
Expand Down Expand Up @@ -54,7 +63,9 @@ const getNonGatsbyCodeFrame = ({ highlightCode = true } = {}) => {
}
}

const getNonGatsbyCodeFrameFormatted = ({ highlightCode = true } = {}) => {
export const getNonGatsbyCodeFrameFormatted = ({ highlightCode = true } = {}):
| null
| string => {
const possibleCodeFrame = getNonGatsbyCodeFrame({
highlightCode,
})
Expand All @@ -66,8 +77,3 @@ const getNonGatsbyCodeFrameFormatted = ({ highlightCode = true } = {}) => {
const { fileName, line, column, codeFrame } = possibleCodeFrame
return `File ${chalk.bold(`${fileName}:${line}:${column}`)}\n${codeFrame}`
}

module.exports = {
getNonGatsbyCodeFrame,
getNonGatsbyCodeFrameFormatted,
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3778,6 +3778,11 @@
resolved "https://registry.yarnpkg.com/@types/anymatch/-/anymatch-1.3.1.tgz#336badc1beecb9dacc38bea2cf32adf627a8421a"
integrity sha512-/+CRPXpBDpo2RK9C68N3b2cOvO0Cf5B9aPijHsoDQTHivnGSObdOF2BRQOYjojWTDy6nQvMjmqRXIxH55VjxxA==

"@types/babel__code-frame@^7.0.1":
version "7.0.1"
resolved "https://registry.yarnpkg.com/@types/babel__code-frame/-/babel__code-frame-7.0.1.tgz#baf2529c4abbfb5e4008c845efcfe39a187e2f99"
integrity sha512-FFfbQozKxYmOnCKFYV+EQprjBI7u2yaNc2ly/K9AhzyC8MzXtCtSRqptpw+HUJxhwCOo5mLwf1ATmzyhOaVbDg==

"@types/babel__core@^7.1.0":
version "7.1.2"
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.2.tgz#608c74f55928033fce18b99b213c16be4b3d114f"
Expand Down Expand Up @@ -4095,6 +4100,11 @@
resolved "https://registry.yarnpkg.com/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9"
integrity sha512-K5K+yml8LTo9bWJI/rECfIPrGgxdpeNbj+d53lwN4QjW1MCwlkhUms+gtdzigTeUyBr09+u8BwOIY3MXvHdcsA==

"@types/stack-trace@^0.0.29":
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/stack-trace/-/stack-trace-0.0.29.tgz#eb7a7c60098edb35630ed900742a5ecb20cfcb4d"
integrity sha512-TgfOX+mGY/NyNxJLIbDWrO9DjGoVSW9+aB8H2yy1fy32jsvxijhmyJI9fDFgvz3YP4lvJaq9DzdR/M1bOgVc9g==

"@types/stack-utils@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
Expand Down

0 comments on commit 1513f35

Please sign in to comment.