Skip to content

Commit

Permalink
style[coral]: introduce overrides to eslint config
Browse files Browse the repository at this point in the history
Introduce Ids to import restrict patterns. With Ids it
will be easier to filter some specific pattern for a given
file override.

Refers: #155
  • Loading branch information
SmuliS committed Oct 28, 2022
1 parent 27450f9 commit 1361979
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
57 changes: 51 additions & 6 deletions coral/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
const NO_RESTRICTED_IMPORTS_RULES = ["error", {
patterns: [{
id: "DENY_PAGES_IMPORT",
group: ["src/pages*"],
message: "Pages should be only imported from '/src/router.tsx'"
}]
}]

function isObject(value) {
return typeof value === 'object' &&
!Array.isArray(value) &&
value !== null
}

const hasPatterns = (value) => isObject(value) && "patterns" in value;
const dropIdFromPatterns = (patterns) => patterns.map(({group, message}) => ({group, message}))

function filterPatternsByIds(patterns, ids) {
return patterns.filter(pattern => {
if ("id" in pattern) {
return !ids.includes(pattern.id)
}
return true
})
}

function strip_ids_from_no_restricted_imports(configuration) {
return configuration.map((rule) => {
if (hasPatterns(rule)) {
return {...rule, patterns: dropIdFromPatterns(rule.patterns)}
}
return rule
})
}

function filter_patterns_for_ids(configuration, ids) {
return configuration.map(rule => {
if (hasPatterns(rule)) {
return {...rule, patterns: dropIdFromPatterns(filterPatternsByIds(rule.patterns, ids))}
}
return rule
})
}

module.exports = {
"env": {
"browser": true,
Expand All @@ -11,6 +55,12 @@ module.exports = {
"prettier"
],
"overrides": [
{
"files": "src/router.tsx",
"rules": {
"no-restricted-imports": filter_patterns_for_ids(NO_RESTRICTED_IMPORTS_RULES, ["DENY_PAGES_IMPORT"])
}
}
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
Expand All @@ -31,11 +81,6 @@ module.exports = {
"no-relative-import-paths/no-relative-import-paths": [
"error"
],
"no-restricted-imports": ["error", {
"patterns": [{
"group": ["src/pages*"],
"message": "Pages should be only imported from '/src/router.tsx'"
}]
}]
"no-restricted-imports": strip_ids_from_no_restricted_imports(NO_RESTRICTED_IMPORTS_RULES)
}
}
2 changes: 0 additions & 2 deletions coral/src/router.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { createBrowserRouter, Navigate, RouteObject } from "react-router-dom";
/* eslint-disable no-restricted-imports */
import HomePage from "src/pages";
import HelloPage from "src/pages/hello";
/* eslint-enable */

const routes: Array<RouteObject> = [
{
Expand Down

0 comments on commit 1361979

Please sign in to comment.