From b699a76a6bc0e96a54c392456d0f618e8164145f Mon Sep 17 00:00:00 2001 From: feugy Date: Mon, 16 Jan 2023 09:57:49 +0100 Subject: [PATCH] chore(feature-detector): renames findDependencies into findGlobals --- docs/pages/packages/feature-detector.mdx | 7 +- .../{find-dependencies.ts => find-globals.ts} | 31 ++++++-- packages/feature-detector/src/index.ts | 2 +- ...endencies.test.ts => find-globals.test.ts} | 77 ++++++++----------- 4 files changed, 58 insertions(+), 59 deletions(-) rename packages/feature-detector/src/{find-dependencies.ts => find-globals.ts} (57%) rename packages/feature-detector/test/{find-dependencies.test.ts => find-globals.test.ts} (57%) diff --git a/docs/pages/packages/feature-detector.mdx b/docs/pages/packages/feature-detector.mdx index b5a9dcd8a..4dc58ada3 100644 --- a/docs/pages/packages/feature-detector.mdx +++ b/docs/pages/packages/feature-detector.mdx @@ -47,10 +47,7 @@ In the code snippet bellow, we're checking: 2. globals used, that are not provided by the Edge Runtime ```ts -import { - hasEdgeSignature, - findDependencies, -} from '@edge-runtime/feature-detector' +import { hasEdgeSignature, findGlobals } from '@edge-runtime/feature-detector' const sourceFilePath = './test.js' // could be TypeScript as well. Must be in current working directory @@ -59,7 +56,7 @@ if (hasEdgeSignature(sourceFilePath)) { console.log(`${sourcefilePath} can run on the edge`) } // 2 -console.log(findDependencies(sourceFilePath).globals) +console.log(findGlobals(sourceFilePath)) ``` ## API diff --git a/packages/feature-detector/src/find-dependencies.ts b/packages/feature-detector/src/find-globals.ts similarity index 57% rename from packages/feature-detector/src/find-dependencies.ts rename to packages/feature-detector/src/find-globals.ts index a2be50b2c..3e4c8eaa3 100644 --- a/packages/feature-detector/src/find-dependencies.ts +++ b/packages/feature-detector/src/find-globals.ts @@ -1,19 +1,35 @@ -import { Project, ts } from 'ts-morph' +import { dirname, resolve } from 'path' +import { Project, SourceFile, ts } from 'ts-morph' import { buildProject } from './utils/project' /** * Find the list of globals used by source files in the provided project. * Analyzed source files can be filtered by provided a list of glob patterns (default to all TypeScript and JavaScript files, excluding type definitions) */ -export function findDependencies( +export function findGlobals( sourcePath: string, project: Project = buildProject() -): { - globals: string[] -} { +): string[] { const globals = new Set() - const sourceFile = project.addSourceFileAtPath(sourcePath) - const program = project.getProgram().compilerObject + const sourceFile = project.getSourceFileOrThrow(sourcePath) + addFileGlobals(sourceFile, globals) + for (const imported of sourceFile.getImportDeclarations()) { + console.log(imported.getModuleSpecifierSourceFileOrThrow()) + const importedFile = project.getSourceFile( + resolve( + dirname(sourceFile.getFilePath()), + imported.getModuleSpecifierValue() + ) + ) + if (importedFile) { + addFileGlobals(importedFile, globals) + } + } + return [...globals] +} + +function addFileGlobals(sourceFile: SourceFile, globals: Set) { + const program = sourceFile.getProject().getProgram().compilerObject const diagnostics = program.getSemanticDiagnostics(sourceFile.compilerNode) for (const { code, messageText } of diagnostics) { if ( @@ -33,5 +49,4 @@ export function findDependencies( } } } - return { globals: [...globals] } } diff --git a/packages/feature-detector/src/index.ts b/packages/feature-detector/src/index.ts index 691e986cf..0d4f5d468 100644 --- a/packages/feature-detector/src/index.ts +++ b/packages/feature-detector/src/index.ts @@ -1,2 +1,2 @@ -export * from './find-dependencies' +export * from './find-globals' export * from './has-edge-signature' diff --git a/packages/feature-detector/test/find-dependencies.test.ts b/packages/feature-detector/test/find-globals.test.ts similarity index 57% rename from packages/feature-detector/test/find-dependencies.test.ts rename to packages/feature-detector/test/find-globals.test.ts index fa68f1d17..9442c2189 100644 --- a/packages/feature-detector/test/find-dependencies.test.ts +++ b/packages/feature-detector/test/find-globals.test.ts @@ -1,7 +1,6 @@ -import { readFile } from 'fs/promises' import { join } from 'path' -import { Project, ScriptKind, SourceFile, SyntaxKind } from 'ts-morph' -import { findDependencies } from '../src' +import { Project, SourceFile } from 'ts-morph' +import { findGlobals } from '../src' import { buildProject } from '../src/utils/project' const fixtureFolder = join(__dirname, 'fixtures') @@ -9,7 +8,7 @@ const fixtureFolder = join(__dirname, 'fixtures') describe.each([ { title: 'for JavaScript' }, { title: 'for TypeScript', isTS: true }, -])('findDependencies() $title', ({ isTS }) => { +])('findGlobals() $title', ({ isTS }) => { let project: Project let file: SourceFile @@ -23,9 +22,10 @@ describe.each([ __filename process.env['TEST'] `) - expect(findDependencies(file.getFilePath(), project)).toEqual({ - globals: ['__filename', 'process'], - }) + expect(findGlobals(file.getFilePath(), project)).toEqual([ + '__filename', + 'process', + ]) }) it('returns globals variable with typeof', () => { @@ -34,18 +34,14 @@ describe.each([ console.log('in node') } `) - expect(findDependencies(file.getFilePath(), project)).toEqual({ - globals: ['process'], - }) + expect(findGlobals(file.getFilePath(), project)).toEqual(['process']) }) it('returns globals variables used as parameters', () => { file.replaceWithText(` console.log('in node', __dirname) `) - expect(findDependencies(file.getFilePath(), project)).toEqual({ - globals: ['__dirname'], - }) + expect(findGlobals(file.getFilePath(), project)).toEqual(['__dirname']) }) it('returns globals variables in instructions', () => { @@ -54,9 +50,10 @@ describe.each([ for (const key in exports) {} } `) - expect(findDependencies(file.getFilePath(), project)).toEqual({ - globals: ['__dirname', 'exports'], - }) + expect(findGlobals(file.getFilePath(), project)).toEqual([ + '__dirname', + 'exports', + ]) }) it('returns globals used with static methods', () => { @@ -65,23 +62,17 @@ describe.each([ return Response.redirect(Buffer.from('ok')) } `) - expect(findDependencies(file.getFilePath(), project)).toEqual({ - globals: ['Buffer'], - }) + expect(findGlobals(file.getFilePath(), project)).toEqual(['Buffer']) }) it('returns globals used with new operator', () => { file.replaceWithText(`new Buffer(['o', 'k'])`) - expect(findDependencies(file.getFilePath(), project)).toEqual({ - globals: ['Buffer'], - }) + expect(findGlobals(file.getFilePath(), project)).toEqual(['Buffer']) }) it('returns globals used as properties', () => { file.replaceWithText(`Buffer.poolSize`) - expect(findDependencies(file.getFilePath(), project)).toEqual({ - globals: ['Buffer'], - }) + expect(findGlobals(file.getFilePath(), project)).toEqual(['Buffer']) }) it('returns globals used as functions', () => { @@ -90,9 +81,10 @@ describe.each([ $('.do-you[remember="the time"]') }) `) - expect(findDependencies(file.getFilePath(), project)).toEqual({ - globals: ['setImmediate', '$'], - }) + expect(findGlobals(file.getFilePath(), project)).toEqual([ + 'setImmediate', + '$', + ]) }) it('ignores known DOM globals', () => { @@ -100,9 +92,8 @@ describe.each([ console.log(JSON.stringify({ msg: btoa('hi') })) const controller = new AbortController() `) - expect(findDependencies(file.getFilePath(), project)).toEqual({ - globals: [], // no console, JSON, btoa, AbortController - }) + // no console, JSON, btoa, AbortController + expect(findGlobals(file.getFilePath(), project)).toEqual([]) }) it('dedupes identified globals', () => { @@ -115,24 +106,20 @@ describe.each([ } } `) - expect(findDependencies(file.getFilePath(), project)).toEqual({ - globals: ['Buffer'], - }) + expect(findGlobals(file.getFilePath(), project)).toEqual(['Buffer']) }) - it('find globals from 3rd party code', async () => { + it('finds globals from 3rd party code', async () => { const file = project.addSourceFileAtPath( join(fixtureFolder, 'with-axios.out.js') ) - expect(findDependencies(file.getFilePath(), project)).toEqual({ - globals: [ - 'navigator', - 'window', - 'document', - 'Buffer', - 'XMLHttpRequest', - 'process', - ], - }) + expect(findGlobals(file.getFilePath(), project)).toEqual([ + 'navigator', + 'window', + 'document', + 'Buffer', + 'XMLHttpRequest', + 'process', + ]) }) })