debt - add lint rule to check for layering violations, #15293

This commit is contained in:
Johannes Rieken 2016-11-10 12:40:19 +01:00
parent 5e043d348f
commit d9a83a62f1
3 changed files with 183 additions and 3 deletions

View file

@ -0,0 +1,71 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Lint = require('tslint/lib/lint');
var path_1 = require('path');
var Rule = (function (_super) {
__extends(Rule, _super);
function Rule() {
_super.apply(this, arguments);
}
Rule.prototype.apply = function (sourceFile) {
var parts = path_1.dirname(sourceFile.fileName).split(/\\|\//);
var ruleArgs = this.getOptions().ruleArguments[0];
var config;
for (var i = parts.length - 1; i >= 0; i--) {
if (ruleArgs[parts[i]]) {
config = {
allowed: new Set(ruleArgs[parts[i]]).add(parts[i]),
disallowed: new Set()
};
Object.keys(ruleArgs).forEach(function (key) {
if (!config.allowed.has(key)) {
config.disallowed.add(key);
}
});
break;
}
}
if (!config) {
return [];
}
return this.applyWithWalker(new LayeringRule(sourceFile, config, this.getOptions()));
};
return Rule;
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
var LayeringRule = (function (_super) {
__extends(LayeringRule, _super);
function LayeringRule(file, config, opts) {
_super.call(this, file, opts);
this._config = config;
}
LayeringRule.prototype.visitImportDeclaration = function (node) {
var path = node.moduleSpecifier.getText();
if (path[0] === '.') {
path = path_1.join(path_1.dirname(node.getSourceFile().fileName), path);
}
var parts = path_1.dirname(path).split(/\\|\//);
for (var i = parts.length - 1; i >= 0; i--) {
var part = parts[i];
if (this._config.allowed.has(part)) {
// GOOD - same layer
return;
}
if (this._config.disallowed.has(part)) {
// BAD - wrong layer
var message = "Bad layering. You are not allowed to access '" + part + "' from here, allowed layers are: [" + new Array(...this._config.allowed.keys()).join(', ') + "]";
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), message));
return;
}
}
};
return LayeringRule;
}(Lint.RuleWalker));

View file

@ -0,0 +1,78 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as ts from 'typescript';
import * as Lint from 'tslint/lib/lint';
import { join, dirname } from 'path';
interface Config {
allowed: Set<string>;
disallowed: Set<string>;
}
export class Rule extends Lint.Rules.AbstractRule {
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const parts = dirname(sourceFile.fileName).split(/\\|\//);
let ruleArgs = this.getOptions().ruleArguments[0];
let config: Config;
for (let i = parts.length - 1; i >= 0; i--) {
if (ruleArgs[parts[i]]) {
config = {
allowed: new Set<string>(<string[]>ruleArgs[parts[i]]).add(parts[i]),
disallowed: new Set<string>()
};
Object.keys(ruleArgs).forEach(key => {
if (!config.allowed.has(key)) {
config.disallowed.add(key);
}
});
break;
}
}
if (!config) {
return [];
}
return this.applyWithWalker(new LayeringRule(sourceFile, config, this.getOptions()));
}
}
class LayeringRule extends Lint.RuleWalker {
private _config: Config;
constructor(file: ts.SourceFile, config: Config, opts: Lint.IOptions) {
super(file, opts);
this._config = config;
}
protected visitImportDeclaration(node: ts.ImportDeclaration): void {
let path = node.moduleSpecifier.getText();
if (path[0] === '.') {
path = join(dirname(node.getSourceFile().fileName), path);
}
const parts = dirname(path).split(/\\|\//);
for (let i = parts.length - 1; i >= 0; i--) {
const part = parts[i];
if (this._config.allowed.has(part)) {
// GOOD - same layer
return;
}
if (this._config.disallowed.has(part)) {
// BAD - wrong layer
const message = `Bad layering. You are not allowed to access '${part}' from here, allowed layers are: [${new Array<string>(...<any>this._config.allowed.keys()).join(', ')}]`;
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), message));
return;
}
}
}
}

View file

@ -6,15 +6,46 @@
"no-unused-variable": true,
"curly": true,
"class-name": true,
"semicolon": ["always"],
"semicolon": [
"always"
],
"triple-equals": true,
"no-unexternalized-strings": [
true,
{
"signatures": ["localize", "nls.localize"],
"signatures": [
"localize",
"nls.localize"
],
"keyIndex": 0,
"messageIndex": 1
}
],
"layering": [
false,
{
"common": [],
"node": [
"common"
],
"browser": [
"common"
],
"workbench": [
"common",
"browser"
],
"electron-browser": [
"common",
"browser",
"workbench",
"node"
],
"electron-main": [
"common",
"node"
]
}
]
}
}
}