Skip to content

Commit

Permalink
feat: support TS5 extends array (#45)
Browse files Browse the repository at this point in the history
Co-authored-by: hiroki osame <[email protected]>
  • Loading branch information
shun-shobon and privatenumber committed Mar 26, 2023
1 parent d056db8 commit c88b05a
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 96 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -62,8 +62,8 @@
"pkgroll": "^1.8.0",
"slash": "^5.0.0",
"tsx": "^3.12.1",
"type-fest": "^3.4.0",
"typescript": "^4.9.4"
"type-fest": "^3.7.1",
"typescript": "^5.0.2"
},
"eslintConfig": {
"extends": "@pvtnbr/eslint-config",
Expand Down
78 changes: 39 additions & 39 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 67 additions & 53 deletions src/parse-tsconfig/index.ts
Expand Up @@ -4,11 +4,53 @@ import slash from 'slash';
import type { TsConfigJson, TsConfigJsonResolved } from '../types.js';
import { normalizePath } from '../utils/normalize-path.js';
import { readJsonc } from '../utils/read-jsonc.js';
import { resolveExtends } from './resolve-extends.js';
import { resolveExtendsPath } from './resolve-extends-path.js';

export function parseTsconfig(
const resolveExtends = (
extendsPath: string,
directoryPath: string,
) => {
const resolvedExtendsPath = resolveExtendsPath(
extendsPath,
directoryPath,
);

const extendsConfig = parseTsconfig(resolvedExtendsPath);

delete extendsConfig.references;

if (extendsConfig.compilerOptions?.baseUrl) {
const { compilerOptions } = extendsConfig;

compilerOptions.baseUrl = path.relative(
directoryPath,
path.join(path.dirname(resolvedExtendsPath), compilerOptions.baseUrl!),
) || './';
}

if (extendsConfig.files) {
extendsConfig.files = extendsConfig.files.map(
file => path.relative(
directoryPath,
path.join(path.dirname(resolvedExtendsPath), file),
),
);
}

if (extendsConfig.include) {
extendsConfig.include = extendsConfig.include.map(
file => path.relative(
directoryPath,
path.join(path.dirname(resolvedExtendsPath), file),
),
);
}
return extendsConfig;
};

export const parseTsconfig = (
tsconfigPath: string,
): TsConfigJsonResolved {
): TsConfigJsonResolved => {
let realTsconfigPath: string;
try {
realTsconfigPath = fs.realpathSync(tsconfigPath);
Expand All @@ -23,62 +65,34 @@ export function parseTsconfig(
}

if (config.extends) {
const extendsPath = resolveExtends(
config.extends,
directoryPath,
const extendsPathList = (
Array.isArray(config.extends)
? config.extends
: [config.extends]
);

const extendsConfig = parseTsconfig(extendsPath);

delete extendsConfig.references;

if (extendsConfig.compilerOptions?.baseUrl) {
const { compilerOptions } = extendsConfig;

compilerOptions.baseUrl = path.relative(
directoryPath,
path.join(path.dirname(extendsPath), compilerOptions.baseUrl!),
) || './';
}

if (extendsConfig.files) {
extendsConfig.files = extendsConfig.files.map(
file => path.relative(
directoryPath,
path.join(path.dirname(extendsPath), file),
),
);
}

if (extendsConfig.include) {
extendsConfig.include = extendsConfig.include.map(
file => path.relative(
directoryPath,
path.join(path.dirname(extendsPath), file),
),
);
}

delete config.extends;

const merged = {
...extendsConfig,
...config,
for (const extendsPath of extendsPathList.reverse()) {
const extendsConfig = resolveExtends(extendsPath, directoryPath);
const merged = {
...extendsConfig,
...config,

compilerOptions: {
...extendsConfig.compilerOptions,
...config.compilerOptions,
},
};

if (extendsConfig.watchOptions) {
merged.watchOptions = {
...extendsConfig.watchOptions,
...config.watchOptions,
compilerOptions: {
...extendsConfig.compilerOptions,
...config.compilerOptions,
},
};
}

config = merged;
if (extendsConfig.watchOptions) {
merged.watchOptions = {
...extendsConfig.watchOptions,
...config.watchOptions,
};
}
config = merged;
}
}

if (config.compilerOptions) {
Expand Down Expand Up @@ -119,4 +133,4 @@ export function parseTsconfig(
}

return config;
}
};
Expand Up @@ -25,7 +25,7 @@ function resolveFromPackageJsonPath(packageJsonPath: string) {
);
}

export function resolveExtends(
export function resolveExtendsPath(
requestedPath: string,
directoryPath: string,
) {
Expand Down

0 comments on commit c88b05a

Please sign in to comment.