123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- "use strict"
- const path = require("path")
- const { Minimatch } = require("minimatch")
- function match(matcher, absolute, { filePath, name }) {
- if (absolute) {
- return filePath != null && matcher.match(filePath)
- }
- return matcher.match(name)
- }
- class Restriction {
-
- constructor({ name, message }) {
- const names = Array.isArray(name) ? name : [name]
- const matchers = names.map(raw => {
- const negate = raw[0] === "!" && raw[1] !== "("
- const pattern = negate ? raw.slice(1) : raw
- const absolute = path.isAbsolute(pattern)
- const matcher = new Minimatch(pattern, { dot: true })
- return { absolute, matcher, negate }
- })
- this.matchers = matchers
- this.message = message ? ` ${message}` : ""
- }
-
- match(importee) {
- return this.matchers.reduce(
- (ret, { absolute, matcher, negate }) =>
- negate
- ? ret && !match(matcher, absolute, importee)
- : ret || match(matcher, absolute, importee),
- false
- )
- }
- }
- function createRestriction(def) {
- if (typeof def === "string") {
- return new Restriction({ name: def })
- }
- return new Restriction(def)
- }
- function createRestrictions(defs) {
- return (defs || []).map(createRestriction)
- }
- module.exports = function checkForRestriction(context, targets) {
- const restrictions = createRestrictions(context.options[0])
- for (const target of targets) {
- const restriction = restrictions.find(r => r.match(target))
- if (restriction) {
- context.report({
- node: target.node,
- messageId: "restricted",
- data: {
- name: target.name,
- customMessage: restriction.message,
- },
- })
- }
- }
- }
|