Skip to content

Commit

Permalink
lint-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed Feb 7, 2022
1 parent ae76905 commit d96591e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ export function readConfig(
// Some options are relative to the config file, so must be converted to absolute paths here
if (options.require) {
// Modules are found relative to the tsconfig file, not the `dir` option
const tsconfigRelativeResolver = createProjectLocalResolveHelper(configPath);
const tsconfigRelativeResolver =
createProjectLocalResolveHelper(configPath);
options.require = options.require.map((path: string) =>
tsconfigRelativeResolver(path, false)
);
Expand Down
21 changes: 17 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,8 @@ export function create(rawOptions: CreateOptions = {}): Service {
* be changed by the tsconfig, so we have to do this twice.
*/
function loadCompiler(name: string | undefined, relativeToPath: string) {
const projectLocalResolveHelper = createProjectLocalResolveHelper(relativeToPath);
const projectLocalResolveHelper =
createProjectLocalResolveHelper(relativeToPath);
const compiler = projectLocalResolveHelper(name || 'typescript', true);
const ts: typeof _ts = attemptRequireWithV8CompileCache(require, compiler);
return { compiler, ts, projectLocalResolveHelper };
Expand All @@ -605,7 +606,12 @@ export function create(rawOptions: CreateOptions = {}): Service {
// Compute minimum options to read the config file.
let { compiler, ts, projectLocalResolveHelper } = loadCompiler(
compilerName,
getBasePathForProjectLocalDependencyResolution(undefined, rawOptions.projectSearchDir, rawOptions.project, cwd)
getBasePathForProjectLocalDependencyResolution(
undefined,
rawOptions.projectSearchDir,
rawOptions.project,
cwd
)
);

// Read config file and merge new options between env and CLI options.
Expand All @@ -627,8 +633,15 @@ export function create(rawOptions: CreateOptions = {}): Service {
// Compiler is loaded relative to tsconfig.json, so tsconfig discovery may cause us to load a
// different compiler than we did above, even if the name has not changed.
if (configFilePath) {
({ compiler, ts, projectLocalResolveHelper } = loadCompiler(options.compiler,
getBasePathForProjectLocalDependencyResolution(configFilePath, rawOptions.projectSearchDir, rawOptions.project, cwd)));
({ compiler, ts, projectLocalResolveHelper } = loadCompiler(
options.compiler,
getBasePathForProjectLocalDependencyResolution(
configFilePath,
rawOptions.projectSearchDir,
rawOptions.project,
cwd
)
));
}

// Experimental REPL await is not compatible targets lower than ES2018
Expand Down
15 changes: 12 additions & 3 deletions src/resolver-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ export function createResolverFunctions(kwargs: {
config: _ts.ParsedCommandLine;
projectLocalResolveHelper: ProjectLocalResolveHelper;
}) {
const { host, ts, config, cwd, getCanonicalFileName, projectLocalResolveHelper } =
kwargs;
const {
host,
ts,
config,
cwd,
getCanonicalFileName,
projectLocalResolveHelper,
} = kwargs;
const moduleResolutionCache = ts.createModuleResolutionCache(
cwd,
getCanonicalFileName,
Expand Down Expand Up @@ -137,7 +143,10 @@ export function createResolverFunctions(kwargs: {
// Resolve @types/node relative to project first, then __dirname (copy logic from elsewhere / refactor into reusable function)
let typesNodePackageJsonPath: string | undefined;
try {
typesNodePackageJsonPath = projectLocalResolveHelper('@types/node/package.json', true);
typesNodePackageJsonPath = projectLocalResolveHelper(
'@types/node/package.json',
true
);
} catch {} // gracefully do nothing when @types/node is not installed for any reason
if (typesNodePackageJsonPath) {
const typeRoots = [resolve(typesNodePackageJsonPath, '../..')];
Expand Down
5 changes: 4 additions & 1 deletion src/transpilers/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
// Load swc compiler
let swcInstance: typeof swcWasm;
if (typeof swc === 'string') {
swcInstance = require(projectLocalResolveHelper(swc, true)) as typeof swcWasm;
swcInstance = require(projectLocalResolveHelper(
swc,
true
)) as typeof swcWasm;
} else if (swc == null) {
let swcResolved;
try {
Expand Down
5 changes: 4 additions & 1 deletion src/transpilers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export type TranspilerFactory = (
export interface CreateTranspilerOptions {
// TODO this is confusing because its only a partial Service. Rename?
// Careful: must avoid stripInternal breakage by guarding with Extract<>
service: Pick<Service, Extract<'config' | 'options' | 'projectLocalResolveHelper', keyof Service>>;
service: Pick<
Service,
Extract<'config' | 'options' | 'projectLocalResolveHelper', keyof Service>
>;
}
export interface Transpiler {
// TODOs
Expand Down
22 changes: 17 additions & 5 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,32 @@ export function attemptRequireWithV8CompileCache(
* @internal
*/
export function createProjectLocalResolveHelper(localDirectory: string) {
return function projectLocalResolveHelper(specifier: string, fallbackToTsNodeRelative: boolean) {
return function projectLocalResolveHelper(
specifier: string,
fallbackToTsNodeRelative: boolean
) {
return require.resolve(specifier, {
paths: fallbackToTsNodeRelative ? [localDirectory, __dirname] : [localDirectory]
paths: fallbackToTsNodeRelative
? [localDirectory, __dirname]
: [localDirectory],
});
}
};
}
/** @internal */
export type ProjectLocalResolveHelper = ReturnType<typeof createProjectLocalResolveHelper>;
export type ProjectLocalResolveHelper = ReturnType<
typeof createProjectLocalResolveHelper
>;

/**
* Used as a reminder of all the factors we must consider when finding project-local dependencies and when a config file
* on disk may or may not exist.
* @internal
*/
export function getBasePathForProjectLocalDependencyResolution(configFilePath: string | undefined, projectSearchDirOption: string | undefined, projectOption: string | undefined, cwdOption: string) {
export function getBasePathForProjectLocalDependencyResolution(
configFilePath: string | undefined,
projectSearchDirOption: string | undefined,
projectOption: string | undefined,
cwdOption: string
) {
return configFilePath ?? projectSearchDirOption ?? projectOption ?? cwdOption;
}

0 comments on commit d96591e

Please sign in to comment.