tslint-no-circular-imports
Advanced tools
Comparing version 0.0.2 to 0.0.3
"use strict"; | ||
const path_1 = require('path'); | ||
const Lint = require('tslint/lib/lint'); | ||
@@ -8,3 +9,3 @@ class Rule extends Lint.Rules.AbstractRule { | ||
} | ||
Rule.FAILURE_STRING = 'circular import detected'; | ||
Rule.FAILURE_STRING = 'Circular import detected'; | ||
Rule.metadata = { | ||
@@ -33,3 +34,3 @@ ruleName: 'no-circular-imports', | ||
if (this.hasCycle(thisModuleName)) { | ||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING)); | ||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), `${Rule.FAILURE_STRING}: ${this.getCycle(thisModuleName).concat(thisModuleName).map(_ => path_1.basename(_)).join(' -> ')}`)); | ||
} | ||
@@ -47,10 +48,16 @@ super.visitImportDeclaration(node); | ||
} | ||
hasCycle(moduleName, accumulator = []) { | ||
hasCycle(moduleName) { | ||
return this.getCycle(moduleName).length > 0; | ||
} | ||
getCycle(moduleName, accumulator = []) { | ||
if (!imports.get(moduleName)) | ||
return false; | ||
return []; | ||
if (accumulator.includes(moduleName)) | ||
return true; | ||
return Array.from(imports.get(moduleName).values()).some(_ => this.hasCycle(_, accumulator.concat(moduleName))); | ||
return accumulator; | ||
return Array.from(imports.get(moduleName).values()).reduce((_prev, _) => { | ||
const c = this.getCycle(_, accumulator.concat(moduleName)); | ||
return c.length ? c : []; | ||
}, []); | ||
} | ||
} | ||
//# sourceMappingURL=noCircularImportsRule.js.map |
@@ -0,1 +1,2 @@ | ||
import { basename } from 'path' | ||
import * as ts from 'typescript' | ||
@@ -5,3 +6,3 @@ import * as Lint from 'tslint/lib/lint' | ||
export class Rule extends Lint.Rules.AbstractRule { | ||
static FAILURE_STRING = 'circular import detected' | ||
static FAILURE_STRING = 'Circular import detected' | ||
@@ -41,3 +42,7 @@ static metadata: Lint.IRuleMetadata = { | ||
if (this.hasCycle(thisModuleName)) { | ||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING)) | ||
this.addFailure( | ||
this.createFailure(node.getStart(), node.getWidth(), `${Rule.FAILURE_STRING}: ${ | ||
this.getCycle(thisModuleName).concat(thisModuleName).map(_ => basename(_)).join(' -> ') | ||
}`) | ||
) | ||
} | ||
@@ -58,10 +63,15 @@ | ||
private hasCycle(moduleName: string, accumulator: string[] = []): boolean { | ||
if (!imports.get(moduleName)) return false | ||
if (accumulator.includes(moduleName)) return true | ||
return Array.from(imports.get(moduleName)!.values()).some(_ => | ||
this.hasCycle(_, accumulator.concat(moduleName)) | ||
) | ||
private hasCycle(moduleName: string): boolean { | ||
return this.getCycle(moduleName).length > 0 | ||
} | ||
private getCycle(moduleName: string, accumulator: string[] = []): string[] { | ||
if (!imports.get(moduleName)) return [] | ||
if (accumulator.includes(moduleName)) return accumulator | ||
return Array.from(imports.get(moduleName) !.values()).reduce((_prev, _) => { | ||
const c = this.getCycle(_, accumulator.concat(moduleName)) | ||
return c.length ? c : [] | ||
}, [] as string[]) | ||
} | ||
} |
{ | ||
"name": "tslint-no-circular-imports", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "TSLint plugin to detect and warn about cicular imports", | ||
"main": "noCircularImportsRule.js", | ||
"main": "./tslint-no-circular-imports.json", | ||
"scripts": { | ||
@@ -7,0 +7,0 @@ "build": "npm run build-sources && npm test", |
@@ -21,6 +21,3 @@ # tslint-no-circular-imports [![Build Status][build]](https://circleci.com/gh/bcherny/tslint-no-circular-imports) [![npm]](https://www.npmjs.com/package/tslint-no-circular-imports) [![mit]](https://opensource.org/licenses/MIT) | ||
{ | ||
"rulesDirectory": "node_modules/tslint-no-circular-imports", | ||
"rules": { | ||
"no-circular-imports": true | ||
} | ||
"extends": ["tslint-no-circular-imports"] | ||
} | ||
@@ -27,0 +24,0 @@ ``` |
@@ -6,11 +6,6 @@ "use strict"; | ||
child_process_1.exec('../node_modules/.bin/tslint -c ./tslint.json -r ../ ./*.ts', { cwd: __dirname }, (error, stdout, stderr) => { | ||
assert.equal(stdout, `case1.ts[1, 1]: circular import detected | ||
case1.ts[2, 1]: circular import detected | ||
assert.equal(stdout, `case1.ts[1, 1]: Circular import detected: case1.ts -> case1.1.ts -> case1.ts | ||
case1.ts[2, 1]: Circular import detected: case1.ts -> case1.2.ts -> case1.ts | ||
`); | ||
// if (error) { | ||
// console.error(`exec error: ${error}`) | ||
// } | ||
// console.log(`stdout: ${stdout}`) | ||
// console.log(`stderr: ${stderr}`) | ||
}); | ||
//# sourceMappingURL=test.js.map |
@@ -7,10 +7,5 @@ import { exec } from 'child_process' | ||
exec('../node_modules/.bin/tslint -c ./tslint.json -r ../ ./*.ts', { cwd: __dirname }, (error, stdout, stderr) => { | ||
assert.equal(stdout, `case1.ts[1, 1]: circular import detected | ||
case1.ts[2, 1]: circular import detected | ||
assert.equal(stdout, `case1.ts[1, 1]: Circular import detected: case1.ts -> case1.1.ts -> case1.ts | ||
case1.ts[2, 1]: Circular import detected: case1.ts -> case1.2.ts -> case1.ts | ||
`) | ||
// if (error) { | ||
// console.error(`exec error: ${error}`) | ||
// } | ||
// console.log(`stdout: ${stdout}`) | ||
// console.log(`stderr: ${stderr}`) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1
13419
25
221
33