Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(react): fix react library generator for strict option (#5249)
  • Loading branch information
puku0x committed May 3, 2021
1 parent 6cb1dc6 commit 321815d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
29 changes: 10 additions & 19 deletions packages/react/src/generators/library/library.spec.ts
Expand Up @@ -92,6 +92,14 @@ describe('lib', () => {
path: './tsconfig.spec.json',
},
]);
expect(tsconfigJson.compilerOptions.strict).not.toBeDefined();
expect(
tsconfigJson.compilerOptions.forceConsistentCasingInFileNames
).not.toBeDefined();
expect(tsconfigJson.compilerOptions.noImplicitReturns).not.toBeDefined();
expect(
tsconfigJson.compilerOptions.noFallthroughCasesInSwitch
).not.toBeDefined();
});

it('should extend the local tsconfig.json with tsconfig.spec.json', async () => {
Expand Down Expand Up @@ -580,12 +588,12 @@ describe('lib', () => {
});

describe('--strict', () => {
it('should update the projects tsconfig with strict true', async () => {
it('should update tsconfig.json', async () => {
await libraryGenerator(appTree, {
...defaultSchema,
strict: true,
});
const tsconfigJson = readJson(appTree, '/libs/my-lib/tsconfig.lib.json');
const tsconfigJson = readJson(appTree, '/libs/my-lib/tsconfig.json');

expect(tsconfigJson.compilerOptions.strict).toBeTruthy();
expect(
Expand All @@ -596,23 +604,6 @@ describe('lib', () => {
tsconfigJson.compilerOptions.noFallthroughCasesInSwitch
).toBeTruthy();
});

it('should default to strict false', async () => {
await libraryGenerator(appTree, {
...defaultSchema,
name: 'myLib',
});
const tsconfigJson = readJson(appTree, '/libs/my-lib/tsconfig.lib.json');

expect(tsconfigJson.compilerOptions.strict).not.toBeDefined();
expect(
tsconfigJson.compilerOptions.forceConsistentCasingInFileNames
).not.toBeDefined();
expect(tsconfigJson.compilerOptions.noImplicitReturns).not.toBeDefined();
expect(
tsconfigJson.compilerOptions.noFallthroughCasesInSwitch
).not.toBeDefined();
});
});

it.each`
Expand Down
38 changes: 20 additions & 18 deletions packages/react/src/generators/library/library.ts
Expand Up @@ -18,7 +18,6 @@ import {
reactVersion,
typesReactRouterDomVersion,
} from '../../utils/versions';
import { join } from 'path';
import { Schema } from './schema';
import {
addDependenciesToPackageJson,
Expand Down Expand Up @@ -83,7 +82,7 @@ export async function libraryGenerator(host: Tree, schema: Schema) {
createFiles(host, options);

if (!options.skipTsConfig) {
updateTsConfig(host, options);
updateBaseTsConfig(host, options);
}

if (options.unitTestRunner === 'jest') {
Expand Down Expand Up @@ -217,23 +216,27 @@ function addProject(host: Tree, options: NormalizedSchema) {
});
}

function updateLibTsConfig(tree: Tree, options: NormalizedSchema) {
updateJson(tree, join(options.projectRoot, 'tsconfig.lib.json'), (json) => {
if (options.strict) {
json.compilerOptions = {
...json.compilerOptions,
forceConsistentCasingInFileNames: true,
strict: true,
noImplicitReturns: true,
noFallthroughCasesInSwitch: true,
};
function updateTsConfig(tree: Tree, options: NormalizedSchema) {
updateJson(
tree,
joinPathFragments(options.projectRoot, 'tsconfig.json'),
(json) => {
if (options.strict) {
json.compilerOptions = {
...json.compilerOptions,
forceConsistentCasingInFileNames: true,
strict: true,
noImplicitReturns: true,
noFallthroughCasesInSwitch: true,
};
}

return json;
}

return json;
});
);
}

function updateTsConfig(host: Tree, options: NormalizedSchema) {
function updateBaseTsConfig(host: Tree, options: NormalizedSchema) {
updateJson(host, 'tsconfig.base.json', (json) => {
const c = json.compilerOptions;
c.paths = c.paths || {};
Expand Down Expand Up @@ -263,7 +266,6 @@ function createFiles(host: Tree, options: NormalizedSchema) {
{
...options,
...names(options.name),
strict: undefined,
tmpl: '',
offsetFromRoot: offsetFromRoot(options.projectRoot),
}
Expand All @@ -277,7 +279,7 @@ function createFiles(host: Tree, options: NormalizedSchema) {
toJS(host);
}

updateLibTsConfig(host, options);
updateTsConfig(host, options);
}

function updateAppRoutes(host: Tree, options: NormalizedSchema) {
Expand Down

0 comments on commit 321815d

Please sign in to comment.