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

feat: add analysis.transformAST hook #396

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions readme.md
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
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
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
@@ -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
@@ -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
@@ -0,0 +1,2 @@
import { x } from '#x';
console.log(x);
4 changes: 4 additions & 0 deletions test/unit/imports-transform-ast/output.js
@@ -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
@@ -0,0 +1,4 @@
{
"name": "x",
"type": "module"
}