Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gatsby): Convert babel-parse-to-ast to TypeScript #22480

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/gatsby/src/bootstrap/resolve-module-exports.js
@@ -1,10 +1,10 @@
// @flow
const fs = require(`fs`)
const traverse = require(`@babel/traverse`).default
const get = require(`lodash/get`)
const { codeFrameColumns } = require(`@babel/code-frame`)
const { babelParseToAst } = require(`../utils/babel-parse-to-ast`)
const report = require(`gatsby-cli/lib/reporter`)
import fs from "fs"
import traverse from "@babel/traverse"
import get from "lodash/get"
import { codeFrameColumns } from "@babel/code-frame"
import { babelParseToAst } from "../utils/babel-parse-to-ast"
blainekasten marked this conversation as resolved.
Show resolved Hide resolved
import report from "gatsby-cli/lib/reporter"

import { testRequireError } from "../utils/test-require-error"

Expand Down
Expand Up @@ -67,7 +67,6 @@ Array [
"column": 0,
"line": 6,
},
"filename": true,
"start": Position {
"column": 33,
"line": 2,
Expand Down Expand Up @@ -144,7 +143,6 @@ query PageQueryName {
"column": 0,
"line": 6,
},
"filename": true,
"start": Position {
"column": 26,
"line": 2,
Expand Down Expand Up @@ -221,7 +219,6 @@ query PageQueryIndirect {
"column": 0,
"line": 6,
},
"filename": true,
"start": Position {
"column": 26,
"line": 2,
Expand Down Expand Up @@ -294,7 +291,6 @@ query PageQueryIndirect2 {
"column": 0,
"line": 6,
},
"filename": true,
"start": Position {
"column": 31,
"line": 2,
Expand Down Expand Up @@ -371,7 +367,6 @@ query {
"column": 48,
"line": 4,
},
"filename": true,
"start": Position {
"column": 19,
"line": 4,
Expand Down Expand Up @@ -440,7 +435,6 @@ query {
"column": 26,
"line": 4,
},
"filename": true,
"start": Position {
"column": 19,
"line": 4,
Expand Down Expand Up @@ -513,7 +507,6 @@ query {
"column": 48,
"line": 4,
},
"filename": true,
"start": Position {
"column": 19,
"line": 4,
Expand Down Expand Up @@ -582,7 +575,6 @@ query {
"column": 26,
"line": 4,
},
"filename": true,
"start": Position {
"column": 19,
"line": 4,
Expand Down Expand Up @@ -655,7 +647,6 @@ query {
"column": 48,
"line": 4,
},
"filename": true,
"start": Position {
"column": 19,
"line": 4,
Expand Down Expand Up @@ -728,7 +719,6 @@ query {
"column": 60,
"line": 8,
},
"filename": true,
"start": Position {
"column": 33,
"line": 8,
Expand Down Expand Up @@ -873,7 +863,6 @@ query {
"column": 0,
"line": 9,
},
"filename": true,
"start": Position {
"column": 35,
"line": 2,
Expand Down Expand Up @@ -966,7 +955,6 @@ query {
"column": 0,
"line": 14,
},
"filename": true,
"start": Position {
"column": 33,
"line": 10,
Expand Down Expand Up @@ -1093,7 +1081,6 @@ query {
"column": 0,
"line": 12,
},
"filename": true,
"start": Position {
"column": 22,
"line": 4,
Expand Down Expand Up @@ -1251,7 +1238,6 @@ query {
"column": 69,
"line": 4,
},
"filename": true,
"start": Position {
"column": 22,
"line": 4,
Expand Down Expand Up @@ -1401,7 +1387,6 @@ query {
"column": 82,
"line": 5,
},
"filename": true,
"start": Position {
"column": 33,
"line": 5,
Expand Down Expand Up @@ -1474,7 +1459,6 @@ query {
"column": 67,
"line": 3,
},
"filename": true,
"start": Position {
"column": 38,
"line": 3,
Expand Down Expand Up @@ -1547,7 +1531,6 @@ query {
"column": 67,
"line": 4,
},
"filename": true,
"start": Position {
"column": 38,
"line": 4,
Expand Down Expand Up @@ -1620,7 +1603,6 @@ query {
"column": 81,
"line": 3,
},
"filename": true,
"start": Position {
"column": 52,
"line": 3,
Expand Down Expand Up @@ -1693,7 +1675,6 @@ query {
"column": 82,
"line": 3,
},
"filename": true,
"start": Position {
"column": 53,
"line": 3,
Expand Down Expand Up @@ -1843,7 +1824,6 @@ query {
"column": 69,
"line": 4,
},
"filename": true,
"start": Position {
"column": 22,
"line": 4,
Expand Down Expand Up @@ -1912,7 +1892,6 @@ query {
"column": 51,
"line": 3,
},
"filename": true,
"start": Position {
"column": 38,
"line": 3,
Expand Down Expand Up @@ -1981,7 +1960,6 @@ query {
"column": 32,
"line": 4,
},
"filename": true,
"start": Position {
"column": 19,
"line": 4,
Expand Down
@@ -1,12 +1,11 @@
/* @flow */
const parser = require(`@babel/parser`)
import { parse, ParserOptions } from "@babel/parser"
import { File } from "@babel/types"

const PARSER_OPTIONS = {
const PARSER_OPTIONS: ParserOptions = {
allowImportExportEverywhere: true,
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
sourceType: `unambigious`,
sourceFilename: true,
sourceType: `unambiguous`,
plugins: [
`jsx`,
`flow`,
Expand Down Expand Up @@ -43,20 +42,20 @@ const PARSER_OPTIONS = {
],
}

export function getBabelParserOptions(filePath: string) {
export function getBabelParserOptions(filePath: string): ParserOptions {
// Flow and TypeScript plugins can't be enabled simultaneously
if (/\.tsx?/.test(filePath)) {
const { plugins } = PARSER_OPTIONS
return {
...PARSER_OPTIONS,
plugins: plugins.map(plugin =>
plugins: plugins!.map(plugin =>
plugin === `flow` ? `typescript` : plugin
),
}
}
return PARSER_OPTIONS
}

export function babelParseToAst(contents: string, filePath: string) {
return parser.parse(contents, getBabelParserOptions(filePath))
export function babelParseToAst(contents: string, filePath: string): File {
return parse(contents, getBabelParserOptions(filePath))
}