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): Update resolve-module-exports to TS #24519

Merged
merged 1 commit into from
May 27, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

let mockResults = {}

module.exports = input => {
export const resolveModuleExports = (input: unknown): string[] | undefined => {
// return a mocked result
if (typeof input === `string`) {
return mockResults[input]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jest.mock(`gatsby-cli/lib/reporter`, () => {

const fs = require(`fs`)
const reporter = require(`gatsby-cli/lib/reporter`)
const resolveModuleExports = require(`../resolve-module-exports`)
const { resolveModuleExports } = require(`../resolve-module-exports`)
let resolver

describe(`Resolve module exports`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe(`collatePluginAPIs`, () => {
}

beforeEach(() => {
const resolveModuleExports = require(`../../resolve-module-exports`)
const { resolveModuleExports } = require(`../../resolve-module-exports`)
resolveModuleExports(MOCK_RESULTS)
})

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/bootstrap/load-plugins/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const semver = require(`semver`)
const stringSimilarity = require(`string-similarity`)
const { version: gatsbyVersion } = require(`gatsby/package.json`)
const reporter = require(`gatsby-cli/lib/reporter`)
const resolveModuleExports = require(`../resolve-module-exports`)
const { resolveModuleExports } = require(`../resolve-module-exports`)
const { getLatestAPIs } = require(`../../utils/get-latest-apis`)

const getGatsbyUpgradeVersion = entries =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// @flow
import fs from "fs"
import traverse from "@babel/traverse"
import get from "lodash/get"
import { codeFrameColumns } from "@babel/code-frame"
import { codeFrameColumns, SourceLocation } from "@babel/code-frame"
import { babelParseToAst } from "../utils/babel-parse-to-ast"
import report from "gatsby-cli/lib/reporter"

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

const staticallyAnalyzeExports = (modulePath, resolver = require.resolve) => {
let absPath
const exportNames = []
const staticallyAnalyzeExports = (
modulePath: string,
resolver = require.resolve
): string[] => {
let absPath: string | undefined
const exportNames: string[] = []

try {
absPath = resolver(modulePath)
Expand All @@ -28,7 +30,7 @@ const staticallyAnalyzeExports = (modulePath, resolver = require.resolve) => {
const codeFrame = codeFrameColumns(
code,
{
start: err.loc,
start: ((err as unknown) as { loc: SourceLocation["start"] }).loc,
},
{
highlightCode: true,
Expand All @@ -50,7 +52,7 @@ const staticallyAnalyzeExports = (modulePath, resolver = require.resolve) => {
// extract names of exports from file
traverse(ast, {
// Check if the file is using ES6 imports
ImportDeclaration: function ImportDeclaration(astPath) {
ImportDeclaration: function ImportDeclaration() {
isES6 = true
},

Expand All @@ -59,17 +61,17 @@ const staticallyAnalyzeExports = (modulePath, resolver = require.resolve) => {

// get foo from `export const foo = bar`
if (
get(astPath, `node.declaration.type`) === `VariableDeclaration` &&
get(astPath, `node.declaration.declarations[0].id.name`)
declaration?.type === `VariableDeclaration` &&
declaration.declarations[0]?.id.type === `Identifier`
) {
isES6 = true
exportNames.push(declaration.declarations[0].id.name)
}

// get foo from `export function foo()`
if (
get(astPath, `node.declaration.type`) === `FunctionDeclaration` &&
get(astPath, `node.declaration.id.name`)
declaration?.type === `FunctionDeclaration` &&
declaration.id?.type === `Identifier`
) {
isES6 = true
exportNames.push(declaration.id.name)
Expand Down Expand Up @@ -139,16 +141,16 @@ https://gatsby.dev/no-mixed-modules
*
* Returns [] for invalid paths and modules without exports.
*
* @param {string} modulePath
* @param {string} mode
* @param {function} resolver
* @param modulePath
* @param mode
* @param resolver
*/
module.exports = (
modulePath,
export const resolveModuleExports = (
modulePath: string,
{ mode = `analysis`, resolver = require.resolve } = {}
) => {
): string[] => {
if (mode === `require`) {
let absPath
let absPath: string | undefined
try {
absPath = resolver(modulePath)
return Object.keys(require(modulePath)).filter(
Expand Down