Skip to content

Commit

Permalink
feat(react-conformance): add tsconfigDir config + change tsconfig def…
Browse files Browse the repository at this point in the history
…ault dir to componentPath (#21113)

* add tsconfigDir + change tsconfig default path

* Change files

* update comment

* update comment
  • Loading branch information
YuanboXue-Amber authored Jan 6, 2022
1 parent d48236f commit 18549da
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "add option 'tsconfigDir'; change default tsconfig dir to component path",
"packageName": "@fluentui/react-conformance",
"email": "[email protected]",
"dependentChangeType": "patch"
}
4 changes: 2 additions & 2 deletions packages/react-conformance/src/isConformant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import { getComponentDoc } from './utils/getComponentDoc';

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

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

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

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 @@ -94,6 +94,12 @@ export interface IsConformantOptions<TProps = {}> {
* This is 'root' by default, and only needs to be specified if it's a slot other than 'root'.
*/
primarySlot?: keyof TProps | 'root';

/**
* Test will load the first tsconfig.json file working upwards from `tsconfigDir`.
* @defaultvalue the directory of the component being tested
*/
tsconfigDir?: string;
}

export type ConformanceTest<TProps = {}> = (
Expand Down
4 changes: 2 additions & 2 deletions packages/react-conformance/src/utils/createTsProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ let program: ts.Program;
/**
* Creates a cached TS Program.
*/
export function createTsProgram(componentPath: string): ts.Program {
export function createTsProgram(componentPath: string, tsconfigDir?: string): ts.Program {
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(process.cwd(), fs.existsSync);
const tsconfigPath = ts.findConfigFile(tsconfigDir ?? componentPath, fs.existsSync);

if (!tsconfigPath) {
throw new Error('Cannot find tsconfig.json');
Expand Down

0 comments on commit 18549da

Please sign in to comment.