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(react-conformance): add new TS config api to be able to specify configName and configDir #27664

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ apps/recipes-react-components @microsoft/cxe-red @microsoft/cxe-coastal @microso
#### Packages
packages/azure-themes @Jacqueline-ms @robtaft-ms
packages/bundle-size @microsoft/teams-prg
packages/react-conformance @microsoft/fluentui-react-build
packages/date-time-utilities @microsoft/cxe-red
packages/eslint-plugin @microsoft/fluentui-react-build
packages/foundation-legacy @microsoft/cxe-red @khmakoto
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "feat: add new TS config api to be able to specify configName and configDir",
"packageName": "@fluentui/react-conformance",
"email": "[email protected]",
"dependentChangeType": "patch"
}
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