Skip to content

Commit

Permalink
chore: cleanup imports in d.ts files (#14663)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 30, 2023
1 parent 5ce83ac commit 94915dd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion scripts/buildUtils.mjs
Expand Up @@ -34,7 +34,7 @@ export const typeOnlyPackages = new Set([
]);

// Get absolute paths of all directories under packages/*
export function getPackages() {
function getPackages() {
const packages = fs
.readdirSync(PACKAGES_DIR)
.map(file => path.resolve(PACKAGES_DIR, file))
Expand Down
40 changes: 24 additions & 16 deletions scripts/bundleTs.mjs
Expand Up @@ -14,16 +14,12 @@ import {
ExtractorConfig,
} from '@microsoft/api-extractor';
import chalk from 'chalk';
import {ESLint} from 'eslint';
import {glob} from 'glob';
import fs from 'graceful-fs';
import pkgDir from 'pkg-dir';
import prettier from 'prettier';
import {rimraf} from 'rimraf';
import {copyrightSnippet, getPackages} from './buildUtils.mjs';

const prettierConfig = await prettier.resolveConfig(
fileURLToPath(import.meta.url).replace(/\.js$/, '.d.ts'),
);
import {copyrightSnippet, getPackagesWithTsConfig} from './buildUtils.mjs';

const require = createRequire(import.meta.url);
const typescriptCompilerFolder = await pkgDir(require.resolve('typescript'));
Expand All @@ -32,13 +28,8 @@ const typesNodeReferenceDirective = '/// <reference types="node" />';

const excludedPackages = new Set(['@jest/globals', '@jest/test-globals']);

const packages = getPackages();

const isTsPackage = p =>
fs.existsSync(path.resolve(p.packageDir, 'tsconfig.json'));

const packagesToBundle = packages.filter(
p => isTsPackage(p) && !excludedPackages.has(p.pkg.name),
const packagesToBundle = getPackagesWithTsConfig().filter(
p => !excludedPackages.has(p.pkg.name),
);

console.log(chalk.inverse(' Extracting TypeScript definition files '));
Expand Down Expand Up @@ -108,6 +99,20 @@ await fs.promises.writeFile(
JSON.stringify(sharedExtractorConfig, null, 2),
);

const eslint = new ESLint({
cwd: process.cwd(),
fix: true,
overrideConfig: {
rules: {
// `d.ts` files are by nature `type` only imports, so it's just noise when looking at the file
'@typescript-eslint/consistent-type-imports': [
'error',
{prefer: 'no-type-imports'},
],
},
},
});

let compilerState;

await Promise.all(
Expand Down Expand Up @@ -205,14 +210,17 @@ await Promise.all(

definitionFile = [
copyrightSnippet,
'',
...definitionFile.split(copyrightSnippet),
].join('\n');

const formattedContent = await prettier.format(definitionFile, {
...prettierConfig,
filepath,
const [lintResult] = await eslint.lintText(definitionFile, {
filePath: 'some-file.ts',
});

// if the autofixer did anything, the result is in `output`
const formattedContent = lintResult.output || definitionFile;

await fs.promises.writeFile(
filepath.replace(
`${path.sep}dist${path.sep}`,
Expand Down

0 comments on commit 94915dd

Please sign in to comment.