chore(feat): make suffix optional

This commit is contained in:
Wlad 2021-05-05 08:31:41 +02:00
parent 8cda565b7d
commit 7487be08e5
2 changed files with 24 additions and 4 deletions

View File

@ -27,6 +27,9 @@ module.exports = {
{
type: "object",
properties: {
regexString: {
type: "string",
},
invalidPrefix: {
type: "string",
},
@ -57,9 +60,11 @@ module.exports = {
// create a replacer regex and a target string
const replacers = context.options[0].options.map((option) => [
// match all string [../]*$invalidPrefix/anything/$invalidSuffix[/anything]
new RegExp(
`(?:(?:..\\/)*${option.invalidPrefix})+\\/([^\\/]+)\\/${option.invalidSuffix}(\\/(.+))?`
),
option.regexString ?
new RegExp(option.regexString) :
new RegExp(
`(?:(?:..\\/)*${option.invalidPrefix})+\\/([^\\/]+)${option.invalidSuffix !== "" ? `\\/${option.invalidSuffix}` : ''}(\\/(.+))?`
),
option.template ?? `${option.monorepoRoot}/$1$2`,
]);

View File

@ -40,7 +40,7 @@ ruleTester.run("no-package-imports", rule, {
invalid: [
{
code: 'import { Button } from "../../packages/components/src/Button"',
code: 'import { Button } from "../../../packages/components/src/Button"',
output: 'import { Button } from "@monorepo/components/Button"',
parserOptions: { ecmaVersion: 6, sourceType: "module" },
options,
@ -71,6 +71,21 @@ ruleTester.run("no-package-imports", rule, {
],
errors: [incorrectImport],
},
{
code: 'import Button from "../../../packages/components/Button"',
output: 'import Button from "@monorepo/components/Button"',
parserOptions: { ecmaVersion: 6, sourceType: "module" },
options: [{
options: [
{
invalidPrefix: "packages",
invalidSuffix: "",
monorepoRoot: "@monorepo",
},
],
}],
errors: [incorrectImport],
},
{
code: 'import Button from "../../../packages/components/src/Button"',
output: 'import Button from "@monorepo/components/Button"',