Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/tests/src/rules/no-absolute-path.js
829 views
1
import { test } from '../utils';
2
3
import { RuleTester } from 'eslint';
4
5
const ruleTester = new RuleTester();
6
const rule = require('rules/no-absolute-path');
7
8
const error = {
9
message: 'Do not import modules using an absolute path',
10
};
11
12
ruleTester.run('no-absolute-path', rule, {
13
valid: [
14
test({ code: 'import _ from "lodash"' }),
15
test({ code: 'import find from "lodash.find"' }),
16
test({ code: 'import foo from "./foo"' }),
17
test({ code: 'import foo from "../foo"' }),
18
test({ code: 'import foo from "foo"' }),
19
test({ code: 'import foo from "./"' }),
20
test({ code: 'import foo from "@scope/foo"' }),
21
test({ code: 'var _ = require("lodash")' }),
22
test({ code: 'var find = require("lodash.find")' }),
23
test({ code: 'var foo = require("./foo")' }),
24
test({ code: 'var foo = require("../foo")' }),
25
test({ code: 'var foo = require("foo")' }),
26
test({ code: 'var foo = require("./")' }),
27
test({ code: 'var foo = require("@scope/foo")' }),
28
29
test({ code: 'import events from "events"' }),
30
test({ code: 'import path from "path"' }),
31
test({ code: 'var events = require("events")' }),
32
test({ code: 'var path = require("path")' }),
33
test({ code: 'import path from "path";import events from "events"' }),
34
35
// still works if only `amd: true` is provided
36
test({
37
code: 'import path from "path"',
38
options: [{ amd: true }],
39
}),
40
41
// amd not enabled by default
42
test({ code: 'require(["/some/path"], function (f) { /* ... */ })' }),
43
test({ code: 'define(["/some/path"], function (f) { /* ... */ })' }),
44
test({
45
code: 'require(["./some/path"], function (f) { /* ... */ })',
46
options: [{ amd: true }],
47
}),
48
test({
49
code: 'define(["./some/path"], function (f) { /* ... */ })',
50
options: [{ amd: true }],
51
}),
52
],
53
invalid: [
54
test({
55
code: 'import f from "/foo"',
56
errors: [error],
57
}),
58
test({
59
code: 'import f from "/foo/path"',
60
errors: [error],
61
}),
62
test({
63
code: 'import f from "/some/path"',
64
errors: [error],
65
}),
66
test({
67
code: 'import f from "/some/path"',
68
options: [{ amd: true }],
69
errors: [error],
70
}),
71
test({
72
code: 'var f = require("/foo")',
73
errors: [error],
74
}),
75
test({
76
code: 'var f = require("/foo/path")',
77
errors: [error],
78
}),
79
test({
80
code: 'var f = require("/some/path")',
81
errors: [error],
82
}),
83
test({
84
code: 'var f = require("/some/path")',
85
options: [{ amd: true }],
86
errors: [error],
87
}),
88
// validate amd
89
test({
90
code: 'require(["/some/path"], function (f) { /* ... */ })',
91
options: [{ amd: true }],
92
errors: [error],
93
}),
94
test({
95
code: 'define(["/some/path"], function (f) { /* ... */ })',
96
options: [{ amd: true }],
97
errors: [error],
98
}),
99
],
100
});
101
102