Skip to content

Added convertFileComments API #821

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

Merged
merged 3 commits into from
Nov 15, 2020
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
60 changes: 58 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ if (result.status !== ResultStatus.Succeeded) {

> See the provided `.d.ts` TypeScript typings for full descriptions of inputs and outputs.

## Standalone API
## Standalone Lint Conversion API

> ⚠ This area of code is still considered experimental.
> Use at your own risk.
> Please file an issue on GitHub if you'd like to see changes.

Portions of the individual steps within `convertTSLintConfig` are each available as exported functions as well.
Portions of the individual lint conversion steps within `convertTSLintConfig` are each available as exported functions as well.

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

const formatted = formatOutput("eslintrc.js", raw);
```

## Standalone Comment Conversion API

> ⚠ This area of code is still considered experimental.
> Use at your own risk.
> Please file an issue on GitHub if you'd like to see changes.

The individual per-file conversion logic within `convertComments` is available as a standalone function:

### `convertFileComments`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JamesHenry this is published under v2.0.0-beta4 now, if you want to try it out. Is this the kind of API you're looking for?

I'd recommend using both ruleCommentsCache (for a small perf boost) and ruleEquivalents (for rule accuracy per arguments).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh I missed this tip and just used your snippet in the description, I'll look at adding them in


Takes in a file's content and path, and returns the content with inline lint comments converted from TSLint to ESLint:

```ts
import { convertFileComments } from "tslint-to-eslint-config";

// "// eslint-disable-next-line"
const newContents = convertFileComments({
fileContent: "// tslint:disable-next-line",
filePath: "a.ts",
});
```

If using this function across multiple files, pass it a `ruleCommentsCache` so it can skip some conversion calculations for a mild performance boost:

```ts
import { convertFileComments } from "tslint-to-eslint-config";

const ruleCommentsCache = new Map<string, string[]>();

const firstNewContents = convertFileComments({
fileContent: "...",
filePath: "a.ts",
ruleCommentsCache,
});

const secondNewContents = convertFileComments({
fileContent: "...",
filePath: "b.tsx",
ruleCommentsCache,
});
```

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:

```ts
import { convertFileComments, createESLintConfiguration } from "tslint-to-eslint-config";

const { ruleEquivalents } = await createESLintConfiguration(originalConfigurations);

convertFileComments({
fileContent: "...",
filePath: "a.ts",
ruleEquivalents,
})
```
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@
"test:ci": "jest --coverage --maxWorkers=2",
"tsc": "tsc"
},
"version": "2.0.0-beta3"
"version": "2.0.0-beta4"
}
48 changes: 48 additions & 0 deletions src/api/convertFileCommentsStandalone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { parseFileComments } from "../converters/comments/parseFileComments";
import { replaceFileComments } from "../converters/comments/replaceFileComments";
import { ruleConverters } from "../converters/lintConfigs/rules/ruleConverters";

export type ConvertFileCommentsStandaloneDependencies = {
/**
* Original content of the source file.
*/
fileContent: string;

/**
* Absolute or relative path to the file.
*
* @remarks
* The file extension here is important, as parsing *.ts is different from *.tsx.
*/
filePath: string;

/**
* Optional cache of comment conversions, for performance across multiple file conversions.
*/
ruleCommentsCache?: Map<string, string[]>;

/**
* Known rule equivalents as converted by a previous configuration.
*/
ruleEquivalents?: Map<string, string[]>;
};

/**
* Replaces TSLint disable comments in source code with their ESLint equivalent.
*/
export const convertFileCommentsStandalone = ({
fileContent,
filePath,
ruleCommentsCache = new Map(),
ruleEquivalents = new Map(),
}: ConvertFileCommentsStandaloneDependencies) => {
const comments = parseFileComments(filePath, fileContent);

return replaceFileComments(
fileContent,
comments,
ruleConverters,
ruleCommentsCache,
ruleEquivalents,
);
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { convertFileCommentsStandalone as convertFileComments } from "./api/convertFileCommentsStandalone";
export { convertTSLintConfigStandalone as convertTSLintConfig } from "./api/convertTSLintConfigStandalone";
export { createESLintConfigurationStandalone as createESLintConfiguration } from "./api/createESLintConfigurationStandalone";
export { findOriginalConfigurationsStandalone as findOriginalConfigurations } from "./api/findOriginalConfigurationsStandalone";
Expand Down