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

feat!: add conditional TypeScript support to the ESLint config. #146

Merged
merged 6 commits into from
Apr 5, 2024
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
22 changes: 22 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
const path = require('path');
const fs = require('fs');

/**
* TypeScript configuration for ESLint:
*
* Determine whether a `tsconfig.json` file exists in the root folder of the consuming
* project. If it exists, ensure ESLint is configured for TypeScript appropriately.
*/
const typeScriptParserOptions = {};
const typeScriptExtensions = [];
const tsConfigPath = path.resolve(process.cwd(), 'tsconfig.json');
if (fs.existsSync(tsConfigPath)) {
typeScriptParserOptions.project = tsConfigPath;
typeScriptExtensions.push('airbnb-typescript');
}
/* End TypeScript configuration for ESLint */

module.exports = {
env: {
es6: true,
Expand All @@ -11,7 +29,11 @@ module.exports = {
// as peer dependencies.
'airbnb',
'airbnb/hooks',
...typeScriptExtensions,
],
parserOptions: {
...typeScriptParserOptions,
},
// If you add rule overrides here, add code to test.js that proves you formatted it right.
rules: {
'class-methods-use-this': 'off',
Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
[![Build Status](https://api.travis-ci.org/edx/eslint-config.svg?branch=master)](https://travis-ci.org/edx/eslint-config)
[![Build Status](https://github.com/openedx/eslint-config/actions/workflows/release.yml/badge.svg)](https://github.com/openedx/eslint-config/actions/workflows/release.yml/)
[![NPM Version](https://img.shields.io/npm/v/@edx/eslint-config.svg)](https://www.npmjs.com/package/@edx/eslint-config)
[![npm_downloads](https://img.shields.io/npm/dt/@edx/eslint-config.svg)](https://www.npmjs.com/package/@edx/eslint-config)
[![license](https://img.shields.io/npm/l/@edx/eslint-config.svg)](https://github.com/openedx/eslint-config/blob/master/LICENSE)
[![semantic release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

# eslint-config
# @edx/eslint-config

## Installation

If you're using @edx/frontend-build, then you'll get this package for free. Otherwise, you can install it via:

```
```shell
npx install-peerdeps --dev @edx/eslint-config
```

This will install the package and all of its peer dependencies.

Then, once it's installed, create an `.eslintrc.js` file in the top-level directory of your project with the following contents:

```
```js
module.exports = {
extends: "@edx/eslint-config",
}

```

### Using TypeScript

If `@edx/eslint-config` detects a `tsconfig.json` file in the root folder of your project, it will optionally configure ESLint to add support for TypeScript. If a `tsconfig.json` file exists, the ESLint config will:
* Extend `airbnb-typescript` ([source](https://www.npmjs.com/package/eslint-config-airbnb-typescript)).
* Configure `parserOptions.project` to point to the `tsconfig.json` file in the root folder of your project.

`airbnb-typescript` may only be extended if a `tsconfig.json` file exists in your project.

If your `tsconfig.json` file is defined somewhere other than the root folder of your project, you may override the default ESLint configuration by extending `airbnb-typescript` and including the path to your `tsconfig.json` file in the `parserOptions.project` ESLint configuration.
Loading
Loading