Skip to content

Commit

Permalink
feat: remove moduleResolver API (#6609)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Mar 13, 2023
1 parent e8cdd5c commit f0f45a9
Show file tree
Hide file tree
Showing 15 changed files with 2 additions and 282 deletions.
20 changes: 0 additions & 20 deletions docs/architecture/Parser.mdx
Expand Up @@ -43,7 +43,6 @@ interface ParserOptions {
jsxFragmentName?: string | null;
jsxPragma?: string | null;
lib?: string[];
moduleResolver?: string;
program?: import('typescript').Program;
project?: string | string[] | true;
projectFolderIgnoreList?: string[];
Expand Down Expand Up @@ -150,25 +149,6 @@ Specifies the TypeScript `lib`s that are available. This is used by the scope an

If you provide `parserOptions.project`, you do not need to set this, as it will automatically detected from the compiler.

### `moduleResolver`

> Default `undefined`.
This option allows you to provide a custom module resolution. The value should point to a JS file that default exports (`export default`, or `module.exports =`, or `export =`) a file with the following interface:

```ts
interface ModuleResolver {
version: 1;
resolveModuleNames(
moduleNames: string[],
containingFile: string,
reusedNames: string[] | undefined,
redirectedReference: ts.ResolvedProjectReference | undefined,
options: ts.CompilerOptions,
): (ts.ResolvedModule | undefined)[];
}
```

### `program`

> Default `undefined`.
Expand Down
22 changes: 0 additions & 22 deletions docs/architecture/TypeScript-ESTree.mdx
Expand Up @@ -241,11 +241,6 @@ interface ParseAndGenerateServicesOptions extends ParseOptions {
*/
glob?: number | 'Infinity';
};

/**
* Path to a file exporting a custom `ModuleResolver`.
*/
moduleResolver?: string;
}

interface ParserServices {
Expand Down Expand Up @@ -290,23 +285,6 @@ const { ast, services } = parseAndGenerateServices(code, {
});
```

##### `ModuleResolver`

The `moduleResolver` option allows you to specify the path to a module with a custom module resolver implementation. The module is expected to adhere to the following interface:

```ts
interface ModuleResolver {
version: 1;
resolveModuleNames(
moduleNames: string[],
containingFile: string,
reusedNames: string[] | undefined,
redirectedReference: ts.ResolvedProjectReference | undefined,
options: ts.CompilerOptions,
): (ts.ResolvedModule | undefined)[];
}
```

#### `parseWithNodeMaps(code, options)`

Parses the given string of code with the options provided and returns both the ESTree-compatible AST as well as the node maps.
Expand Down
1 change: 0 additions & 1 deletion packages/types/src/parser-options.ts
Expand Up @@ -59,7 +59,6 @@ interface ParserOptions {
tokens?: boolean;
tsconfigRootDir?: string;
warnOnUnsupportedTypeScriptVersion?: boolean;
moduleResolver?: string;
cacheLifetime?: {
glob?: CacheDurationSeconds;
};
Expand Down
Expand Up @@ -4,10 +4,7 @@ import * as ts from 'typescript';

import type { ParseSettings } from '../parseSettings';
import type { ASTAndDefiniteProgram } from './shared';
import {
createDefaultCompilerOptionsFromExtra,
getModuleResolver,
} from './shared';
import { createDefaultCompilerOptionsFromExtra } from './shared';

const log = debug('typescript-eslint:typescript-estree:createDefaultProgram');

Expand Down Expand Up @@ -47,12 +44,6 @@ function createDefaultProgram(
/* setParentNodes */ true,
);

if (parseSettings.moduleResolver) {
compilerHost.resolveModuleNames = getModuleResolver(
parseSettings.moduleResolver,
).resolveModuleNames;
}

const oldReadFile = compilerHost.readFile;
compilerHost.readFile = (fileName: string): string | undefined =>
path.normalize(fileName) === path.normalize(parseSettings.filePath)
Expand Down
Expand Up @@ -10,7 +10,6 @@ import {
createDefaultCompilerOptionsFromExtra,
createHash,
getCanonicalFileName,
getModuleResolver,
} from './shared';
import type { WatchCompilerHostOfConfigFile } from './WatchCompilerHostOfConfigFile';

Expand Down Expand Up @@ -268,12 +267,6 @@ function createWatchProgram(
/*reportWatchStatus*/ () => {},
) as WatchCompilerHostOfConfigFile<ts.BuilderProgram>;

if (parseSettings.moduleResolver) {
watchCompilerHost.resolveModuleNames = getModuleResolver(
parseSettings.moduleResolver,
).resolveModuleNames;
}

// ensure readFile reads the code being linted instead of the copy on disk
const oldReadFile = watchCompilerHost.readFile;
watchCompilerHost.readFile = (filePathIn, encoding): string | undefined => {
Expand Down
19 changes: 0 additions & 19 deletions packages/typescript-estree/src/create-program/shared.ts
Expand Up @@ -2,7 +2,6 @@ import path from 'path';
import type { Program } from 'typescript';
import * as ts from 'typescript';

import type { ModuleResolver } from '../parser-options';
import type { ParseSettings } from '../parseSettings';

interface ASTAndNoProgram {
Expand Down Expand Up @@ -112,23 +111,6 @@ function getAstFromProgram(
return ast && { ast, program: currentProgram };
}

function getModuleResolver(moduleResolverPath: string): ModuleResolver {
let moduleResolver: ModuleResolver;

try {
moduleResolver = require(moduleResolverPath) as ModuleResolver;
} catch (error) {
const errorLines = [
'Could not find the provided parserOptions.moduleResolver.',
'Hint: use an absolute path if you are not in control over where the ESLint instance runs.',
];

throw new Error(errorLines.join('\n'));
}

return moduleResolver;
}

/**
* Hash content for compare content.
* @param content hashed contend
Expand All @@ -154,5 +136,4 @@ export {
ensureAbsolutePath,
getCanonicalFileName,
getAstFromProgram,
getModuleResolver,
};
Expand Up @@ -68,7 +68,6 @@ export function createParseSettings(
: options.loggerFn === false
? (): void => {}
: console.log, // eslint-disable-line no-console
moduleResolver: options.moduleResolver ?? '',
preserveNodeMaps: options.preserveNodeMaps !== false,
programs: Array.isArray(options.programs) ? options.programs : null,
projects: [],
Expand Down
5 changes: 0 additions & 5 deletions packages/typescript-estree/src/parseSettings/index.ts
Expand Up @@ -93,11 +93,6 @@ export interface MutableParseSettings {
*/
log: (message: string) => void;

/**
* Path for a module resolver to use for the compiler host's `resolveModuleNames`.
*/
moduleResolver: string;

/**
* Whether two-way AST node maps are preserved during the AST conversion process.
*/
Expand Down
16 changes: 0 additions & 16 deletions packages/typescript-estree/src/parser-options.ts
Expand Up @@ -188,11 +188,6 @@ interface ParseAndGenerateServicesOptions extends ParseOptions {
*/
glob?: CacheDurationSeconds;
};

/**
* Path to a file exporting a custom `ModuleResolver`.
*/
moduleResolver?: string;
}

export type TSESTreeOptions = ParseAndGenerateServicesOptions;
Expand Down Expand Up @@ -228,14 +223,3 @@ export interface ParserServicesWithoutTypeInformation
export type ParserServices =
| ParserServicesWithTypeInformation
| ParserServicesWithoutTypeInformation;

export interface ModuleResolver {
version: 1;
resolveModuleNames(
moduleNames: string[],
containingFile: string,
reusedNames: string[] | undefined,
redirectedReference: ts.ResolvedProjectReference | undefined,
options: ts.CompilerOptions,
): (ts.ResolvedModule | undefined)[];
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit f0f45a9

Please sign in to comment.