Skip to content

Commit 6e7720c

Browse files
authored
Merge pull request #35 from siuvdlec/main
no-lib-imports preserve quotes
2 parents 69950fb + 7323537 commit 6e7720c

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/rules/no-lib-imports.ts

+17-16
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,29 @@ export default createRule({
2020
create(context) {
2121
return {
2222
ImportDeclaration(node) {
23-
const sourceValue = node.source.value?.toString();
24-
if (sourceValue) {
25-
const forbiddenImportPattern = /^fp-ts\/lib\//;
23+
const sourceValue = node.source.raw;
24+
const openQuote = /^['"]{1}/;
25+
const forbiddenImportPattern = /fp-ts\/lib\//;
2626

27+
if (
28+
sourceValue.match(openQuote.source + forbiddenImportPattern.source)
29+
) {
2730
const fixedImportSource = sourceValue.replace(
2831
forbiddenImportPattern,
2932
"fp-ts/"
3033
);
3134

32-
if (sourceValue.match(forbiddenImportPattern)) {
33-
context.report({
34-
node: node.source,
35-
messageId: "importNotAllowed",
36-
data: {
37-
detected: node.source.value,
38-
fixed: fixedImportSource,
39-
},
40-
fix(fixer) {
41-
return fixer.replaceText(node.source, `"${fixedImportSource}"`);
42-
},
43-
});
44-
}
35+
context.report({
36+
node: node.source,
37+
messageId: "importNotAllowed",
38+
data: {
39+
detected: node.source.value,
40+
fixed: fixedImportSource,
41+
},
42+
fix(fixer) {
43+
return fixer.replaceText(node.source, fixedImportSource);
44+
},
45+
});
4546
}
4647
},
4748
};

tests/rules/no-lib-imports.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ const ruleTester = new ESLintUtils.RuleTester({
1111
ruleTester.run("no-lib-imports", rule, {
1212
valid: [
1313
'import { Option } from "fp-ts/Option"',
14+
"import { Option } from 'fp-ts/Option'",
1415
'import { option } from "fp-ts"',
16+
"import { option } from 'fp-ts'",
17+
'import { option } from "library/fp-ts/lib"',
18+
"import { option } from 'library/fp-ts/lib'",
1519
],
1620
invalid: [
1721
{
@@ -23,5 +27,14 @@ ruleTester.run("no-lib-imports", rule, {
2327
],
2428
output: 'import { Option } from "fp-ts/Option"',
2529
},
30+
{
31+
code: "import { Option } from 'fp-ts/lib/Option'",
32+
errors: [
33+
{
34+
messageId: "importNotAllowed",
35+
},
36+
],
37+
output: "import { Option } from 'fp-ts/Option'",
38+
},
2639
],
2740
});

0 commit comments

Comments
 (0)