Skip to content

Commit 12683a1

Browse files
author
Josh Goldberg
authored
Added convertFileComments API (#821)
* Added convertFileComments API * 2.0.0-beta4
1 parent ab01d78 commit 12683a1

File tree

5 files changed

+109
-4
lines changed

5 files changed

+109
-4
lines changed

docs/API.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ if (result.status !== ResultStatus.Succeeded) {
6464

6565
> See the provided `.d.ts` TypeScript typings for full descriptions of inputs and outputs.
6666
67-
## Standalone API
67+
## Standalone Lint Conversion API
6868

6969
> ⚠ This area of code is still considered experimental.
7070
> Use at your own risk.
7171
> Please file an issue on GitHub if you'd like to see changes.
7272
73-
Portions of the individual steps within `convertTSLintConfig` are each available as exported functions as well.
73+
Portions of the individual lint conversion steps within `convertTSLintConfig` are each available as exported functions as well.
7474

7575
* **[`findOriginalConfigurations`](#findOriginalConfigurations)** takes in an object of original configuration locations and retrieves their raw and computed contents.
7676
* **[`findReportedConfiguration`](#findReportedConfiguration)** runs a config print command and parses its output as JSON.
@@ -136,3 +136,59 @@ const raw = joinConfigConversionResults(summarizedConfiguration, originalConfigu
136136

137137
const formatted = formatOutput("eslintrc.js", raw);
138138
```
139+
140+
## Standalone Comment Conversion API
141+
142+
> ⚠ This area of code is still considered experimental.
143+
> Use at your own risk.
144+
> Please file an issue on GitHub if you'd like to see changes.
145+
146+
The individual per-file conversion logic within `convertComments` is available as a standalone function:
147+
148+
### `convertFileComments`
149+
150+
Takes in a file's content and path, and returns the content with inline lint comments converted from TSLint to ESLint:
151+
152+
```ts
153+
import { convertFileComments } from "tslint-to-eslint-config";
154+
155+
// "// eslint-disable-next-line"
156+
const newContents = convertFileComments({
157+
fileContent: "// tslint:disable-next-line",
158+
filePath: "a.ts",
159+
});
160+
```
161+
162+
If using this function across multiple files, pass it a `ruleCommentsCache` so it can skip some conversion calculations for a mild performance boost:
163+
164+
```ts
165+
import { convertFileComments } from "tslint-to-eslint-config";
166+
167+
const ruleCommentsCache = new Map<string, string[]>();
168+
169+
const firstNewContents = convertFileComments({
170+
fileContent: "...",
171+
filePath: "a.ts",
172+
ruleCommentsCache,
173+
});
174+
175+
const secondNewContents = convertFileComments({
176+
fileContent: "...",
177+
filePath: "b.tsx",
178+
ruleCommentsCache,
179+
});
180+
```
181+
182+
If running alongside a lint config conversion, pass the filled out `ruleEquivalents` map for more accurate conversions of TSLint rules whose ESLint equivalents change on different arguments:
183+
184+
```ts
185+
import { convertFileComments, createESLintConfiguration } from "tslint-to-eslint-config";
186+
187+
const { ruleEquivalents } = await createESLintConfiguration(originalConfigurations);
188+
189+
convertFileComments({
190+
fileContent: "...",
191+
filePath: "a.ts",
192+
ruleEquivalents,
193+
})
194+
```

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,5 @@
7676
"test:ci": "jest --coverage --maxWorkers=2",
7777
"tsc": "tsc"
7878
},
79-
"version": "2.0.0-beta3"
79+
"version": "2.0.0-beta4"
8080
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { parseFileComments } from "../converters/comments/parseFileComments";
2+
import { replaceFileComments } from "../converters/comments/replaceFileComments";
3+
import { ruleConverters } from "../converters/lintConfigs/rules/ruleConverters";
4+
5+
export type ConvertFileCommentsStandaloneDependencies = {
6+
/**
7+
* Original content of the source file.
8+
*/
9+
fileContent: string;
10+
11+
/**
12+
* Absolute or relative path to the file.
13+
*
14+
* @remarks
15+
* The file extension here is important, as parsing *.ts is different from *.tsx.
16+
*/
17+
filePath: string;
18+
19+
/**
20+
* Optional cache of comment conversions, for performance across multiple file conversions.
21+
*/
22+
ruleCommentsCache?: Map<string, string[]>;
23+
24+
/**
25+
* Known rule equivalents as converted by a previous configuration.
26+
*/
27+
ruleEquivalents?: Map<string, string[]>;
28+
};
29+
30+
/**
31+
* Replaces TSLint disable comments in source code with their ESLint equivalent.
32+
*/
33+
export const convertFileCommentsStandalone = ({
34+
fileContent,
35+
filePath,
36+
ruleCommentsCache = new Map(),
37+
ruleEquivalents = new Map(),
38+
}: ConvertFileCommentsStandaloneDependencies) => {
39+
const comments = parseFileComments(filePath, fileContent);
40+
41+
return replaceFileComments(
42+
fileContent,
43+
comments,
44+
ruleConverters,
45+
ruleCommentsCache,
46+
ruleEquivalents,
47+
);
48+
};

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { convertFileCommentsStandalone as convertFileComments } from "./api/convertFileCommentsStandalone";
12
export { convertTSLintConfigStandalone as convertTSLintConfig } from "./api/convertTSLintConfigStandalone";
23
export { createESLintConfigurationStandalone as createESLintConfiguration } from "./api/createESLintConfigurationStandalone";
34
export { findOriginalConfigurationsStandalone as findOriginalConfigurations } from "./api/findOriginalConfigurationsStandalone";

0 commit comments

Comments
 (0)