Repository: nzakas/eslint-plugin-typescript
Pull request #114 (2018-03-11 16:27:02)

New: Add no-var-requires
This PR adds the [`no-var-requires`](https://palantir.github.io/tslint/rules/no-var-requires/) rule as listed in the description of #5.


Patch:
diff --git a/README.md b/README.md
index ccc1723..c64b194 100644
--- a/README.md
+++ b/README.md
@@ -70,6 +70,7 @@ Then configure the rules you want to use under the rules section.
 * [`typescript/no-type-alias`](./docs/rules/no-type-alias.md) — Disallow the use of type aliases (`interface-over-type-literal` from TSLint)
 * [`typescript/no-unused-vars`](./docs/rules/no-unused-vars.md) — Prevent TypeScript-specific constructs from being erroneously flagged as unused
 * [`typescript/no-use-before-define`](./docs/rules/no-use-before-define.md) — Disallow the use of variables before they are defined
+* [`typescript/no-var-requires`](./docs/rules/no-var-requires.md) — Disallows the use of require statements except in import statements (`no-var-requires` from TSLint)
 * [`typescript/prefer-namespace-keyword`](./docs/rules/prefer-namespace-keyword.md) — Require the use of the `namespace` keyword instead of the `module` keyword to declare custom TypeScript modules. (`no-internal-module` from TSLint)
 * [`typescript/type-annotation-spacing`](./docs/rules/type-annotation-spacing.md) — Require consistent spacing around type annotations
 <!-- end rule list -->
diff --git a/docs/rules/no-var-requires.md b/docs/rules/no-var-requires.md
new file mode 100644
index 0000000..d376176
--- /dev/null
+++ b/docs/rules/no-var-requires.md
@@ -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
+var foo = require("foo");
+const foo = require("foo");
+let foo = require("foo");
+```
+
+Examples of **correct** code for this rule:
+
+```ts
+import foo = require("foo");
+require("foo");
+```
+
+## When Not To Use It
+
+If you don't care about TypeScript module syntax, then you will not need this rule.
+
+## Compatibility
+
+* TSLint: [no-var-requires](https://palantir.github.io/tslint/rules/no-var-requires/)
diff --git a/lib/rules/no-var-requires.js b/lib/rules/no-var-requires.js
new file mode 100644
index 0000000..d4e6592
--- /dev/null
+++ b/lib/rules/no-var-requires.js
@@ -0,0 +1,45 @@
+/**
+ * @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" &&
+                    node.parent.type === "VariableDeclarator"
+                ) {
+                    context.report({
+                        node,
+                        message:
+                            "Require statement not part of import statement"
+                    });
+                }
+            }
+        };
+    }
+};


Test patch:
diff --git a/tests/lib/rules/no-var-requires.js b/tests/lib/rules/no-var-requires.js
new file mode 100644
index 0000000..4715cb9
--- /dev/null
+++ b/tests/lib/rules/no-var-requires.js
@@ -0,0 +1,56 @@
+/**
+ * @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')", "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
+                }
+            ]
+        }
+    ]
+});