Skip to content

Commit dedec3a

Browse files
committed
feat: replace tsconfig-paths + typescript with get-tsconfig
1 parent a257df9 commit dedec3a

File tree

10 files changed

+157
-64
lines changed

10 files changed

+157
-64
lines changed

.changeset/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changesets
2+
3+
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
4+
with multi-package repos, or single-package repos to help you version and publish your code. You can
5+
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
6+
7+
We have a quick list of common questions to get you started engaging with this project in
8+
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)

.changeset/config.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "https://unpkg.com/@changesets/config/schema.json",
3+
"commit": false,
4+
"linked": [],
5+
"access": "public",
6+
"baseBranch": "fork-release",
7+
"updateInternalDependencies": "patch",
8+
"ignore": []
9+
}

.github/FUNDING.yml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
# These are supported funding model platforms
2-
3-
github: [ljharb]
4-
patreon: # Replace with a single Patreon username
5-
open_collective: eslint-plugin-import # Replace with a single Open Collective username
6-
ko_fi: # Replace with a single Ko-fi username
7-
tidelift: npm/eslint-plugin-import
8-
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9-
liberapay: # Replace with a single Liberapay username
10-
issuehunt: # Replace with a single IssueHunt username
11-
otechie: # Replace with a single Otechie username
12-
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
1+
github:
2+
- JounQin
3+
- 1stG
4+
- rx-ts
5+
- un-ts
6+
patreon: 1stG
7+
open_collective: unts
8+
custom:
9+
- https://opencollective.com/1stG
10+
- https://opencollective.com/rxts
11+
- https://afdian.net/@JounQin

.github/workflows/rebase-upstream.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Rebase
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: '0 0 * * *'
7+
8+
jobs:
9+
rebase:
10+
name: Rebase and Push
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout Repo
14+
uses: actions/checkout@v3
15+
with:
16+
fetch-depth: 0
17+
ref: fork-release
18+
19+
- name: Set Git Info
20+
run: |
21+
git config --global user.email [email protected]
22+
git config --global user.name JounQin
23+
git remote add upstream https://github.com/import-js/eslint-plugin-import.git
24+
25+
- name: Rebase and Push
26+
run: |
27+
git fetch upstream main:main
28+
git rebase upstream/main
29+
git push -f

.github/workflows/release.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches:
6+
- fork-release
7+
8+
jobs:
9+
release:
10+
name: Release
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout Repo
14+
uses: actions/checkout@v3
15+
with:
16+
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
17+
fetch-depth: 0
18+
19+
- name: Setup Node.js 16
20+
uses: actions/setup-node@v3
21+
with:
22+
node-version: 16
23+
24+
- name: Install Dependencies
25+
run: npm install
26+
27+
- name: Publish to npm
28+
uses: changesets/action@v1
29+
with:
30+
publish: npx @changesets/cli publish
31+
env:
32+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
34+
35+
- name: Sync to cnpm
36+
run: npx cnpm sync eslint-plugin-i

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
# eslint-plugin-i
2+
3+
A fork of [`eslint-plugin-import`] using [`get-tsconfig`] to replace [`tsconfig-paths`](https://github.com/dividab/tsconfig-paths) and heavy [`typescript`](https://github.com/microsoft/TypeScript) under the hood.
4+
5+
It will rebase and try to release in order to sync with the upstream every day, see [.github/workflows/rebase-upstream.yml](.github/workflows/rebase-upstream.yml) for details.
6+
7+
And also you can take https://github.com/import-js/eslint-plugin-import/pull/2447 and https://github.com/import-js/eslint-plugin-import/pull/2504 to understand why this forked project exists.
8+
9+
Issues related to `get-tsconfig` should be posted here or [`get-tsconfig`] instead, and other issues should be posted to [`eslint-plugin-import`] instead.
10+
11+
[`eslint-plugin-import`]: https://github.com/import-js/eslint-plugin-import
12+
[`get-tsconfig`]: https://github.com/privatenumber/get-tsconfig
13+
14+
## Installation
15+
16+
```bash
17+
$ npm install -D eslint-plugin-import@npm:eslint-plugin-i@latest
18+
```
19+
20+
---
21+
122
# eslint-plugin-import
223

324
[![github actions][actions-image]][actions-url]

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
2-
"name": "eslint-plugin-import",
2+
"name": "eslint-plugin-i",
33
"version": "2.28.0",
4-
"description": "Import with sanity.",
4+
"description": "A fork of `eslint-plugin-import` using `get-tsconfig` to replace `tsconfig-paths` and heavy `typescript` under the hood.",
5+
"funding": "https://opencollective.com/unts",
56
"engines": {
67
"node": ">=4"
78
},
@@ -24,7 +25,7 @@
2425
"copy-metafiles": "node --require babel-register ./scripts/copyMetafiles",
2526
"watch": "npm run tests-only -- -- --watch",
2627
"pretest": "linklocal",
27-
"posttest": "eslint . && npm run update:eslint-docs -- --check",
28+
"posttest": "eslint .",
2829
"mocha": "cross-env BABEL_ENV=test nyc mocha",
2930
"tests-only": "npm run mocha tests/src",
3031
"test": "npm run tests-only",
@@ -37,7 +38,7 @@
3738
},
3839
"repository": {
3940
"type": "git",
40-
"url": "https://github.com/import-js/eslint-plugin-import"
41+
"url": "https://github.com/un-es/eslint-plugin-i"
4142
},
4243
"keywords": [
4344
"eslint",
@@ -50,11 +51,10 @@
5051
"export"
5152
],
5253
"author": "Ben Mosher <[email protected]>",
54+
"contributors": [
55+
"JounQin (https://www.1stG.me) <[email protected]>"
56+
],
5357
"license": "MIT",
54-
"bugs": {
55-
"url": "https://github.com/import-js/eslint-plugin-import/issues"
56-
},
57-
"homepage": "https://github.com/import-js/eslint-plugin-import",
5858
"devDependencies": {
5959
"@angular-eslint/template-parser": "^13.5.0",
6060
"@eslint/import-test-order-redirect-scoped": "file:./tests/files/order-redirect-scoped",
@@ -109,6 +109,7 @@
109109
"doctrine": "^2.1.0",
110110
"eslint-import-resolver-node": "^0.3.7",
111111
"eslint-module-utils": "^2.8.0",
112+
"get-tsconfig": "^4.6.0",
112113
"has": "^1.0.3",
113114
"is-core-module": "^2.12.1",
114115
"is-glob": "^4.0.3",
@@ -117,7 +118,6 @@
117118
"object.groupby": "^1.0.0",
118119
"object.values": "^1.1.6",
119120
"resolve": "^1.22.3",
120-
"semver": "^6.3.1",
121-
"tsconfig-paths": "^3.14.2"
121+
"semver": "^6.3.1"
122122
}
123123
}

src/ExportMap.js

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { dirname } from 'path';
2+
import { resolve as pathResolve } from 'path';
33

44
import doctrine from 'doctrine';
55

@@ -15,12 +15,10 @@ import isIgnored, { hasValidExtension } from 'eslint-module-utils/ignore';
1515
import { hashObject } from 'eslint-module-utils/hash';
1616
import * as unambiguous from 'eslint-module-utils/unambiguous';
1717

18-
import { tsConfigLoader } from 'tsconfig-paths/lib/tsconfig-loader';
18+
import { getTsconfig } from 'get-tsconfig';
1919

2020
import includes from 'array-includes';
2121

22-
let ts;
23-
2422
const log = debug('eslint-plugin-import:ExportMap');
2523

2624
const exportCache = new Map();
@@ -548,41 +546,34 @@ ExportMap.parse = function (path, content, context) {
548546

549547
const source = makeSourceCode(content, ast);
550548

551-
function readTsConfig(context) {
552-
const tsconfigInfo = tsConfigLoader({
553-
cwd: context.parserOptions && context.parserOptions.tsconfigRootDir || process.cwd(),
554-
getEnv: (key) => process.env[key],
555-
});
556-
try {
557-
if (tsconfigInfo.tsConfigPath !== undefined) {
558-
// Projects not using TypeScript won't have `typescript` installed.
559-
if (!ts) { ts = require('typescript'); } // eslint-disable-line import/no-extraneous-dependencies
560-
561-
const configFile = ts.readConfigFile(tsconfigInfo.tsConfigPath, ts.sys.readFile);
562-
return ts.parseJsonConfigFileContent(
563-
configFile.config,
564-
ts.sys,
565-
dirname(tsconfigInfo.tsConfigPath),
566-
);
567-
}
568-
} catch (e) {
569-
// Catch any errors
570-
}
571-
572-
return null;
573-
}
574-
575549
function isEsModuleInterop() {
550+
const parserOptions = context.parserOptions || {};
551+
let tsconfigRootDir = parserOptions.tsconfigRootDir;
552+
const project = parserOptions.project;
576553
const cacheKey = hashObject({
577-
tsconfigRootDir: context.parserOptions && context.parserOptions.tsconfigRootDir,
554+
tsconfigRootDir,
555+
project,
578556
}).digest('hex');
579557
let tsConfig = tsconfigCache.get(cacheKey);
580558
if (typeof tsConfig === 'undefined') {
581-
tsConfig = readTsConfig(context);
559+
tsconfigRootDir = tsconfigRootDir || process.cwd();
560+
let tsconfigResult;
561+
if (project) {
562+
const projects = Array.isArray(project) ? project : [project];
563+
for (const project of projects) {
564+
tsconfigResult = getTsconfig(pathResolve(tsconfigRootDir, project));
565+
if (tsconfigResult) {
566+
break;
567+
}
568+
}
569+
} else {
570+
tsconfigResult = getTsconfig(tsconfigRootDir);
571+
}
572+
tsConfig = tsconfigResult && tsconfigResult.config || null;
582573
tsconfigCache.set(cacheKey, tsConfig);
583574
}
584575

585-
return tsConfig && tsConfig.options ? tsConfig.options.esModuleInterop : false;
576+
return tsConfig && tsConfig.compilerOptions ? tsConfig.compilerOptions.esModuleInterop : false;
586577
}
587578

588579
ast.body.forEach(function (n) {

tests/src/core/getExports.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import semver from 'semver';
33
import sinon from 'sinon';
44
import eslintPkg from 'eslint/package.json';
55
import typescriptPkg from 'typescript/package.json';
6-
import * as tsConfigLoader from 'tsconfig-paths/lib/tsconfig-loader';
6+
import * as getTsconfig from 'get-tsconfig';
77
import ExportMap from '../../../src/ExportMap';
88

99
import * as fs from 'fs';
@@ -371,11 +371,11 @@ describe('ExportMap', function () {
371371
let imports;
372372
before('load imports', function () {
373373
this.timeout(20e3); // takes a long time :shrug:
374-
sinon.spy(tsConfigLoader, 'tsConfigLoader');
374+
sinon.spy(getTsconfig, 'getTsconfig');
375375
imports = ExportMap.get('./typescript.ts', context);
376376
});
377377
after('clear spies', function () {
378-
tsConfigLoader.tsConfigLoader.restore();
378+
getTsconfig.getTsconfig.restore();
379379
});
380380

381381
it('returns something for a TypeScript file', function () {
@@ -413,11 +413,11 @@ describe('ExportMap', function () {
413413
tsconfigRootDir: null,
414414
},
415415
};
416-
expect(tsConfigLoader.tsConfigLoader.callCount).to.equal(0);
416+
expect(getTsconfig.getTsconfig.callCount).to.equal(0);
417417
ExportMap.parse('./baz.ts', 'export const baz = 5', customContext);
418-
expect(tsConfigLoader.tsConfigLoader.callCount).to.equal(1);
418+
expect(getTsconfig.getTsconfig.callCount).to.equal(1);
419419
ExportMap.parse('./baz.ts', 'export const baz = 5', customContext);
420-
expect(tsConfigLoader.tsConfigLoader.callCount).to.equal(1);
420+
expect(getTsconfig.getTsconfig.callCount).to.equal(1);
421421

422422
const differentContext = {
423423
...context,
@@ -427,7 +427,7 @@ describe('ExportMap', function () {
427427
};
428428

429429
ExportMap.parse('./baz.ts', 'export const baz = 5', differentContext);
430-
expect(tsConfigLoader.tsConfigLoader.callCount).to.equal(2);
430+
expect(getTsconfig.getTsconfig.callCount).to.equal(2);
431431
});
432432

433433
it('should cache after parsing for an ambiguous module', function () {

tests/src/rules/no-relative-packages.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ ruleTester.run('no-relative-packages', rule, {
7272
test({
7373
code: 'import bar from "../bar"',
7474
filename: testFilePath('./package-named/index.js'),
75-
errors: [{
76-
message: `Relative import from another package is not allowed. Use \`${normalize('eslint-plugin-import/tests/files/bar')}\` instead of \`../bar\``,
75+
errors: [ {
76+
message: `Relative import from another package is not allowed. Use \`${normalize('eslint-plugin-i/tests/files/bar')}\` instead of \`../bar\``,
7777
line: 1,
7878
column: 17,
79-
}],
80-
output: `import bar from "eslint-plugin-import/tests/files/bar"`,
79+
} ],
80+
output: `import bar from "eslint-plugin-i/tests/files/bar"`,
8181
}),
8282
],
8383
});

0 commit comments

Comments
 (0)