/
no-absolute-path.js
126 lines (119 loc) · 3.84 KB
/
no-absolute-path.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { test } from '../utils';
import { RuleTester } from 'eslint';
const ruleTester = new RuleTester();
const rule = require('rules/no-absolute-path');
const error = {
message: 'Do not import modules using an absolute path',
};
ruleTester.run('no-absolute-path', rule, {
valid: [
test({ code: 'import _ from "lodash"' }),
test({ code: 'import find from "lodash.find"' }),
test({ code: 'import foo from "./foo"' }),
test({ code: 'import foo from "../foo"' }),
test({ code: 'import foo from "foo"' }),
test({ code: 'import foo from "./"' }),
test({ code: 'import foo from "@scope/foo"' }),
test({ code: 'var _ = require("lodash")' }),
test({ code: 'var find = require("lodash.find")' }),
test({ code: 'var foo = require("./foo")' }),
test({ code: 'var foo = require("../foo")' }),
test({ code: 'var foo = require("foo")' }),
test({ code: 'var foo = require("./")' }),
test({ code: 'var foo = require("@scope/foo")' }),
test({ code: 'import events from "events"' }),
test({ code: 'import path from "path"' }),
test({ code: 'var events = require("events")' }),
test({ code: 'var path = require("path")' }),
test({ code: 'import path from "path";import events from "events"' }),
// still works if only `amd: true` is provided
test({
code: 'import path from "path"',
options: [{ amd: true }],
}),
// amd not enabled by default
test({ code: 'require(["/some/path"], function (f) { /* ... */ })' }),
test({ code: 'define(["/some/path"], function (f) { /* ... */ })' }),
test({
code: 'require(["./some/path"], function (f) { /* ... */ })',
options: [{ amd: true }],
}),
test({
code: 'define(["./some/path"], function (f) { /* ... */ })',
options: [{ amd: true }],
}),
],
invalid: [
test({
code: 'import f from "/foo"',
filename: '/foo/bar/index.js',
errors: [error],
output: 'import f from ".."',
}),
test({
code: 'import f from "/foo/bar/baz.js"',
filename: '/foo/bar/index.js',
errors: [error],
output: 'import f from "./baz.js"',
}),
test({
code: 'import f from "/foo/path"',
filename: '/foo/bar/index.js',
errors: [error],
output: 'import f from "../path"',
}),
test({
code: 'import f from "/some/path"',
filename: '/foo/bar/index.js',
errors: [error],
output: 'import f from "../../some/path"',
}),
test({
code: 'import f from "/some/path"',
filename: '/foo/bar/index.js',
options: [{ amd: true }],
errors: [error],
output: 'import f from "../../some/path"',
}),
test({
code: 'var f = require("/foo")',
filename: '/foo/bar/index.js',
errors: [error],
output: 'var f = require("..")',
}),
test({
code: 'var f = require("/foo/path")',
filename: '/foo/bar/index.js',
errors: [error],
output: 'var f = require("../path")',
}),
test({
code: 'var f = require("/some/path")',
filename: '/foo/bar/index.js',
errors: [error],
output: 'var f = require("../../some/path")',
}),
test({
code: 'var f = require("/some/path")',
filename: '/foo/bar/index.js',
options: [{ amd: true }],
errors: [error],
output: 'var f = require("../../some/path")',
}),
// validate amd
test({
code: 'require(["/some/path"], function (f) { /* ... */ })',
filename: '/foo/bar/index.js',
options: [{ amd: true }],
errors: [error],
output: 'require(["../../some/path"], function (f) { /* ... */ })',
}),
test({
code: 'define(["/some/path"], function (f) { /* ... */ })',
filename: '/foo/bar/index.js',
options: [{ amd: true }],
errors: [error],
output: 'define(["../../some/path"], function (f) { /* ... */ })',
}),
],
});