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
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
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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();
}
},
});
},
},
});
```
4 changes: 4 additions & 0 deletions src/analyze.ts
Original file line number Diff line number Diff line change
@@ -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<
2 changes: 2 additions & 0 deletions src/node-file-trace.ts
Original file line number Diff line number Diff line change
@@ -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';

@@ -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>;
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;
@@ -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>;
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;
@@ -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)),
{
@@ -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) =>
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"
}