Skip to content

Commit

Permalink
feat: add analysis.transformAST hook
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Mar 1, 2024
1 parent 5b25e72 commit 241cba6
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 2 deletions.
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ const { fileList } = await nodeFileTrace(files, {
computeFileReferences: true,
// evaluate known bindings to assist with glob and file reference analysis
evaluatePureExpressions: true,
// optional hook into the analysis step to inspect or modify the generated AST
transformAST: async (path, ast) => {
await walk(ast, {
async enter(node) {
if (
node.type === 'ImportDeclaration' &&
node.source.value === 'foo'
) {
this.remove();
}
},
});
},
},
});
```
Expand Down
4 changes: 4 additions & 0 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ export default async function analyze(
}
}

if (job.analysis.transformAST) {
await job.analysis.transformAST(id, ast);
}

const importMetaUrl = pathToFileURL(id).href;

const knownBindings: Record<
Expand Down
2 changes: 2 additions & 0 deletions src/node-file-trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import analyze, { AnalyzeResult } from './analyze';
import resolveDependency, { NotFoundError } from './resolve-dependency';
import { isMatch } from 'micromatch';
import { sharedLibEmit } from './utils/sharedlib-emit';
import { Node } from './utils/types';
import { join } from 'path';
import { CachedFileSystem } from './fs';

Expand Down Expand Up @@ -61,6 +62,7 @@ export class Job {
emitGlobs?: boolean;
computeFileReferences?: boolean;
evaluatePureExpressions?: boolean;
transformAST?: (path: string, node: Node) => Promise<void>;
};
private analysisCache: Map<string, AnalyzeResult>;
public fileList: Set<string>;
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Job } from './node-file-trace';
import { Node } from './utils/types';

export interface Stats {
isFile(): boolean;
Expand Down Expand Up @@ -41,6 +42,7 @@ export interface NodeFileTraceOptions {
emitGlobs?: boolean;
computeFileReferences?: boolean;
evaluatePureExpressions?: boolean;
transformAST?: (path: string, node: Node) => Promise<void>;
};
cache?: any;
paths?: Record<string, string>;
Expand Down
23 changes: 21 additions & 2 deletions test/unit.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs');
const { join, relative } = require('path');
const { asyncWalk } = require('estree-walker');
const { nodeFileTrace } = require('../out/node-file-trace');
const gracefulFS = require('graceful-fs');
const analyze = require('../out/analyze.js').default;
Expand Down Expand Up @@ -122,6 +123,25 @@ for (const { testName, isRoot } of unitTests) {
inputFileNames.push('input-2.js', 'input-3.js', 'input-4.js');
}

// disable analysis for basic-analysis unit tests
let analysis = !testName.startsWith('basic-analysis');

if (testName === 'imports-transform-ast') {
analysis = {
transformAST: async (path, ast) => {
expect(path).toEqual(join(unitPath, 'input.js'));

await asyncWalk(ast, {
async enter(node) {
if (node.type === 'ImportDeclaration') {
this.remove();
}
},
});
},
};
}

const { fileList, reasons } = await nodeFileTrace(
inputFileNames.map((file) => join(unitPath, file)),
{
Expand All @@ -135,8 +155,7 @@ for (const { testName, isRoot } of unitTests) {
exportsOnly: testName.startsWith('exports-only'),
ts: true,
log: true,
// disable analysis for basic-analysis unit tests
analysis: !testName.startsWith('basic-analysis'),
analysis,
mixedModules: true,
// Ignore unit test output "actual.js", and ignore GitHub Actions preinstalled packages
ignore: (str) =>
Expand Down
2 changes: 2 additions & 0 deletions test/unit/imports-transform-ast/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { x } from '#x';
console.log(x);
4 changes: 4 additions & 0 deletions test/unit/imports-transform-ast/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"test/unit/imports-transform-ast/input.js",
"test/unit/imports-transform-ast/package.json"
]
4 changes: 4 additions & 0 deletions test/unit/imports-transform-ast/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "x",
"type": "module"
}

0 comments on commit 241cba6

Please sign in to comment.