-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from crazyfactory/15-import-react-rule
feat: import-react rule
- Loading branch information
Showing
8 changed files
with
99 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import * as Lint from "tslint"; | ||
import * as ts from "typescript"; | ||
|
||
interface IOption { | ||
type: "star" | "default"; | ||
} | ||
|
||
class ImportReactRule extends Lint.AbstractWalker<IOption> { | ||
public walk(sourceFile: ts.SourceFile): void { | ||
if (sourceFile.isDeclarationFile) { | ||
return; | ||
} | ||
const cb = (node: ts.Node): void => { | ||
if ( | ||
ts.isImportDeclaration(node) | ||
&& node.moduleSpecifier.getText(sourceFile) === `"react"` | ||
&& node.importClause | ||
) { | ||
// import * as React has namedBindings, and import React has only name | ||
if (this.options.type === "star" && !node.importClause.namedBindings) { | ||
this.addFailureAtNode(node, "You must use `import * as React from 'react'`"); | ||
} else if (this.options.type === "default" && node.importClause.namedBindings) { | ||
this.addFailureAtNode(node, "You must use `import React from 'react'`"); | ||
} | ||
} | ||
return ts.forEachChild(node, cb); | ||
}; | ||
return ts.forEachChild(sourceFile, cb); | ||
} | ||
} | ||
|
||
// tslint:disable-next-line:export-name max-classes-per-file | ||
export class Rule extends Lint.Rules.AbstractRule { | ||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker( | ||
new ImportReactRule( | ||
sourceFile, | ||
"import-react", | ||
{type: this.ruleArguments[0] && this.ruleArguments[0].type ? this.ruleArguments[0].type : "star"} | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as React from "react"; | ||
import React from "react"; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ [You must use `import * as React from 'react'`] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"rulesDirectory": [ | ||
"../../../../lib" | ||
], | ||
"rules": { | ||
"import-react": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as React from "react"; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [You must use `import React from 'react'`] | ||
import React from "react"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"rulesDirectory": [ | ||
"../../../../lib" | ||
], | ||
"rules": { | ||
"import-react": [ | ||
true, | ||
{ | ||
"type": "default" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as React from "react"; | ||
import React from "react"; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ [You must use `import * as React from 'react'`] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"rulesDirectory": [ | ||
"../../../../lib" | ||
], | ||
"rules": { | ||
"import-react": [ | ||
true, | ||
{ | ||
"type": "star" | ||
} | ||
] | ||
} | ||
} |