Skip to content

Added Node API #732

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 9 commits into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ _Default: `tsconfig.json`_
Path to a TypeScript configuration file to read TypeScript compiler options from.
This will help inform the generated ESLint configuration file's [env](https://eslint.org/docs/user-guide/configuring#specifying-parser-options) settings.


## Node API

You can use `tslint-to-eslint-config` programmatically via its exported functions.
See [docs/API](./docs/API.md) for details.

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

const result = await convertLintConfig();
```

## Development

See the [Code of Conduct](./.github/CODE_OF_CONDUCT.md) and [general development docs](./docs/Development.md). 💖
65 changes: 65 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# API

You can use `tslint-to-eslint-config` programmatically in your Node apps.
It provides a **[`convertTSLintConfig`](#convertTSLintConfig)** function to find relevant configurations on disk and output the generated ESLint configuration.

## `convertTSLintConfig`

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

const result = await convertTSLintConfig();
```

Finds relevant configurations on disk and outputs the generated ESLint configuration.

Optionally takes in the same settings you can provide via the CLI:

* `config`: Output ESLint configuration file path _(default: `.eslintrc.js`)_.
* `eslint`: Original ESLint configuration file path _(default: `.eslintrc.js`)_.
* `package`: Original packages configuration file path _(default: `package.json`)_.
* `prettier`: Whether to add `eslint-config-prettier` to the plugins list.
* `tslint`: Original TSLint configuration file path _(default: `tslint.json`)_.
* `typescript`: Original TypeScript configuration file path _(default: `tsconfig.json`)_.

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

const result = await convertTSLintConfig({
config: "./path/to/output/eslintrc.js",
eslint: "./path/to/input/eslintrc.js",
package: "./path/to/package.json",
prettier: true, // Prettier: highly recommended!
tslint: "./path/to/tslint.json",
typescript: "./path/to/tsconfig.json",
});
```

If the TSLint configuration or any manually specified configurations fail to read from disk, the result will contain:

* `complaints`: String complaints describing the errors.
* `status`: `ResultStatus.ConfigurationError` (`2`).

If no error is detected, the result will contain:

* `data`: Resultant ESLint configuration as:
* `formatted`: Stringified result per the output config path's file type.
* `raw`: Plain old JavaScript object.
* `status`: `ResultStatus.Succeeded` (`0`).

```ts
import { convertTSLintConfig, ResultStatus } from "tslint-to-eslint-config";

const result = await convertTSLintConfig({ /* ... */ });

if (result.status !== ResultStatus.Succeeded) {
console.info("Oh no!");
console.error(result.complaints.join("\n"));
} else {
console.info("Hooray!");
console.log(result.data.formatted);
console.log(result.data.raw);
}
```

> See the provided `.d.ts` TypeScript typings for full descriptions of inputs and outputs.
11 changes: 7 additions & 4 deletions docs/Architecture/Linters.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
TSLint-to-ESLint linter configuration conversion is the first root-level converter run.
Within `src/converters/lintConfigs/convertLintConfig.ts`, the following steps occur:

1. Raw TSLint rules are mapped to their ESLint equivalents.
2. Those ESLint equivalents are deduplicated and relevant preset(s) detected.
3. Those deduplicated rules and metadata are written to the output configuration file.
4. A summary of conversion results is printed, along with any now-missing packages.
1. Deduplicated ESLint rules and metadata are generated from raw TSLint rules.
1a. Raw TSLint rules are mapped to their ESLint equivalents.
1b. Those ESLint equivalents are deduplicated and relevant preset(s) detected.
2. Those deduplicated rules and metadata are written to the output configuration file.
3. A summary of conversion results is printed, along with any now-missing packages.

> Stepss 1 and 2 are the logic exported by the [Node API](../API.md) as [`convertTSLintConfig`](../API.md#convertTSLintConfig).

## Rule Conversion

Expand Down
2 changes: 1 addition & 1 deletion docs/Dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Its dependencies object is manually created in `src/cli/main.ts` and bound to th
## When to Use Dependencies

Most functions don't need a `dependencies` object.
Only add one if something should be stubbed out during tests.
Only add one if something should be stubbed out during tests _or_ should be available to multiple callers.

## How to Use Dependencies

Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
"!./src/**/*.d.ts",
"!./src/**/*.stubs.ts",
"!./src/adapters/*.ts",
"!./src/api/*.ts",
"!./src/cli/main.ts",
"!./src/converters/editorConfigs/editorSettingsConverters.ts",
"!./src/converters/lintConfigs/rules/ruleConverters.ts",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"prettier --write"
]
},
"main": "./src/index.js",
"name": "tslint-to-eslint-config",
"repository": {
"type": "git",
Expand Down
73 changes: 73 additions & 0 deletions src/api/convertTSLintConfigStandalone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { createESLintConfiguration } from "../converters/lintConfigs/createESLintConfiguration";
import { formatOutput } from "../converters/lintConfigs/formatting/formatOutput";
import {
joinConfigConversionResults,
JoinedConversionResult,
} from "../converters/lintConfigs/joinConfigConversionResults";
import { findOriginalConfigurations } from "../input/findOriginalConfigurations";
import {
ConfigurationErrorResult,
LintConfigConversionSettings,
ResultStatus,
SucceededDataResult,
} from "../types";
import {
createESLintConfigurationDependencies,
findOriginalConfigurationsDependencies,
} from "./dependencies";

/**
* Resultant configuration data from converting a TSLint configuration.
*/
export type TSLintConversionData = {
/**
* Formatted configuration string per the output file's extension.
*/
formatted: string;

/**
* Object description of the resultant configuration data.
*/
raw: JoinedConversionResult;
};

/**
* Finds relevant configurations on disk and outputs the generated ESLint configuration.
*
* @param settings - Settings to find and convert configurations to an ESLint configuration.
*/
export const convertTSLintConfigStandalone = async (
rawSettings: Partial<LintConfigConversionSettings> = {},
): Promise<ConfigurationErrorResult | SucceededDataResult<TSLintConversionData>> => {
const settings = {
...rawSettings,
config: ".eslintrc.js",
};
const originalConfigurations = await findOriginalConfigurations(
findOriginalConfigurationsDependencies,
settings,
);
if (originalConfigurations.status !== ResultStatus.Succeeded) {
return originalConfigurations;
}

const summarizedConfiguration = await createESLintConfiguration(
createESLintConfigurationDependencies,
originalConfigurations.data,
settings.prettier,
new Map(),
);

const output = joinConfigConversionResults(
summarizedConfiguration,
originalConfigurations.data,
);

return {
data: {
formatted: formatOutput(settings.config, output),
raw: output,
},
status: ResultStatus.Succeeded,
};
};
Loading