-
Notifications
You must be signed in to change notification settings - Fork 63
New: Add no-var-requires #114
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Disallows the use of require statements except in import statements (no-var-requires) | ||
|
||
In other words, the use of forms such as `var foo = require("foo")` are banned. Instead use ES6 style imports or `import foo = require("foo")` imports. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
require("foo"); | ||
var foo = require("foo"); | ||
const foo = require("foo"); | ||
let foo = require("foo"); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
import foo = require("foo"); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't care about TypeScript module syntax, then you will not need this rule. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These make me giggle each time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha yeah, I agree 😂 |
||
|
||
## Compatibility | ||
|
||
* TSLint: [no-var-requires](https://palantir.github.io/tslint/rules/no-var-requires/) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* @fileoverview Disallows the use of require statements except in import statements. | ||
* @author Macklin Underdown | ||
*/ | ||
"use strict"; | ||
|
||
const util = require("../util"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
"Disallows the use of require statements except in import statements", | ||
extraDescription: [util.tslintRule("no-var-requires")], | ||
category: "TypeScript", | ||
url: | ||
"https://github.com/nzakas/eslint-plugin-typescript/blob/master/docs/rules/no-var-requires.md" | ||
}, | ||
schema: [] | ||
}, | ||
create(context) { | ||
//---------------------------------------------------------------------- | ||
// Public | ||
//---------------------------------------------------------------------- | ||
|
||
return { | ||
CallExpression(node) { | ||
if (node.callee.name === "require") { | ||
context.report({ | ||
node, | ||
message: | ||
"Require statement not part of import statement" | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @fileoverview Disallows the use of require statements except in import statements. | ||
* @author Macklin Underdown | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/no-var-requires"), | ||
RuleTester = require("eslint").RuleTester; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: "typescript-eslint-parser" | ||
}); | ||
|
||
ruleTester.run("no-var-requires", rule, { | ||
valid: ["import foo = require('foo')"], | ||
invalid: [ | ||
{ | ||
code: "var foo = require('foo')", | ||
errors: [ | ||
{ | ||
message: "Require statement not part of import statement", | ||
line: 1, | ||
column: 11 | ||
} | ||
] | ||
}, | ||
{ | ||
code: "const foo = require('foo')", | ||
errors: [ | ||
{ | ||
message: "Require statement not part of import statement", | ||
line: 1, | ||
column: 13 | ||
} | ||
] | ||
}, | ||
{ | ||
code: "let foo = require('foo')", | ||
errors: [ | ||
{ | ||
message: "Require statement not part of import statement", | ||
line: 1, | ||
column: 11 | ||
} | ||
] | ||
}, | ||
{ | ||
code: "require('foo')", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe an unassigned call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Users can replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Forgot about that, thanks for pointing that out. I’ll push up a commit next week when I get access to a computer |
||
errors: [ | ||
{ | ||
message: "Require statement not part of import statement", | ||
line: 1, | ||
column: 1 | ||
} | ||
] | ||
} | ||
] | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be allowed IMO since there isn’t an equivalent with the
import =
syntax.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I could see how introducing this as-is would be a breaking change, since all standalone
require()
calls would be flagged by this rule.I will push up a fix that allows this. If we ultimately want to disallow it before merging, I will just revert the commit. 👍