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

CLI: Fix Javascript language detection #23426

Merged
merged 1 commit into from
Jul 12, 2023
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
95 changes: 87 additions & 8 deletions code/lib/cli/src/detect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,28 @@ describe('Detect', () => {
it(`should return type HTML if html option is passed`, async () => {
const packageManager = {
retrievePackageJson: () => Promise.resolve({ dependencies: {}, devDependencies: {} }),
getAllDependencies: () => Promise.resolve({}),
getPackageVersion: () => Promise.resolve(null),
} as any as JsPackageManager;
} as Partial<JsPackageManager>;

await expect(detect(packageManager, { html: true })).resolves.toBe(ProjectType.HTML);
await expect(detect(packageManager as any, { html: true })).resolves.toBe(ProjectType.HTML);
});

it(`should return language javascript if the TS dependency is present but less than minimum supported`, async () => {
(logger.warn as jest.MockedFunction<typeof logger.warn>).mockClear();

const packageManager = {
retrievePackageJson: () => Promise.resolve({ dependencies: {}, devDependencies: {} }),
retrievePackageJson: () =>
Promise.resolve({
dependencies: {},
devDependencies: {
typescript: '1.0.0',
},
}),
getAllDependencies: () =>
Promise.resolve({
typescript: '1.0.0',
}),
getPackageVersion: (packageName) => {
switch (packageName) {
case 'typescript':
Expand All @@ -266,7 +277,17 @@ describe('Detect', () => {
it(`should return language typescript-3-8 if the TS dependency is >=3.8 and <4.9`, async () => {
await expect(
detectLanguage({
retrievePackageJson: () => Promise.resolve({ dependencies: {}, devDependencies: {} }),
retrievePackageJson: () =>
Promise.resolve({
dependencies: {},
devDependencies: {
typescript: '3.8.0',
},
}),
getAllDependencies: () =>
Promise.resolve({
typescript: '3.8.0',
}),
getPackageVersion: (packageName: string) => {
switch (packageName) {
case 'typescript':
Expand All @@ -280,7 +301,17 @@ describe('Detect', () => {

await expect(
detectLanguage({
retrievePackageJson: () => Promise.resolve({ dependencies: {}, devDependencies: {} }),
retrievePackageJson: () =>
Promise.resolve({
dependencies: {},
devDependencies: {
typescript: '4.8.0',
},
}),
getAllDependencies: () =>
Promise.resolve({
typescript: '4.8.0',
}),
getPackageVersion: (packageName: string) => {
switch (packageName) {
case 'typescript':
Expand All @@ -296,7 +327,17 @@ describe('Detect', () => {
it(`should return language typescript-4-9 if the dependency is >TS4.9`, async () => {
await expect(
detectLanguage({
retrievePackageJson: () => Promise.resolve({ dependencies: {}, devDependencies: {} }),
retrievePackageJson: () =>
Promise.resolve({
dependencies: {},
devDependencies: {
typescript: '4.9.1',
},
}),
getAllDependencies: () =>
Promise.resolve({
typescript: '4.9.1',
}),
getPackageVersion: (packageName: string) => {
switch (packageName) {
case 'typescript':
Expand All @@ -312,7 +353,17 @@ describe('Detect', () => {
it(`should return language typescript if the dependency is =TS4.9`, async () => {
await expect(
detectLanguage({
retrievePackageJson: () => Promise.resolve({ dependencies: {}, devDependencies: {} }),
retrievePackageJson: () =>
Promise.resolve({
dependencies: {},
devDependencies: {
typescript: '4.9.0',
},
}),
getAllDependencies: () =>
Promise.resolve({
typescript: '4.9.0',
}),
getPackageVersion: (packageName: string) => {
switch (packageName) {
case 'typescript':
Expand All @@ -328,7 +379,17 @@ describe('Detect', () => {
it(`should return language typescript if the dependency is =TS4.9beta`, async () => {
await expect(
detectLanguage({
retrievePackageJson: () => Promise.resolve({ dependencies: {}, devDependencies: {} }),
retrievePackageJson: () =>
Promise.resolve({
dependencies: {},
devDependencies: {
typescript: '4.9.0-beta',
},
}),
getAllDependencies: () =>
Promise.resolve({
typescript: '4.9.0-beta',
}),
getPackageVersion: (packageName: string) => {
switch (packageName) {
case 'typescript':
Expand All @@ -345,13 +406,31 @@ describe('Detect', () => {
await expect(
detectLanguage({
retrievePackageJson: () => Promise.resolve({ dependencies: {}, devDependencies: {} }),
getAllDependencies: () => Promise.resolve({}),
getPackageVersion: () => {
return Promise.resolve(null);
},
} as Partial<JsPackageManager> as JsPackageManager)
).resolves.toBe(SupportedLanguage.JAVASCRIPT);
});

it(`should return language Javascript even when Typescript is detected in the node_modules but not listed as a direct dependency`, async () => {
await expect(
detectLanguage({
retrievePackageJson: () => Promise.resolve({ dependencies: {}, devDependencies: {} }),
getAllDependencies: () => Promise.resolve({}),
getPackageVersion: (packageName) => {
switch (packageName) {
case 'typescript':
return Promise.resolve('4.9.0');
default:
return Promise.resolve(null);
}
},
} as Partial<JsPackageManager> as JsPackageManager)
).resolves.toBe(SupportedLanguage.JAVASCRIPT);
});

describe('detectFrameworkPreset should return', () => {
afterEach(() => {
jest.clearAllMocks();
Expand Down
33 changes: 19 additions & 14 deletions code/lib/cli/src/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ export async function detectLanguage(packageManager: JsPackageManager) {
return language;
}

const isTypescriptDirectDependency = await packageManager
.getAllDependencies()
.then((deps) => Boolean(deps['typescript']));

const typescriptVersion = await packageManager.getPackageVersion('typescript');
const prettierVersion = await packageManager.getPackageVersion('prettier');
const babelPluginTransformTypescriptVersion = await packageManager.getPackageVersion(
Expand All @@ -178,20 +182,21 @@ export async function detectLanguage(packageManager: JsPackageManager) {
'eslint-plugin-storybook'
);

if (
typescriptVersion &&
semver.gte(typescriptVersion, '4.9.0') &&
(!prettierVersion || semver.gte(prettierVersion, '2.8.0')) &&
(!babelPluginTransformTypescriptVersion ||
semver.gte(babelPluginTransformTypescriptVersion, '7.20.0')) &&
(!typescriptEslintParserVersion || semver.gte(typescriptEslintParserVersion, '5.44.0')) &&
(!eslintPluginStorybookVersion || semver.gte(eslintPluginStorybookVersion, '0.6.8'))
) {
language = SupportedLanguage.TYPESCRIPT_4_9;
} else if (typescriptVersion && semver.gte(typescriptVersion, '3.8.0')) {
language = SupportedLanguage.TYPESCRIPT_3_8;
} else if (typescriptVersion && semver.lt(typescriptVersion, '3.8.0')) {
logger.warn('Detected TypeScript < 3.8, populating with JavaScript examples');
if (isTypescriptDirectDependency && typescriptVersion) {
if (
semver.gte(typescriptVersion, '4.9.0') &&
(!prettierVersion || semver.gte(prettierVersion, '2.8.0')) &&
(!babelPluginTransformTypescriptVersion ||
semver.gte(babelPluginTransformTypescriptVersion, '7.20.0')) &&
(!typescriptEslintParserVersion || semver.gte(typescriptEslintParserVersion, '5.44.0')) &&
(!eslintPluginStorybookVersion || semver.gte(eslintPluginStorybookVersion, '0.6.8'))
) {
language = SupportedLanguage.TYPESCRIPT_4_9;
} else if (semver.gte(typescriptVersion, '3.8.0')) {
language = SupportedLanguage.TYPESCRIPT_3_8;
} else if (semver.lt(typescriptVersion, '3.8.0')) {
logger.warn('Detected TypeScript < 3.8, populating with JavaScript examples');
}
}

return language;
Expand Down