Skip to content

Commit

Permalink
simplify usage, better error message
Browse files Browse the repository at this point in the history
  • Loading branch information
bcherny committed Nov 15, 2016
1 parent 3b2a216 commit adaba18
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
5 changes: 1 addition & 4 deletions README.md
Expand Up @@ -18,10 +18,7 @@ Add the following to your tslint.json:

```json
{
"rulesDirectory": "node_modules/tslint-no-circular-imports",
"rules": {
"no-circular-imports": true
}
"extends": ["tslint-no-circular-imports"]
}
```

Expand Down
26 changes: 18 additions & 8 deletions noCircularImportsRule.ts
@@ -1,8 +1,9 @@
import { basename } from 'path'
import * as ts from 'typescript'
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'

static metadata: Lint.IRuleMetadata = {
ruleName: 'no-circular-imports',
Expand Down Expand Up @@ -37,7 +38,11 @@ class NoCircularImportsWalker extends Lint.RuleWalker {

// check for cycles
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(' -> ')
}`)
)
}

super.visitImportDeclaration(node)
Expand All @@ -53,12 +58,17 @@ class NoCircularImportsWalker extends Lint.RuleWalker {
imports.get(thisModuleName)!.add(importCanonicalName)
}

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[])
}

}
4 changes: 2 additions & 2 deletions package.json
@@ -1,8 +1,8 @@
{
"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": {
"build": "npm run build-sources && npm test",
"build-sources": "tsc -p ./tsconfig.json",
Expand Down
6 changes: 6 additions & 0 deletions tslint-no-circular-imports.json
@@ -0,0 +1,6 @@
{
"rulesDirectory": "./",
"rules": {
"no-circular-imports": true
}
}

0 comments on commit adaba18

Please sign in to comment.