eslint-plugin-esm 0.8.1 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/common.d.ts +6 -1
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/rules/no-dynamic-imports.d.ts +2 -1
- package/dist/rules/no-dynamic-imports.d.ts.map +1 -1
- package/dist/rules/no-dynamic-imports.js +1 -1
- package/dist/rules/no-empty-exports.d.ts +2 -1
- package/dist/rules/no-empty-exports.d.ts.map +1 -1
- package/dist/rules/no-empty-exports.js +1 -1
- package/dist/rules/no-query-suffixes.d.ts +5 -0
- package/dist/rules/no-query-suffixes.d.ts.map +1 -0
- package/dist/rules/no-query-suffixes.js +10 -0
- package/dist/rules/no-side-effect-imports.d.ts.map +1 -1
- package/dist/rules/no-side-effect-imports.js +1 -1
- package/dist/rules/top-side-effect-imports.d.ts.map +1 -1
- package/dist/rules/top-side-effect-imports.js +1 -1
- package/dist/utils.d.ts +2 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +1 -1
- package/package.json +5 -3
- package/CHANGELOG.md +0 -96
- package/doc/rules/existing-file-imports.md +0 -28
- package/doc/rules/no-declaration-file-imports.md +0 -35
- package/doc/rules/no-directory-imports.md +0 -30
- package/doc/rules/no-dynamic-imports.md +0 -30
- package/doc/rules/no-empty-exports.md +0 -29
- package/doc/rules/no-git-ignored-imports.md +0 -36
- package/doc/rules/no-phantom-dep-imports.md +0 -31
- package/doc/rules/no-relative-parent-imports.md +0 -33
- package/doc/rules/no-rename-exports.md +0 -29
- package/doc/rules/no-rename-imports.md +0 -28
- package/doc/rules/no-side-effect-imports.md +0 -30
- package/doc/rules/no-useless-path-segments.md +0 -54
- package/doc/rules/required-exports.md +0 -31
- package/doc/rules/top-side-effect-imports.md +0 -31
- package/src/common.ts +0 -97
- package/src/index.ts +0 -31
- package/src/rules/existing-file-imports.test.ts +0 -28
- package/src/rules/existing-file-imports.ts +0 -25
- package/src/rules/no-declaration-file-imports.test.ts +0 -33
- package/src/rules/no-declaration-file-imports.ts +0 -12
- package/src/rules/no-directory-imports.test.ts +0 -31
- package/src/rules/no-directory-imports.ts +0 -34
- package/src/rules/no-dynamic-imports.test.ts +0 -27
- package/src/rules/no-dynamic-imports.ts +0 -15
- package/src/rules/no-empty-exports.test.ts +0 -22
- package/src/rules/no-empty-exports.ts +0 -14
- package/src/rules/no-git-ignored-imports.test.ts +0 -40
- package/src/rules/no-git-ignored-imports.ts +0 -58
- package/src/rules/no-phantom-dep-imports.test.ts +0 -35
- package/src/rules/no-phantom-dep-imports.ts +0 -124
- package/src/rules/no-relative-parent-imports.test.ts +0 -28
- package/src/rules/no-relative-parent-imports.ts +0 -13
- package/src/rules/no-rename-exports.test.ts +0 -22
- package/src/rules/no-rename-exports.ts +0 -17
- package/src/rules/no-rename-imports.test.ts +0 -22
- package/src/rules/no-rename-imports.ts +0 -16
- package/src/rules/no-side-effect-imports.test.ts +0 -24
- package/src/rules/no-side-effect-imports.ts +0 -39
- package/src/rules/no-useless-path-segments.test.ts +0 -52
- package/src/rules/no-useless-path-segments.ts +0 -31
- package/src/rules/required-exports.test.ts +0 -24
- package/src/rules/required-exports.ts +0 -22
- package/src/rules/top-side-effect-imports.test.ts +0 -24
- package/src/rules/top-side-effect-imports.ts +0 -22
- package/src/utils.ts +0 -18
- package/tsconfig.json +0 -5
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
<!-- prettier-ignore-start -->
|
|
2
|
-
# no-relative-parent-imports
|
|
3
|
-
|
|
4
|
-
Disallow importing module from a relative parent path too deeply.
|
|
5
|
-
|
|
6
|
-
## Rule Details
|
|
7
|
-
|
|
8
|
-
### Fail
|
|
9
|
-
|
|
10
|
-
```ts
|
|
11
|
-
import foo from '../../../foo'
|
|
12
|
-
import '../../../foo'
|
|
13
|
-
import('../../../foo')
|
|
14
|
-
export * from '../../../foo'
|
|
15
|
-
export {name} from '../../../foo'
|
|
16
|
-
import foo from '../../../../foo'
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
### Pass
|
|
20
|
-
|
|
21
|
-
```ts
|
|
22
|
-
import foo from 'foo'
|
|
23
|
-
import 'foo'
|
|
24
|
-
require('foo')
|
|
25
|
-
import('foo')
|
|
26
|
-
export * from 'foo'
|
|
27
|
-
export {name} from 'foo'
|
|
28
|
-
import foo from '.foo'
|
|
29
|
-
import foo from './foo'
|
|
30
|
-
import foo from '../foo'
|
|
31
|
-
import foo from '../../foo'
|
|
32
|
-
```
|
|
33
|
-
<!-- prettier-ignore-end -->
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
<!-- prettier-ignore-start -->
|
|
2
|
-
# no-rename-exports
|
|
3
|
-
|
|
4
|
-
Disallow renaming the named-exports.
|
|
5
|
-
|
|
6
|
-
## Rule Details
|
|
7
|
-
|
|
8
|
-
### Fail
|
|
9
|
-
|
|
10
|
-
```ts
|
|
11
|
-
let foo=1; export {foo as bar}
|
|
12
|
-
let foo=1; export {foo as default}
|
|
13
|
-
export {foo as bar} from './foo'
|
|
14
|
-
export {default as foo} from './foo'
|
|
15
|
-
export {type Foo as Bar}
|
|
16
|
-
export type {Foo as Bar}
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
### Pass
|
|
20
|
-
|
|
21
|
-
```ts
|
|
22
|
-
let foo=1; export {foo}
|
|
23
|
-
export let foo
|
|
24
|
-
export const foo = bar
|
|
25
|
-
export default foo
|
|
26
|
-
export default {}
|
|
27
|
-
export {}
|
|
28
|
-
```
|
|
29
|
-
<!-- prettier-ignore-end -->
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
<!-- prettier-ignore-start -->
|
|
2
|
-
# no-rename-imports
|
|
3
|
-
|
|
4
|
-
Disallow renaming the named-imports.
|
|
5
|
-
|
|
6
|
-
## Rule Details
|
|
7
|
-
|
|
8
|
-
### Fail
|
|
9
|
-
|
|
10
|
-
```ts
|
|
11
|
-
import {foo as bar} from 'foo'
|
|
12
|
-
import {default as foo} from 'foo'
|
|
13
|
-
import type {foo as bar} from 'foo'
|
|
14
|
-
import {type foo as bar} from 'foo'
|
|
15
|
-
import type {default as foo} from 'foo'
|
|
16
|
-
import {type default as foo} from 'foo'
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
### Pass
|
|
20
|
-
|
|
21
|
-
```ts
|
|
22
|
-
import Foo from 'foo'
|
|
23
|
-
import {foo, bar} from 'foo'
|
|
24
|
-
import {type foo} from 'foo'
|
|
25
|
-
import type {foo} from 'foo'
|
|
26
|
-
import type Foo from 'foo'
|
|
27
|
-
```
|
|
28
|
-
<!-- prettier-ignore-end -->
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
<!-- prettier-ignore-start -->
|
|
2
|
-
# no-side-effect-imports
|
|
3
|
-
|
|
4
|
-
Side effect import is often used for polyfills and css. It's unsafe to use it.
|
|
5
|
-
|
|
6
|
-
## Rule Details
|
|
7
|
-
|
|
8
|
-
### Fail
|
|
9
|
-
|
|
10
|
-
```ts
|
|
11
|
-
import 'foo'
|
|
12
|
-
import './foo'
|
|
13
|
-
import {} from 'foo'
|
|
14
|
-
import {} from './foo'
|
|
15
|
-
import './reflect-metadata'
|
|
16
|
-
import './foo.module.css'
|
|
17
|
-
import 'foo.module.css'
|
|
18
|
-
import 'foo.css'
|
|
19
|
-
import './foo.css'
|
|
20
|
-
import 'module.css'
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
### Pass
|
|
24
|
-
|
|
25
|
-
```ts
|
|
26
|
-
import 'reflect-metadata'
|
|
27
|
-
import {} from 'reflect-metadata'
|
|
28
|
-
import {foo} from 'foo'
|
|
29
|
-
```
|
|
30
|
-
<!-- prettier-ignore-end -->
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
<!-- prettier-ignore-start -->
|
|
2
|
-
# no-useless-path-segments
|
|
3
|
-
|
|
4
|
-
The relative source path should be a nearest relative path.
|
|
5
|
-
|
|
6
|
-
## Rule Details
|
|
7
|
-
|
|
8
|
-
### Fail
|
|
9
|
-
|
|
10
|
-
```ts
|
|
11
|
-
import xxx from ".././../a" // filename: /a/b/c/d/e.js
|
|
12
|
-
import ".././../a" // filename: /a/b/c/d/e.js
|
|
13
|
-
import(".././../a") // filename: /a/b/c/d/e.js
|
|
14
|
-
export * from ".././../a" // filename: /a/b/c/d/e.js
|
|
15
|
-
export {a} from ".././../a" // filename: /a/b/c/d/e.js
|
|
16
|
-
import xxx from "./../a" // filename: /a/b/c/d/e.js
|
|
17
|
-
import "./../a" // filename: /a/b/c/d/e.js
|
|
18
|
-
import("./../a") // filename: /a/b/c/d/e.js
|
|
19
|
-
export * from "./../a" // filename: /a/b/c/d/e.js
|
|
20
|
-
export {a} from "./../a" // filename: /a/b/c/d/e.js
|
|
21
|
-
import "././foo" // filename: /a/b/c/d/e.js
|
|
22
|
-
import "./../.././foo" // filename: /a/b/c/d/e.js
|
|
23
|
-
import("./../.././foo") // filename: /a/b/c/d/e.js
|
|
24
|
-
export * from "./../.././foo" // filename: /a/b/c/d/e.js
|
|
25
|
-
export {a} from "./../.././foo" // filename: /a/b/c/d/e.js
|
|
26
|
-
import "./../foo" // filename: /a/b/c/d/e.js
|
|
27
|
-
import("./../foo") // filename: /a/b/c/d/e.js
|
|
28
|
-
export * from "./../foo" // filename: /a/b/c/d/e.js
|
|
29
|
-
export {a} from "./../foo" // filename: /a/b/c/d/e.js
|
|
30
|
-
import foo from "./" // filename: /a/b/c/d/e.js
|
|
31
|
-
import foo from '../' // filename: /a/b/c/d/e.js
|
|
32
|
-
import foo from '../../' // filename: /a/b/c/d/e.js
|
|
33
|
-
import foo from './..' // filename: /a/b/c/d/e.js
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
### Pass
|
|
37
|
-
|
|
38
|
-
```ts
|
|
39
|
-
import xxx from "../a" // filename: /a/b/c/d/e.js
|
|
40
|
-
import "../a" // filename: /a/b/c/d/e.js
|
|
41
|
-
import("../a") // filename: /a/b/c/d/e.js
|
|
42
|
-
require("../a") // filename: /a/b/c/d/e.js
|
|
43
|
-
import xxx from "./a" // filename: /a/b/c/d/e.js
|
|
44
|
-
import xxx from "a" // filename: /a/b/c/d/e.js
|
|
45
|
-
import xxx from ".a" // filename: /a/b/c/d/e.js
|
|
46
|
-
export * from "a" // filename: /a/b/c/d/e.js
|
|
47
|
-
export * from "./a" // filename: /a/b/c/d/e.js
|
|
48
|
-
export {a} from "a" // filename: /a/b/c/d/e.js
|
|
49
|
-
export {a} from "./a" // filename: /a/b/c/d/e.js
|
|
50
|
-
import foo from "." // filename: /a/b/c/d/e.js
|
|
51
|
-
import foo from '..' // filename: /a/b/c/d/e.js
|
|
52
|
-
import foo from '../..' // filename: /a/b/c/d/e.js
|
|
53
|
-
```
|
|
54
|
-
<!-- prettier-ignore-end -->
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
<!-- prettier-ignore-start -->
|
|
2
|
-
# required-exports
|
|
3
|
-
|
|
4
|
-
It's required at least one `export` statement in a file.
|
|
5
|
-
|
|
6
|
-
## Rule Details
|
|
7
|
-
|
|
8
|
-
### Fail
|
|
9
|
-
|
|
10
|
-
```ts
|
|
11
|
-
// This rule will also report on empty files
|
|
12
|
-
|
|
13
|
-
console.log()
|
|
14
|
-
import foo from 'foo'
|
|
15
|
-
exports.foo = {}
|
|
16
|
-
module.exports = {}
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
### Pass
|
|
20
|
-
|
|
21
|
-
```ts
|
|
22
|
-
export {}
|
|
23
|
-
const foo = 'foo'; export {foo}
|
|
24
|
-
export const foo = {}
|
|
25
|
-
export default {}
|
|
26
|
-
export {foo} from 'foo'
|
|
27
|
-
export {} from 'foo'
|
|
28
|
-
export * as foo from 'foo'
|
|
29
|
-
export {}; let foo = ''
|
|
30
|
-
```
|
|
31
|
-
<!-- prettier-ignore-end -->
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
<!-- prettier-ignore-start -->
|
|
2
|
-
# top-side-effect-imports
|
|
3
|
-
|
|
4
|
-
Side effect imports must be placed before other import statements.
|
|
5
|
-
|
|
6
|
-
## Rule Details
|
|
7
|
-
|
|
8
|
-
### Fail
|
|
9
|
-
|
|
10
|
-
```ts
|
|
11
|
-
import {foo} from 'foo'; import 'reflect-metadata'
|
|
12
|
-
import {bar} from 'bar'; import 'foo.css'
|
|
13
|
-
import {foo} from 'foo'; import {} from 'bar'
|
|
14
|
-
import {foo} from 'foo'; import 'bar'; import * as foo from 'reflect-metadata'
|
|
15
|
-
import {foo} from 'foo'; import {} from 'reflect-metadata'; import * as bar from 'bar'
|
|
16
|
-
import 'bar'; import r from 'reflect-metadata'; import {} from 'foo'
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
### Pass
|
|
20
|
-
|
|
21
|
-
```ts
|
|
22
|
-
import 'reflect-metadata'; import {foo} from 'foo'
|
|
23
|
-
import 'foo.css'; import {bar} from 'bar'
|
|
24
|
-
import 'reflect-metadata'; import 'foo.css'
|
|
25
|
-
import 'foo.css'; import 'bar'
|
|
26
|
-
import 'reflect-metadata'; import {foo} from 'foo'; import {bar} from 'bar'
|
|
27
|
-
import 'foo.css'; import {foo} from 'foo'; import {bar} from 'bar'
|
|
28
|
-
import 'reflect-metadata'
|
|
29
|
-
import {foo} from 'foo'
|
|
30
|
-
```
|
|
31
|
-
<!-- prettier-ignore-end -->
|
package/src/common.ts
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import { fileURLToPath } from "node:url";
|
|
3
|
-
import type { Rule } from "eslint";
|
|
4
|
-
import type {
|
|
5
|
-
ExportAllDeclaration,
|
|
6
|
-
ExportNamedDeclaration,
|
|
7
|
-
ImportDeclaration,
|
|
8
|
-
ImportExpression,
|
|
9
|
-
} from "estree";
|
|
10
|
-
import type { JSONSchema4 } from "json-schema";
|
|
11
|
-
|
|
12
|
-
export const DEFAULT_MESSAGE_ID = "default";
|
|
13
|
-
|
|
14
|
-
export function createRule({
|
|
15
|
-
name,
|
|
16
|
-
message,
|
|
17
|
-
schema,
|
|
18
|
-
fixable,
|
|
19
|
-
type = "suggestion",
|
|
20
|
-
create: createFn,
|
|
21
|
-
}: {
|
|
22
|
-
name: string;
|
|
23
|
-
message: string;
|
|
24
|
-
schema?: JSONSchema4[];
|
|
25
|
-
fixable?: Rule.RuleMetaData["fixable"];
|
|
26
|
-
type?: Rule.RuleMetaData["type"];
|
|
27
|
-
create: (context: Rule.RuleContext) => Rule.RuleListener;
|
|
28
|
-
}): { name: string; rule: Rule.RuleModule } {
|
|
29
|
-
const rule: Rule.RuleModule = {
|
|
30
|
-
meta: {
|
|
31
|
-
...(schema && { schema }),
|
|
32
|
-
...(fixable && { fixable }),
|
|
33
|
-
messages: {
|
|
34
|
-
[DEFAULT_MESSAGE_ID]: message,
|
|
35
|
-
},
|
|
36
|
-
type,
|
|
37
|
-
docs: {
|
|
38
|
-
url: `https://github.com/zanminkian/fenge/blob/main/packages/eslint-plugin-esm/doc/rules/${name}.md`,
|
|
39
|
-
description: message,
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
create: createFn,
|
|
43
|
-
};
|
|
44
|
-
return { name, rule };
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function getRuleName(importMetaUrl: string) {
|
|
48
|
-
// remove '.js' extension
|
|
49
|
-
return path.parse(fileURLToPath(importMetaUrl)).name;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export type ImportationNode =
|
|
53
|
-
| ImportDeclaration
|
|
54
|
-
| ImportExpression
|
|
55
|
-
| ExportAllDeclaration
|
|
56
|
-
| ExportNamedDeclaration;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Create ESLint RuleListener to check string importation source.
|
|
60
|
-
* @param context ESLint RuleContext
|
|
61
|
-
* @param check the check logic
|
|
62
|
-
* @returns ESLint RuleListener
|
|
63
|
-
*/
|
|
64
|
-
export function create(
|
|
65
|
-
context: Rule.RuleContext,
|
|
66
|
-
check: (filename: string, source: string, node: ImportationNode) => boolean,
|
|
67
|
-
): Rule.RuleListener {
|
|
68
|
-
const handle = (node: ImportationNode) => {
|
|
69
|
-
if (!node.source) return;
|
|
70
|
-
if (!("value" in node.source)) return;
|
|
71
|
-
if (typeof node.source.value !== "string") return;
|
|
72
|
-
if (check(context.filename, node.source.value, node))
|
|
73
|
-
context.report({ node: node.source, messageId: DEFAULT_MESSAGE_ID });
|
|
74
|
-
};
|
|
75
|
-
return {
|
|
76
|
-
ImportDeclaration: handle,
|
|
77
|
-
ImportExpression: handle,
|
|
78
|
-
ExportAllDeclaration: handle,
|
|
79
|
-
ExportNamedDeclaration: handle,
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export function getSourceType(source: string) {
|
|
84
|
-
if (
|
|
85
|
-
source.startsWith("/") ||
|
|
86
|
-
source.startsWith("./") ||
|
|
87
|
-
source.startsWith("../") ||
|
|
88
|
-
source === "." ||
|
|
89
|
-
source === ".."
|
|
90
|
-
) {
|
|
91
|
-
return "local";
|
|
92
|
-
}
|
|
93
|
-
if (source.startsWith("node:")) {
|
|
94
|
-
return "builtin";
|
|
95
|
-
}
|
|
96
|
-
return "module";
|
|
97
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { existingFileImports } from "./rules/existing-file-imports.ts";
|
|
2
|
-
import { noDeclarationFileImports } from "./rules/no-declaration-file-imports.ts";
|
|
3
|
-
import { noDirectoryImports } from "./rules/no-directory-imports.ts";
|
|
4
|
-
import { noDynamicImports } from "./rules/no-dynamic-imports.ts";
|
|
5
|
-
import { noEmptyExports } from "./rules/no-empty-exports.ts";
|
|
6
|
-
import { noGitIgnoredImports } from "./rules/no-git-ignored-imports.ts";
|
|
7
|
-
import { noPhantomDepImports } from "./rules/no-phantom-dep-imports.ts";
|
|
8
|
-
import { noRelativeParentImports } from "./rules/no-relative-parent-imports.ts";
|
|
9
|
-
import { noRenameExports } from "./rules/no-rename-exports.ts";
|
|
10
|
-
import { noRenameImports } from "./rules/no-rename-imports.ts";
|
|
11
|
-
import { noSideEffectImports } from "./rules/no-side-effect-imports.ts";
|
|
12
|
-
import { noUselessPathSegments } from "./rules/no-useless-path-segments.ts";
|
|
13
|
-
import { requiredExports } from "./rules/required-exports.ts";
|
|
14
|
-
import { topSideEffectImports } from "./rules/top-side-effect-imports.ts";
|
|
15
|
-
|
|
16
|
-
export const rules = {
|
|
17
|
-
[existingFileImports.name]: existingFileImports.rule,
|
|
18
|
-
[noDeclarationFileImports.name]: noDeclarationFileImports.rule,
|
|
19
|
-
[noDirectoryImports.name]: noDirectoryImports.rule,
|
|
20
|
-
[noDynamicImports.name]: noDynamicImports.rule,
|
|
21
|
-
[noEmptyExports.name]: noEmptyExports.rule,
|
|
22
|
-
[noGitIgnoredImports.name]: noGitIgnoredImports.rule,
|
|
23
|
-
[noPhantomDepImports.name]: noPhantomDepImports.rule,
|
|
24
|
-
[noRelativeParentImports.name]: noRelativeParentImports.rule,
|
|
25
|
-
[noRenameExports.name]: noRenameExports.rule,
|
|
26
|
-
[noRenameImports.name]: noRenameImports.rule,
|
|
27
|
-
[noSideEffectImports.name]: noSideEffectImports.rule,
|
|
28
|
-
[noUselessPathSegments.name]: noUselessPathSegments.rule,
|
|
29
|
-
[requiredExports.name]: requiredExports.rule,
|
|
30
|
-
[topSideEffectImports.name]: topSideEffectImports.rule,
|
|
31
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { fileURLToPath } from "node:url";
|
|
2
|
-
import { test } from "@fenge/dev-utils";
|
|
3
|
-
import { existingFileImports } from "./existing-file-imports.ts";
|
|
4
|
-
|
|
5
|
-
const filename = fileURLToPath(import.meta.url);
|
|
6
|
-
|
|
7
|
-
const valid = [
|
|
8
|
-
"import foo from 'foo'",
|
|
9
|
-
|
|
10
|
-
// with ext
|
|
11
|
-
"import foo from './existing-file-imports.test.ts'",
|
|
12
|
-
].map((code) => ({ code, filename }));
|
|
13
|
-
|
|
14
|
-
const invalid = [
|
|
15
|
-
"import foo from './existing-file-imports.test.js'",
|
|
16
|
-
// without ext
|
|
17
|
-
"import foo from './existing-file-imports.test'",
|
|
18
|
-
"import foo from './inexistent-file'",
|
|
19
|
-
"import foo from '../inexistent-file'",
|
|
20
|
-
// directory
|
|
21
|
-
"import foo from '../rules'",
|
|
22
|
-
"import foo from '.'",
|
|
23
|
-
"import foo from './'",
|
|
24
|
-
"import foo from '..'",
|
|
25
|
-
"import foo from '../'",
|
|
26
|
-
].map((code) => ({ code, filename }));
|
|
27
|
-
|
|
28
|
-
test({ valid, invalid, ...existingFileImports });
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { create, createRule, getRuleName, getSourceType } from "../common.ts";
|
|
4
|
-
import { memoize } from "../utils.ts";
|
|
5
|
-
|
|
6
|
-
export const existingFileImports = createRule({
|
|
7
|
-
name: getRuleName(import.meta.url),
|
|
8
|
-
message: "Only allow importing from an existing local file.",
|
|
9
|
-
create: (context) => create(context, check),
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
function check(filename: string, source: string) {
|
|
13
|
-
return getSourceType(source) === "local"
|
|
14
|
-
? !isExisting(path.resolve(path.dirname(filename), source))
|
|
15
|
-
: false;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const isExisting = memoize((filepath: string): boolean => {
|
|
19
|
-
try {
|
|
20
|
-
const stat = fs.statSync(filepath);
|
|
21
|
-
return stat.isFile();
|
|
22
|
-
} catch {
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
});
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { test } from "@fenge/dev-utils";
|
|
2
|
-
import { noDeclarationFileImports } from "./no-declaration-file-imports.ts";
|
|
3
|
-
|
|
4
|
-
const invalid = [
|
|
5
|
-
"import foo from 'foo.d.bar'",
|
|
6
|
-
"import foo from './foo.d.bar'",
|
|
7
|
-
"import foo from './foo/foo.d.bar'",
|
|
8
|
-
|
|
9
|
-
"import foo from './foo.d.ts'",
|
|
10
|
-
"import foo from './foo.d.cts'",
|
|
11
|
-
"import foo from './foo.d.mts'",
|
|
12
|
-
"import foo from './foo.d.tsx'",
|
|
13
|
-
|
|
14
|
-
"import foo from './foo.d.js'",
|
|
15
|
-
"import foo from './foo.d.cjs'",
|
|
16
|
-
"import foo from './foo.d.mjs'",
|
|
17
|
-
"import foo from './foo.d.jsx'",
|
|
18
|
-
|
|
19
|
-
"import foo from '/foo.d.js'",
|
|
20
|
-
];
|
|
21
|
-
|
|
22
|
-
const valid = [
|
|
23
|
-
"import foo from 'foo'",
|
|
24
|
-
|
|
25
|
-
"import foo from './foo.ts'",
|
|
26
|
-
"import foo from './foo.cts'",
|
|
27
|
-
"import foo from './foo.mts'",
|
|
28
|
-
"import foo from './foo.tsx'",
|
|
29
|
-
|
|
30
|
-
"import foo from '/foo.ts'",
|
|
31
|
-
];
|
|
32
|
-
|
|
33
|
-
test({ valid, invalid, ...noDeclarationFileImports });
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { create, createRule, getRuleName } from "../common.ts";
|
|
2
|
-
|
|
3
|
-
export const noDeclarationFileImports = createRule({
|
|
4
|
-
name: getRuleName(import.meta.url),
|
|
5
|
-
message: "Disallow importing from a declaration style file.",
|
|
6
|
-
create: (context) => create(context, check),
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
function check(_filename: string, source: string) {
|
|
10
|
-
const file = source.split("/").at(-1);
|
|
11
|
-
return !file || file.includes(".d.");
|
|
12
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import process from "node:process";
|
|
2
|
-
import { fileURLToPath } from "node:url";
|
|
3
|
-
import { test } from "@fenge/dev-utils";
|
|
4
|
-
import { noDirectoryImports } from "./no-directory-imports.ts";
|
|
5
|
-
|
|
6
|
-
const valid = [
|
|
7
|
-
"import foo from 'foo'",
|
|
8
|
-
"import foo from './foo'",
|
|
9
|
-
`import foo from '${process.cwd()}/index.js'`,
|
|
10
|
-
`import foo from '${process.cwd()}/index.ts'`,
|
|
11
|
-
`import foo from '${process.cwd()}/package.json'`,
|
|
12
|
-
].map((code) => ({
|
|
13
|
-
code,
|
|
14
|
-
filename: fileURLToPath(import.meta.url),
|
|
15
|
-
}));
|
|
16
|
-
|
|
17
|
-
const invalid = [
|
|
18
|
-
"import foo from '.'",
|
|
19
|
-
"import foo from './'",
|
|
20
|
-
"import foo from '..'",
|
|
21
|
-
"import foo from '../'",
|
|
22
|
-
"import foo from '../rules'",
|
|
23
|
-
"import foo from '../rules/'",
|
|
24
|
-
"import foo from '../../src'",
|
|
25
|
-
`import foo from '${process.cwd()}'`,
|
|
26
|
-
].map((code) => ({
|
|
27
|
-
code,
|
|
28
|
-
filename: fileURLToPath(import.meta.url),
|
|
29
|
-
}));
|
|
30
|
-
|
|
31
|
-
test({ valid, invalid, ...noDirectoryImports });
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { create, createRule, getRuleName, getSourceType } from "../common.ts";
|
|
4
|
-
import { memoize } from "../utils.ts";
|
|
5
|
-
|
|
6
|
-
export const noDirectoryImports = createRule({
|
|
7
|
-
name: getRuleName(import.meta.url),
|
|
8
|
-
message: "Disallow importing from a directory.",
|
|
9
|
-
create: (context) => create(context, check),
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
function check(filename: string, source: string) {
|
|
13
|
-
if (getSourceType(source) !== "local") {
|
|
14
|
-
return false;
|
|
15
|
-
}
|
|
16
|
-
if (source.endsWith(".") || source.endsWith("./")) {
|
|
17
|
-
return true;
|
|
18
|
-
}
|
|
19
|
-
const absolutePath = path.resolve(path.dirname(filename), source);
|
|
20
|
-
if (!absolutePath.startsWith("/")) {
|
|
21
|
-
throw new Error(
|
|
22
|
-
`ESLint plugin internal error. Absolute path incorrect: ${absolutePath}.`,
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
return isDir(absolutePath);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const isDir = memoize((filePath: string) => {
|
|
29
|
-
try {
|
|
30
|
-
return fs.statSync(filePath).isDirectory();
|
|
31
|
-
} catch {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
});
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { test } from "@fenge/dev-utils";
|
|
2
|
-
import { noDynamicImports } from "./no-dynamic-imports.ts";
|
|
3
|
-
|
|
4
|
-
const valid = [
|
|
5
|
-
"import('foo')",
|
|
6
|
-
'import("foo")',
|
|
7
|
-
'import("./foo")',
|
|
8
|
-
'await import("foo")',
|
|
9
|
-
'const foo = await import("foo")',
|
|
10
|
-
];
|
|
11
|
-
|
|
12
|
-
const invalid = [
|
|
13
|
-
// 'import()',
|
|
14
|
-
// 'await import()',
|
|
15
|
-
"import(false)",
|
|
16
|
-
"import(123)",
|
|
17
|
-
"await import(123)",
|
|
18
|
-
"import(`foo`)",
|
|
19
|
-
"import(foo)",
|
|
20
|
-
"import({})",
|
|
21
|
-
"import([])",
|
|
22
|
-
"const foo = await import(foo)",
|
|
23
|
-
// "import(foo, {})", // only ts support it, not ecmascript
|
|
24
|
-
// 'import("foo", {})', // only ts support it, not ecmascript
|
|
25
|
-
];
|
|
26
|
-
|
|
27
|
-
test({ valid, invalid, ...noDynamicImports });
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { Node } from "estree";
|
|
2
|
-
import { createRule, DEFAULT_MESSAGE_ID, getRuleName } from "../common.ts";
|
|
3
|
-
|
|
4
|
-
export const noDynamicImports = createRule({
|
|
5
|
-
name: getRuleName(import.meta.url),
|
|
6
|
-
message: "`import()` should be called with string literal.",
|
|
7
|
-
create: (context) => ({
|
|
8
|
-
"ImportExpression > :not(Literal)": (node: Node) => {
|
|
9
|
-
context.report({ node, messageId: DEFAULT_MESSAGE_ID });
|
|
10
|
-
},
|
|
11
|
-
"ImportExpression > Literal[raw=/^[^'\"].*[^'\"]$/]": (node: Node) => {
|
|
12
|
-
context.report({ node, messageId: DEFAULT_MESSAGE_ID });
|
|
13
|
-
},
|
|
14
|
-
}),
|
|
15
|
-
});
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { test } from "@fenge/dev-utils";
|
|
2
|
-
import { noEmptyExports } from "./no-empty-exports.ts";
|
|
3
|
-
|
|
4
|
-
const valid = [
|
|
5
|
-
"var name = 123; export {name as age};",
|
|
6
|
-
"const name = {}; export {name};",
|
|
7
|
-
"export const name = {};",
|
|
8
|
-
"export default {};",
|
|
9
|
-
"var foo = 213; export {foo as default};",
|
|
10
|
-
"export {default} from 'foo';",
|
|
11
|
-
"export * as foo from 'foo';",
|
|
12
|
-
];
|
|
13
|
-
|
|
14
|
-
const invalid = [
|
|
15
|
-
"export {};",
|
|
16
|
-
"console.log(123); export {};",
|
|
17
|
-
"export default {}; export {};",
|
|
18
|
-
"export {} from 'foo';",
|
|
19
|
-
"export {} from './foo';",
|
|
20
|
-
];
|
|
21
|
-
|
|
22
|
-
test({ valid, invalid, ...noEmptyExports });
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { Node } from "estree";
|
|
2
|
-
import { createRule, DEFAULT_MESSAGE_ID, getRuleName } from "../common.ts";
|
|
3
|
-
|
|
4
|
-
export const noEmptyExports = createRule({
|
|
5
|
-
name: getRuleName(import.meta.url),
|
|
6
|
-
message: "Disallow `export {}`.",
|
|
7
|
-
create: (context) => ({
|
|
8
|
-
"ExportNamedDeclaration[specifiers.length=0][declaration=null]": (
|
|
9
|
-
node: Node,
|
|
10
|
-
) => {
|
|
11
|
-
context.report({ node, messageId: DEFAULT_MESSAGE_ID });
|
|
12
|
-
},
|
|
13
|
-
}),
|
|
14
|
-
});
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import process from "node:process";
|
|
2
|
-
import { fileURLToPath } from "node:url";
|
|
3
|
-
import { test } from "@fenge/dev-utils";
|
|
4
|
-
import { noGitIgnoredImports } from "./no-git-ignored-imports.ts";
|
|
5
|
-
|
|
6
|
-
const valid = [
|
|
7
|
-
"import foo from 'foo'",
|
|
8
|
-
"import 'foo'",
|
|
9
|
-
"require('foo')",
|
|
10
|
-
"import('foo')",
|
|
11
|
-
"export * from 'foo'",
|
|
12
|
-
"export {name} from 'foo'",
|
|
13
|
-
|
|
14
|
-
"import foo from '.foo'",
|
|
15
|
-
"import foo from '../../../../for-test'",
|
|
16
|
-
].map((code) => ({
|
|
17
|
-
code,
|
|
18
|
-
filename: fileURLToPath(import.meta.url),
|
|
19
|
-
}));
|
|
20
|
-
|
|
21
|
-
const invalid = [
|
|
22
|
-
"import foo from './dist/foo'",
|
|
23
|
-
"import './dist/foo'",
|
|
24
|
-
"import('./dist/foo')",
|
|
25
|
-
"export * from './dist/foo'",
|
|
26
|
-
"export {name} from './dist/foo'",
|
|
27
|
-
|
|
28
|
-
"import foo from '../dist/index.js'",
|
|
29
|
-
"import foo from '../../node_modules/foo/bar.js'",
|
|
30
|
-
|
|
31
|
-
"import foo from '/foo/tmp'",
|
|
32
|
-
`import foo from '${process.cwd()}/tmp'`,
|
|
33
|
-
"import foo from '../../test/for-test'",
|
|
34
|
-
"import foo from '../../../../../for-test'",
|
|
35
|
-
].map((code) => ({
|
|
36
|
-
code,
|
|
37
|
-
filename: fileURLToPath(import.meta.url),
|
|
38
|
-
}));
|
|
39
|
-
|
|
40
|
-
test({ valid, invalid, ...noGitIgnoredImports });
|