Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): add new rule no-require-imports#199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
JamesHenry merged 4 commits intotypescript-eslint:masterfromlukyth:lukyth/new-rule-no-require-importsFeb 7, 2019
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
1de278f
feat(eslint-plugin): add new rule no-require-imports
lukythc400d7c
Merge branch 'master' into lukyth/new-rule-no-require-imports
armano2b893dc0
Merge branch 'master' into lukyth/new-rule-no-require-imports
bradzachercfbea8e
Merge branch 'master' into lukyth/new-rule-no-require-imports
JamesHenryFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionspackages/eslint-plugin/README.md
This file contains hidden or 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
3 changes: 2 additions & 1 deletionpackages/eslint-plugin/ROADMAP.md
This file contains hidden or 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
34 changes: 34 additions & 0 deletionspackages/eslint-plugin/docs/rules/no-require-imports.md
This file contains hidden or 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,34 @@ | ||
# Disallows invocation of `require()` (no-require-imports) | ||
Prefer the newer ES6-style imports over `require()`. | ||
## Rule Details | ||
Examples of **incorrect** code for this rule: | ||
```ts | ||
var lib = require('lib'); | ||
let lib2 = require('lib2'); | ||
var lib5 = require('lib5'), | ||
lib6 = require('lib6'); | ||
import lib8 = require('lib8'); | ||
``` | ||
Examples of **correct** code for this rule: | ||
```ts | ||
import { l } from 'lib'; | ||
var lib3 = load('not_an_import'); | ||
var lib4 = lib2.subImport; | ||
var lib7 = 700; | ||
import lib9 = lib2.anotherSubImport; | ||
bradzacher marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
import lib10 from 'lib10'; | ||
``` | ||
## When Not To Use It | ||
If you don't care about TypeScript module syntax, then you will not need this rule. | ||
## Compatibility | ||
- TSLint: [no-require-imports](https://palantir.github.io/tslint/rules/no-require-imports/) |
50 changes: 50 additions & 0 deletionspackages/eslint-plugin/lib/rules/no-require-imports.js
This file contains hidden or 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,50 @@ | ||
/** | ||
* @fileoverview Disallows invocation of `require()`. | ||
* @author Kanitkorn Sujautra | ||
*/ | ||
'use strict'; | ||
const util = require('../util'); | ||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallows invocation of `require()`.', | ||
extraDescription: [util.tslintRule('no-require-imports')], | ||
category: 'TypeScript', | ||
url: util.metaDocsUrl('no-require-imports'), | ||
recommended: 'error' | ||
}, | ||
schema: [], | ||
messages: { | ||
noRequireImports: 'A `require()` style import is forbidden.' | ||
} | ||
}, | ||
create(context) { | ||
//---------------------------------------------------------------------- | ||
// Public | ||
//---------------------------------------------------------------------- | ||
return { | ||
CallExpression(node) { | ||
if (node.callee.name === 'require') { | ||
context.report({ | ||
node, | ||
messageId: 'noRequireImports' | ||
}); | ||
} | ||
}, | ||
TSExternalModuleReference(node) { | ||
context.report({ | ||
node, | ||
messageId: 'noRequireImports' | ||
}); | ||
} | ||
}; | ||
} | ||
}; |
81 changes: 81 additions & 0 deletionspackages/eslint-plugin/tests/lib/rules/no-require-imports.js
This file contains hidden or 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,81 @@ | ||
/** | ||
* @fileoverview Disallows invocation of `require()`. | ||
* @author Kanitkorn Sujautra | ||
*/ | ||
'use strict'; | ||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
const rule = require('../../../lib/rules/no-require-imports'), | ||
RuleTester = require('eslint').RuleTester; | ||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
sourceType: 'module' | ||
}, | ||
parser: '@typescript-eslint/parser' | ||
}); | ||
ruleTester.run('no-require-imports', rule, { | ||
valid: [ | ||
"import {l} from 'lib'", | ||
"var lib3 = load('not_an_import')", | ||
'var lib4 = lib2.subImport', | ||
'var lib7 = 700', | ||
'import lib9 = lib2.anotherSubImport', | ||
"import lib10 from 'lib10'" | ||
], | ||
invalid: [ | ||
{ | ||
code: "var lib = require('lib')", | ||
errors: [ | ||
{ | ||
message: 'A `require()` style import is forbidden.', | ||
line: 1, | ||
column: 11 | ||
} | ||
] | ||
}, | ||
{ | ||
code: "let lib2 = require('lib2')", | ||
errors: [ | ||
{ | ||
message: 'A `require()` style import is forbidden.', | ||
line: 1, | ||
column: 12 | ||
} | ||
] | ||
}, | ||
{ | ||
code: "var lib5 = require('lib5'), lib6 = require('lib6')", | ||
errors: [ | ||
{ | ||
message: 'A `require()` style import is forbidden.', | ||
line: 1, | ||
column: 12 | ||
}, | ||
{ | ||
message: 'A `require()` style import is forbidden.', | ||
line: 1, | ||
column: 36 | ||
} | ||
] | ||
}, | ||
{ | ||
code: "import lib8 = require('lib8')", | ||
errors: [ | ||
{ | ||
message: 'A `require()` style import is forbidden.', | ||
line: 1, | ||
column: 15 | ||
} | ||
] | ||
} | ||
] | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.