Skip to content

Commit

Permalink
Work on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Aug 21, 2023
1 parent a3c51c6 commit 8274555
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 29 deletions.
4 changes: 2 additions & 2 deletions docs/scripts/formattedTSDemos.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ async function transpileFile(tsxPath, project) {

async function main(argv) {
const { watch: watchMode, disableCache, pattern } = argv;
const buildProject = createTypescriptProjectBuilder(CORE_TYPESCRIPT_PROJECTS);

// TODO: Remove at some point.
// Though not too soon so that it isn't disruptive.
Expand All @@ -157,7 +156,8 @@ async function main(argv) {
...(await getFiles(path.join(workspaceRoot, 'docs/data'))), // new structure
].filter((fileName) => filePattern.test(fileName));

const project = buildProject('docs');
const buildProject = createTypescriptProjectBuilder(CORE_TYPESCRIPT_PROJECTS);
const project = buildProject('docs', { files: tsxFiles });

let successful = 0;
let failed = 0;
Expand Down
2 changes: 1 addition & 1 deletion packages/api-docs-builder/buildApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ async function run(argv: yargs.ArgumentsCamelCase<CommandOptions>) {
let allBuilds: Array<PromiseSettledResult<ReactApi | null>> = [];
await SETTINGS.reduce(async (resolvedPromise, setting) => {
await resolvedPromise;
const projects = setting.typeScriptProjects.map(buildTypeScriptProject);
const projects = setting.typeScriptProjects.map((project) => buildTypeScriptProject(project));
const apiPagesManifestPath = setting.output.apiManifestPath;

const manifestDir = apiPagesManifestPath.match(/(.*)\/[^/]+\./)?.[1];
Expand Down
11 changes: 9 additions & 2 deletions packages/api-docs-builder/utils/createTypeScriptProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export interface CreateTypeScriptProjectOptions {
* The path must be relative to the root path.
*/
entryPointPath?: string;
/**
* Files to include in the project.
* By default, it will use the files defined in the tsconfig.
*/
files?: string[];
}

export const createTypeScriptProject = (
Expand All @@ -35,6 +40,7 @@ export const createTypeScriptProject = (
rootPath,
tsConfigPath: inputTsConfigPath = 'tsconfig.build.json',
entryPointPath: inputEntryPointPath,
files,
} = options;

const tsConfigPath = path.join(rootPath, inputTsConfigPath);
Expand Down Expand Up @@ -65,7 +71,7 @@ export const createTypeScriptProject = (
}

const program = ts.createProgram({
rootNames: tsConfigFileContent.fileNames,
rootNames: files ?? tsConfigFileContent.fileNames,
options: tsConfigFileContent.options,
});

Expand Down Expand Up @@ -99,7 +105,7 @@ export const createTypescriptProjectBuilder = <P extends string>(
) => {
const projects = new Map<P, TypeScriptProject>();

return (projectName: P) => {
return (projectName: P, options: { files?: string[] } = {}) => {
const cachedProject = projects.get(projectName);
if (cachedProject != null) {
return cachedProject;
Expand All @@ -111,6 +117,7 @@ export const createTypescriptProjectBuilder = <P extends string>(
const project = createTypeScriptProject({
name: projectName,
...projectsConfig[projectName],
...options,
});

projects.set(projectName, project);
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-to-proptypes/src/generatePropTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import _ from 'lodash';
import { PropTypeDefinition, PropTypesComponent, PropType, LiteralType } from './models';
import { createDOMElementType, createBooleanType, uniqueUnionTypes } from './createType';

export interface GenerateOptions {
export interface GeneratePropTypesOptions {
/**
* If source itself written in typescript prop-types disable prop-types validation
* by injecting propTypes as
Expand Down Expand Up @@ -99,7 +99,7 @@ function defaultSortLiteralUnions(a: LiteralType, b: LiteralType) {
*/
export function generatePropTypes(
component: PropTypesComponent,
options: GenerateOptions = {},
options: GeneratePropTypesOptions = {},
): string {
const {
disablePropTypesTypeChecking = false,
Expand Down
3 changes: 3 additions & 0 deletions packages/typescript-to-proptypes/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export { getPropTypesFromFile } from './getPropTypesFromFile';
export type { GetPropTypesFromFileOptions } from './getPropTypesFromFile';
export { injectPropTypesInFile } from './injectPropTypesInFile';
export type { InjectPropTypesInFileOptions } from './injectPropTypesInFile';
export { generatePropTypes } from './generatePropTypes';
export type { GeneratePropTypesOptions } from './generatePropTypes';
4 changes: 2 additions & 2 deletions packages/typescript-to-proptypes/src/injectPropTypesInFile.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as babel from '@babel/core';
import * as babelTypes from '@babel/types';
import { v4 as uuid } from 'uuid';
import { generatePropTypes, GenerateOptions } from './generatePropTypes';
import { generatePropTypes, GeneratePropTypesOptions } from './generatePropTypes';
import { PropTypesComponent, PropTypeDefinition, LiteralType } from './models';

export interface InjectPropTypesInFileOptions
extends Pick<
GenerateOptions,
GeneratePropTypesOptions,
| 'sortProptypes'
| 'includeJSDoc'
| 'comment'
Expand Down
12 changes: 8 additions & 4 deletions packages/typescript-to-proptypes/test/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { ParserOptions, GenerateOptions, InjectOptions } from '../src';
import {
InjectPropTypesInFileOptions,
GetPropTypesFromFileOptions,
GeneratePropTypesOptions,
} from '../src';

export type TestOptions = {
parser?: Partial<ParserOptions>;
generator?: Partial<GenerateOptions>;
injector?: Partial<InjectOptions>;
parser?: GetPropTypesFromFileOptions;
generator?: GeneratePropTypesOptions;
injector?: InjectPropTypesInFileOptions;
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import path from 'path';
import fs from 'fs';
import * as ts from 'typescript';
import { expect } from 'chai';
import glob from 'fast-glob';
import prettier from 'prettier';
import * as ttp from '../src';
import {
TypeScriptProject,
createTypescriptProjectBuilder,
} from '@mui-internal/api-docs-builder/utils/createTypeScriptProject';
import { generatePropTypes } from '../src/generatePropTypes';
import { injectPropTypesInFile } from '../src/injectPropTypesInFile';
import { getPropTypesFromFile } from '../src/getPropTypesFromFile';
import { TestOptions } from './types';

const testCases = glob
Expand All @@ -23,26 +30,38 @@ const testCases = glob
});

describe('typescript-to-proptypes', () => {
let cachedProgram: ttp.ts.Program;
function getProgram() {
return cachedProgram;
let cachedProject: TypeScriptProject;
function getProject() {
return cachedProject;
}

before(function beforeHook() {
// Creating a TS program might take a while.
this.timeout(20000);

const buildProject = createTypescriptProjectBuilder({
test: {
rootPath: path.join(__dirname, '..'),
tsConfigPath: 'tsconfig.json',
},
});

cachedProject = buildProject('test', {
files: testCases.map((testCase) => testCase.inputPath),
});

// Create program for all files to speed up tests
cachedProgram = ttp.createTSProgram(
testCases.map((testCase) => testCase.inputPath),
ttp.loadConfig(path.resolve(__dirname, '../tsconfig.json')),
);
// cachedProject = ttp.createTSProgram(
// testCases.map((testCase) => testCase.inputPath),
// ttp.loadConfig(path.resolve(__dirname, '../tsconfig.json')),
// );
});

testCases.forEach((testCase) => {
const { name: testName, inputPath, inputJS, outputPath } = testCase;

it(testName, async () => {
const program = getProgram();
const project = getProject();
let options: TestOptions = {};
try {
const optionsModule = await import(`./${testName}/options`);
Expand All @@ -51,7 +70,7 @@ describe('typescript-to-proptypes', () => {
// Assume "Cannot find module" which means "no options".
}

const ast = ttp.parseFromProgram(inputPath, program, options.parser);
const components = getPropTypesFromFile({ filePath: inputPath, project, ...options.parser });

let inputSource = null;
if (inputPath.endsWith('.d.ts')) {
Expand All @@ -61,25 +80,29 @@ describe('typescript-to-proptypes', () => {
// ignore
}
} else {
inputSource = ttp.ts.transpileModule(fs.readFileSync(inputPath, 'utf8'), {
inputSource = ts.transpileModule(fs.readFileSync(inputPath, 'utf8'), {
compilerOptions: {
target: ttp.ts.ScriptTarget.ESNext,
jsx: ttp.ts.JsxEmit.Preserve,
target: ts.ScriptTarget.ESNext,
jsx: ts.JsxEmit.Preserve,
},
}).outputText;
}

let result = '';
// For d.ts files we just generate the AST
if (!inputSource) {
result = ast.body
result = components
.map((component) => {
return ttp.generate(component, options.generator);
return generatePropTypes(component, options.generator);
})
.join('\n');
} else {
// For .tsx? files we transpile them and inject the proptypes
const injected = ttp.inject(ast, inputSource, options.injector);
const injected = injectPropTypesInFile({
components,
target: inputSource,
options: options.injector,
});
if (!injected) {
throw new Error('Injection failed');
}
Expand Down

0 comments on commit 8274555

Please sign in to comment.