Repository: Redocly/redocly-cli
Pull request #1382 (2024-01-05 08:26:57)

feat: added filename extension support for more x-codeSamples languages
## What/Why/How?

The documentation for [x-codeSamples](https://redocly.com/docs/api-reference-docs/specification-extensions/x-code-samples/) mentions that `lang` names are supported based on [this list](https://github.com/github-linguist/linguist/blob/master/lib/linguist/popular.yml). However, when it comes to writing the examples in their own files, the `langToExt` utility function only supports a subset of languages.

So, for example, if I use the lang `TypeScript`, the language isn't checked for in the `langToExt` function, which results in the file being names `getundefined`, as the extension file name is undefined.

This PR adds support for all the languages mentioned by the [languages list](https://github.com/github-linguist/linguist/blob/master/lib/linguist/popular.yml), and additionally adds support for `tsx` language. As part of our project, we needed the support for it.

## Reference

## Testing

## Screenshots (optional)

## Check yourself

- [x] Code is linted
- [ ] Tested with redoc/reference-docs/workflows (internal)
- [x] All new/updated code is covered with tests

## Security

- [x] Security impact of change has been considered
- [x] Code follows company security practices and guidelines


changeset-bot[bot]: ###  🦋  Changeset detected

Latest commit: ec11e52b77d530d24be0ff4b9e3f1b64fd133cd7

**The changes in this PR will be included in the next version bump.**

<details><summary>This PR includes changesets to release 2 packages</summary>

  | Name                  | Type  |
| --------------------- | ----- |
| @redocly/cli          | Patch |
| @redocly/openapi-core | Patch |

</details>

Not sure what this means? [Click here  to learn what changesets are](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add another changeset to this PR](https://github.com/shahednasser/redocly-cli/new/feat/add-ext-names?filename=.changeset/odd-dots-explain.md&value=---%0A%22%40redocly%2Fcli%22%3A%20patch%0A---%0A%0Afeat%3A%20added%20filename%20extension%20support%20for%20more%20x-codeSamples%20languages%0A)


shahednasser: > Also please make sure to update the appropriate test case.

I've made changes to the test at `packages/cli/src/__tests__/utils.test.ts`.

Patch:
diff --git a/.changeset/cold-moles-cover.md b/.changeset/cold-moles-cover.md
new file mode 100644
index 00000000..a0f28268
--- /dev/null
+++ b/.changeset/cold-moles-cover.md
@@ -0,0 +1,5 @@
+---
+"@redocly/cli": patch
+---
+
+Added filename extension support for more `x-codeSamples` languages.
diff --git a/packages/cli/src/utils.ts b/packages/cli/src/utils.ts
index 73be04de..ef8bce1f 100644
--- a/packages/cli/src/utils.ts
+++ b/packages/cli/src/utils.ts
@@ -146,6 +146,24 @@ export function langToExt(lang: string) {
     javascript: '.js',
     js: '.js',
     python: '.py',
+    c: '.c',
+    'c++': '.cpp',
+    coffeescript: '.litcoffee',
+    dart: '.dart',
+    elixir: '.ex',
+    go: '.go',
+    groovy: '.groovy',
+    java: '.java',
+    kotlin: '.kt',
+    'objective-c': '.m',
+    perl: '.pl',
+    powershell: '.ps1',
+    ruby: '.rb',
+    rust: '.rs',
+    scala: '.sc',
+    swift: '.swift',
+    typescript: '.ts',
+    tsx: '.tsx',
   };
   return langObj[lang.toLowerCase()];
 }


Test patch:
diff --git a/packages/cli/src/__tests__/utils.test.ts b/packages/cli/src/__tests__/utils.test.ts
index 1817bb52..63d3377e 100644
--- a/packages/cli/src/__tests__/utils.test.ts
+++ b/packages/cli/src/__tests__/utils.test.ts
@@ -295,6 +295,24 @@ describe('langToExt', () => {
     ['javascript', '.js'],
     ['js', '.js'],
     ['python', '.py'],
+    ['c', '.c'],
+    ['c++', '.cpp'],
+    ['coffeescript', '.litcoffee'],
+    ['dart', '.dart'],
+    ['elixir', '.ex'],
+    ['go', '.go'],
+    ['groovy', '.groovy'],
+    ['java', '.java'],
+    ['kotlin', '.kt'],
+    ['objective-c', '.m'],
+    ['perl', '.pl'],
+    ['powershell', '.ps1'],
+    ['ruby', '.rb'],
+    ['rust', '.rs'],
+    ['scala', '.sc'],
+    ['swift', '.swift'],
+    ['typescript', '.ts'],
+    ['tsx', '.tsx'],
   ])('should infer file extension from lang - %s', (lang, expected) => {
     expect(langToExt(lang)).toBe(expected);
   });