Skip to content

Commit

Permalink
feat(react-conformance): add new TS config api to be able to specify …
Browse files Browse the repository at this point in the history
…configName and configDir
  • Loading branch information
Hotell committed Mar 8, 2023
1 parent 3c7c88e commit 5466766
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
18 changes: 16 additions & 2 deletions packages/react-conformance/src/isConformant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,28 @@ import { getComponentDoc } from './utils/getComponentDoc';

export function isConformant<TProps = {}>(...testInfo: Partial<IsConformantOptions<TProps>>[]) {
const mergedOptions = merge<IsConformantOptions>(...testInfo);
const { componentPath, displayName, disabledTests = [], extraTests, tsconfigDir } = mergedOptions;

const {
componentPath,
displayName,
disabledTests = [],
extraTests,
tsConfig,
// eslint-disable-next-line deprecation/deprecation
tsconfigDir,
} = mergedOptions;

const mergedTsConfig = {
configDir: tsConfig?.configDir ?? tsconfigDir,
configName: tsConfig?.configName,
};

describe('isConformant', () => {
if (!fs.existsSync(componentPath)) {
throw new Error(`Path ${componentPath} does not exist`);
}

const tsProgram = createTsProgram(componentPath, tsconfigDir);
const tsProgram = createTsProgram(componentPath, mergedTsConfig);

const components = getComponentDoc(componentPath, tsProgram);
const mainComponents = components.filter(comp => comp.displayName === displayName);
Expand Down
6 changes: 6 additions & 0 deletions packages/react-conformance/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,16 @@ export interface IsConformantOptions<TProps = {}> {
primarySlot?: keyof TProps | 'root';

/**
* @deprecated - use `tsConfig` property
*
* Test will load the first tsconfig.json file working upwards from `tsconfigDir`.
* @defaultvalue the directory of the component being tested
*/
tsconfigDir?: string;
/**
* replaces tsconfigDir
*/
tsConfig?: Partial<{ configName: string; configDir: string }>;
}

export type ConformanceTest<TProps = {}> = (
Expand Down
17 changes: 11 additions & 6 deletions packages/react-conformance/src/utils/createTsProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@ import * as ts from 'typescript';
let program: ts.Program;

/**
* Creates a cached TS Program.
* Creates a ~cached~ TS Program.
* @remarks this will be never cached with current use/setup as jest creates this for every it/describe() block 🐌
*/
export function createTsProgram(componentPath: string, tsconfigDir?: string): ts.Program {
export function createTsProgram(
sourcePath: string,
options: Partial<{ configDir: string; configName: string }> = {},
): ts.Program {
const { configName, configDir } = options;
if (!program) {
// Calling parse() from react-docgen-typescript would create a new ts.Program for every component,
// which can take multiple seconds in a large project. For better performance, we create a single
// ts.Program per package and pass it to parseWithProgramProvider().

const tsconfigPath = ts.findConfigFile(tsconfigDir ?? componentPath, fs.existsSync);
const tsconfigPath = ts.findConfigFile(configDir ?? sourcePath, fs.existsSync, configName);

if (!tsconfigPath) {
throw new Error('Cannot find tsconfig.json');
throw new Error(`Cannot find ${configName}`);
}

const compilerOptions = getCompilerOptions(tsconfigPath);
Expand All @@ -33,9 +38,9 @@ export function createTsProgram(componentPath: string, tsconfigDir?: string): ts
program = ts.createProgram([rootFile], compilerOptions);
}

if (!program.getSourceFile(componentPath)) {
if (!program.getSourceFile(sourcePath)) {
// See earlier comment for why it's handled this way (can reconsider if it becomes a problem)
throw new Error(`Component file "${componentPath}" does not appear to be referenced from the project index file`);
throw new Error(`Component file "${sourcePath}" does not appear to be referenced from the project index file`);
}

return program;
Expand Down

0 comments on commit 5466766

Please sign in to comment.