Skip to content

Commit

Permalink
BREAKING CHANGE: prohibt-import ruleにテストを追加する (#7)
Browse files Browse the repository at this point in the history
* chore: eslint ruleのテスト環境を用意する

* test: prohibit-import ruleのテストを追加する

* fix!(BREAKING CHANGE): prohibit-import ruleのリファクタリング

* test: prohibit-import ruleのSchemaを変更するので、テストのoptionを修正する

* fix!(BREAKING CHANGE): エラーメッセージをtargetごとにカスタマイズできるようにSchemaの変更する

* test: Schemaを全体で統一するのでテストを修正する

* fix! 複数のモジュールを監視し、それぞれメッセージをカスタマイズできるようにする
  • Loading branch information
negiandleek authored Feb 8, 2022
1 parent fcc57e8 commit ddd74fc
Show file tree
Hide file tree
Showing 5 changed files with 2,628 additions and 39 deletions.
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
moduleFileExtensions: ['js'],
testMatch: ['**/test/**/*.+(js)'],
roots: ['<rootDir>/']
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"description": "A sharable ESLint plugin for SmartHR",
"main": "index.js",
"scripts": {
"test": "jest",
"release:dryrun": "standard-version --dry-run",
"release": "standard-version"
},
Expand All @@ -25,6 +26,8 @@
"json5": "^2.2.0"
},
"devDependencies": {
"eslint": "^8.8.0",
"jest": "^27.4.7",
"standard-version": "^9.3.2"
},
"peerDependencies": {
Expand Down
56 changes: 37 additions & 19 deletions rules/prohibit-import.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
const SCHEMA = [
{
type: 'object',
properties: {
targets: { type: 'object', default: {} },
const SCHEMA = [{
type: "object",
patternProperties: {
".+": {
type: "object",
required: [
"imported",
],
properties: {
imported: {
type: ["boolean", "array"],
items: {
type: "string"
}
},
reportMessage: {
type: "string"
}
},
additionalProperties: false
}
},
additionalProperties: false,
additionalProperties: true,
}
]

const defaultReportMessage = '{{prohibit}} は利用しないでください'
const defaultReportMessage = (moduleName, exportName) => `${moduleName}${typeof exportName == 'string' ? `/${exportName}`: ''} は利用しないでください`

module.exports = {
meta: {
Expand All @@ -19,31 +35,32 @@ module.exports = {
schema: SCHEMA,
},
create(context) {
const option = context.options[0]
const parsedOption = Object.entries(option.targets)

const options = context.options[0]
const targetModules = Object.keys(options)
return {
ImportDeclaration: (node) => {
parsedOption.forEach(([matchText, config]) => {
if (!node.source.value.match(new RegExp(matchText))) {
targetModules.forEach((targetModule) => {
if (!node.source.value.match(new RegExp(targetModule))) {
return
}

const imported = (() => {
if (!Array.isArray(config.imported)) {
return !!config.imported

const {imported, reportMessage} = Object.assign({imported: true}, options[targetModule])
const useImported = (() => {
if (!Array.isArray(imported)) {
return !!imported
}
const specifier = node.specifiers.find((s) => config.imported.includes(s.imported.name))

const specifier = node.specifiers.find((s) => s.imported && imported.includes(s.imported.name))

return specifier ? specifier.imported.name : false
})()

if (imported) {
if (useImported) {
context.report({
node,
messageId: 'prohibit_import',
data: {
message: (config.reportMessage || defaultReportMessage).replace('{{prohibit}}', `${node.source.value}${imported === true ? '' : `/${imported}`}`),
message: reportMessage ? reportMessage.replace('{{module}}', node.source.value).replace('{{export}}', useImported) : defaultReportMessage(node.source.value, useImported)
},
});
}
Expand All @@ -52,4 +69,5 @@ module.exports = {
}
},
}

module.exports.schema = SCHEMA
116 changes: 116 additions & 0 deletions test/prohibit-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
const rule = require("../rules/prohibit-import")
const RuleTester = require("eslint").RuleTester

const ruleTester = new RuleTester({
parserOptions: {
sourceType: "module",
ecmaVersion: 2015
},
})

ruleTester.run("prohibit-import-lodash", rule, {
valid: [
{
code: `import _ from 'lodash-es'`,
options: [
{
'^lodash$': {
imported: true,
},
}
]
},
{
code: `import { isEqual } from 'lodash-es'`,
options: [
{
'^lodash$': {
imported: ['isEqual']
},
}
]
},
{
code: `import { isEqaul } from 'lodash'`,
options: [
{
'^lodash$': {
imported: ['isEqual']
},
}
]
},
{
code: `import _ from 'lodash'`,
options: [
{
'^lodash$': {
imported: ['isEqual']
},
}
]
}
],
invalid: [
{
code: `import _ from 'lodash'`,
options: [
{
'^lodash$': {
imported: true
},
}
],
errors: [{ message: 'lodash は利用しないでください' }]
},
{
code: `import { isEqual } from 'lodash'`,
options: [
{
'^lodash$': {
imported: true
},
}
],
errors: [{ message: 'lodash は利用しないでください' }]
},
{
code: `import { isEqual } from 'lodash'`,
options: [
{
'^lodash$': {
imported: ['isEqual']
},
}
],
errors: [{message: 'lodash/isEqual は利用しないでください'}]
},
{
code: `import { isEqual } from 'lodash'`,
options: [
{
'^lodash$': {
imported: ['isEqual'],
"reportMessage": "must not use {{module}}/{{export}}"
},
}
],
errors: [{message: 'must not use lodash/isEqual'}]
},
{
code: `import { isEqual } from 'lodash'`,
options: [
{
'example': {
imported: true,
},
'^lodash$': {
imported: ['isEqual'],
reportMessage: "must not use {{module}}/{{export}}",
},
}
],
errors: [{message: 'must not use lodash/isEqual'}]
}
]
})
Loading

0 comments on commit ddd74fc

Please sign in to comment.