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: support TS5 extends array #45

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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