Skip to content

Commit

Permalink
Move requireResolve call to generic prop access visitor + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Sep 13, 2023
1 parent ec2e450 commit a51a77c
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 6 deletions.
Empty file.
6 changes: 6 additions & 0 deletions fixtures/imports-prop-access-call/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require.resolve('./exists.js');

if ('serviceWorker' in navigator) {
// TODO Implement?
// navigator.serviceWorker.register('worker3.js');
}
3 changes: 3 additions & 0 deletions fixtures/imports-prop-access-call/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "imports-prop-access-call"
}
4 changes: 2 additions & 2 deletions src/typescript/ast-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ export function isRequireCall(callExpression: ts.Node): callExpression is ts.Cal
return args.length === 1;
}

export function isRequireResolveCall(node: ts.Node): node is ts.CallExpression {
export function isPropertyAccessCall(node: ts.Node, identifier: string): node is ts.CallExpression {
return (
ts.isCallExpression(node) &&
ts.isPropertyAccessExpression(node.expression) &&
node.expression.getText() === 'require.resolve'
node.expression.getText() === identifier
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/typescript/visitors/imports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import importCall from './importCall.js';
import importDeclaration from './importDeclaration.js';
import importEqualsDeclaration from './importEqualsDeclaration.js';
import jsDocType from './jsDocType.js';
import propertyAccessCall from './propertyAccessCall.js';
import reExportDeclaration from './reExportDeclaration.js';
import requireCall from './requireCall.js';
import requireResolveCall from './requireResolveCall.js';

const visitors = [
importCall,
importDeclaration,
importEqualsDeclaration,
jsDocType,
propertyAccessCall,
reExportDeclaration,
requireCall,
requireResolveCall,
];

export default (sourceFile: ts.SourceFile) => visitors.map(v => v(sourceFile));
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import ts from 'typescript';
import { isRequireResolveCall } from '../../ast-helpers.js';
import { isPropertyAccessCall } from '../../ast-helpers.js';
import { importVisitor as visit } from '../index.js';

export default visit(
() => true,
node => {
if (isRequireResolveCall(node)) {
if (isPropertyAccessCall(node, 'require.resolve')) {
// Pattern: require.resolve('specifier')
if (node.arguments[0] && ts.isStringLiteralLike(node.arguments[0])) {
const specifier = node.arguments[0].text;
Expand Down
21 changes: 21 additions & 0 deletions tests/imports-prop-access-call.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { main } from '../src/index.js';
import { resolve } from '../src/util/path.js';
import baseArguments from './helpers/baseArguments.js';
import baseCounters from './helpers/baseCounters.js';

const cwd = resolve('fixtures/imports-prop-access-call');

test('Support various prop access calls', async () => {
const { counters } = await main({
...baseArguments,
cwd,
});

assert.deepEqual(counters, {
...baseCounters,
processed: 2,
total: 2,
});
});

0 comments on commit a51a77c

Please sign in to comment.