|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as eslint from 'eslint'; |
| 7 | +import * as estree from 'estree'; |
| 8 | +import { join, dirname } from 'path'; |
| 9 | + |
| 10 | +type Config = { |
| 11 | + allowed: Set<string>; |
| 12 | + disallowed: Set<string>; |
| 13 | +}; |
| 14 | + |
| 15 | +export = new class implements eslint.Rule.RuleModule { |
| 16 | + |
| 17 | + meta = { |
| 18 | + type: 'problem', |
| 19 | + schema: {}, |
| 20 | + messages: { |
| 21 | + layerbreaker: 'Bad layering. You are not allowed to access {{from}} from here, allowed layers are: [{{allowed}}]' |
| 22 | + } |
| 23 | + }; |
| 24 | + |
| 25 | + create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener { |
| 26 | + |
| 27 | + const fileDirname = dirname(context.getFilename()); |
| 28 | + const parts = fileDirname.split(/\\|\//); |
| 29 | + const ruleArgs = <Record<string, string[]>>context.options[0]; |
| 30 | + |
| 31 | + let config: Config | undefined; |
| 32 | + for (let i = parts.length - 1; i >= 0; i--) { |
| 33 | + if (ruleArgs[parts[i]]) { |
| 34 | + config = { |
| 35 | + allowed: new Set(ruleArgs[parts[i]]).add(parts[i]), |
| 36 | + disallowed: new Set() |
| 37 | + }; |
| 38 | + Object.keys(ruleArgs).forEach(key => { |
| 39 | + if (!config!.allowed.has(key)) { |
| 40 | + config!.disallowed.add(key); |
| 41 | + } |
| 42 | + }); |
| 43 | + break; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (!config) { |
| 48 | + // nothing |
| 49 | + return {}; |
| 50 | + } |
| 51 | + |
| 52 | + return { |
| 53 | + ImportDeclaration: (node: estree.Node) => { |
| 54 | + this._checkImport(context, config!, node, (<estree.ImportDeclaration>node).source.value); |
| 55 | + }, |
| 56 | + CallExpression: (node: estree.Node) => { |
| 57 | + const { callee, arguments: args } = <estree.CallExpression>node; |
| 58 | + if ((<any>callee.type) === 'Import' && args[0]?.type === 'Literal') { |
| 59 | + this._checkImport(context, config!, node, (<estree.Literal>args[0]).value); |
| 60 | + } |
| 61 | + } |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + private _checkImport(context: eslint.Rule.RuleContext, config: Config, node: estree.Node, path: any) { |
| 66 | + if (typeof path !== 'string') { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (path[0] === '.') { |
| 71 | + path = join(dirname(context.getFilename()), path); |
| 72 | + } |
| 73 | + |
| 74 | + const parts = dirname(path).split(/\\|\//); |
| 75 | + for (let i = parts.length - 1; i >= 0; i--) { |
| 76 | + const part = parts[i]; |
| 77 | + |
| 78 | + if (config!.allowed.has(part)) { |
| 79 | + // GOOD - same layer |
| 80 | + break; |
| 81 | + } |
| 82 | + |
| 83 | + if (config!.disallowed.has(part)) { |
| 84 | + // BAD - wrong layer |
| 85 | + context.report({ |
| 86 | + node, |
| 87 | + messageId: 'layerbreaker', |
| 88 | + data: { |
| 89 | + from: part, |
| 90 | + allowed: [...config!.allowed.keys()].join(', ') |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +}; |
| 99 | + |
0 commit comments