Skip to content

Commit cb62ee8

Browse files
committed
feat(:sparkles:): add excludePaths option
1 parent 6fead1d commit cb62ee8

File tree

13 files changed

+263
-7
lines changed

13 files changed

+263
-7
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ This plugin requires your project to use TypeScript (>=4.1.3).
2828
yarn add eslint-plugin-ts-ban-snippets --dev
2929
```
3030

31-
## Example Configuration
31+
## Example Configurations
3232

3333
The plugin relies on TypeScript compiler services to resolve types.
3434
You need to set your `tsconfig.json` file in your eslint configuration via `parserOptions`.
3535

36+
### Simple example
37+
3638
```json
3739
{
3840
"plugins": ["ts-ban-snippets"],
@@ -55,6 +57,31 @@ You need to set your `tsconfig.json` file in your eslint configuration via `pars
5557
}
5658
```
5759

60+
### Example with excluded paths
61+
62+
```json
63+
{
64+
"plugins": ["ts-ban-snippets"],
65+
"parserOptions": {
66+
"project": "./tsconfig.json"
67+
},
68+
"rules": {
69+
"ts-ban-snippets/ts-ban-snippets": [
70+
"error",
71+
{
72+
"banned": [
73+
{
74+
"snippets": ["return void reject", "return void resolve"],
75+
"message": "Please do not return void - instead place the return statement on the following line.",
76+
"excludePaths": ["excluded"]
77+
}
78+
]
79+
}
80+
]
81+
}
82+
}
83+
```
84+
5885
## authors
5986

6087
Original work by Sean Ryan - mr.sean.ryan(at gmail.com)

docs/ts-ban-snippets.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Banned TypeScript Snippets (`ts-ban-snippets`)
22

3-
## Example with Options
3+
## Simple Example with Options
44

55
### file: .eslintrc
66

@@ -26,3 +26,30 @@
2626
}
2727
}
2828
```
29+
30+
### Example with excluded paths
31+
32+
### file: .eslintrc
33+
34+
```json
35+
{
36+
"plugins": ["ts-ban-snippets"],
37+
"parserOptions": {
38+
"project": "./tsconfig.json"
39+
},
40+
"rules": {
41+
"ts-ban-snippets/ts-ban-snippets": [
42+
"error",
43+
{
44+
"banned": [
45+
{
46+
"snippets": ["return void reject", "return void resolve"],
47+
"message": "Please do not return void - instead place the return statement on the following line.",
48+
"excludePaths": ["excluded"]
49+
}
50+
]
51+
}
52+
]
53+
}
54+
}
55+
```

itests/simple-harness/.eslintrc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
{
1212
"snippets": ["return void reject", "return void resolve"],
1313
"message":
14-
"Please do not return void - instead place the return statement on the following line.",
15-
},
16-
],
14+
"Please do not return void - instead place the return statement on the following line."
15+
}
16+
]
1717
}
18-
],
18+
]
1919
}
2020
}

src/rules/ts-ban-snippets.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { RuleContext } from "@typescript-eslint/experimental-utils/dist/ts-eslin
66
type Banned = {
77
snippets: string[];
88
message: string;
9-
// TODO includePaths?: string excludePaths?: string
9+
// TODO includePaths?: string
10+
excludePaths?: string[];
1011
};
1112

1213
export type Options = [
@@ -26,11 +27,21 @@ const createRule = ESLintUtils.RuleCreator((name) => {
2627
const BannedSnippetMessage =
2728
"'{{name}}' is a banned code snippet - {{message}} [{{ruleName}}]";
2829

30+
function isFileInPaths(filePath: string, paths: string[]): boolean {
31+
return paths.some((path) => filePath.indexOf(path) >= 0);
32+
}
33+
2934
const analyzeNodeFor = (
3035
node: Node,
3136
banned: Banned,
3237
context: Readonly<RuleContext<"BannedSnippetMessage", Options>>
3338
) => {
39+
if (
40+
!!banned.excludePaths &&
41+
isFileInPaths(node.getSourceFile().fileName, banned.excludePaths)
42+
)
43+
return;
44+
3445
const text = node.getText();
3546

3647
node.getStart();
@@ -87,6 +98,12 @@ export default createRule<Options, MessageIds>({
8798
message: {
8899
type: "string",
89100
},
101+
excludePaths: {
102+
type: "array",
103+
items: {
104+
type: "string",
105+
},
106+
},
90107
},
91108
additionalProperties: false,
92109
},
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import fs from "fs";
2+
import path from "path";
3+
4+
import { ESLintUtils } from "@typescript-eslint/experimental-utils";
5+
6+
import rule from "../../src/rules/ts-ban-snippets";
7+
8+
// TODO dedupe (also the tsconfig)
9+
const ruleTester = new ESLintUtils.RuleTester({
10+
parser: "@typescript-eslint/parser",
11+
parserOptions: {
12+
project: "./tsconfig.eslint.json",
13+
tsconfigRootDir: path.join(__dirname, "fixtures"),
14+
sourceType: "module",
15+
ecmaFeatures: {
16+
jsx: true,
17+
},
18+
},
19+
});
20+
21+
const code = (name: string) =>
22+
fs.readFileSync(path.join(__dirname, name), "utf8");
23+
24+
ruleTester.run("ts-ban-snippets - excludePaths", rule, {
25+
valid: [
26+
{
27+
code: code("fixtures/valid/test.ts"),
28+
filename: "valid/test.ts",
29+
options: [
30+
{
31+
banned: [
32+
{
33+
snippets: ["return void reject", "return void resolve"],
34+
message:
35+
"Please do not return void - instead place the return statement on the following line.",
36+
},
37+
],
38+
},
39+
],
40+
},
41+
{
42+
code: code("fixtures/valid/test1-excluded.ts"),
43+
filename: "valid/test1-excluded.ts",
44+
options: [
45+
{
46+
banned: [
47+
{
48+
snippets: ["return void reject", "return void resolve"],
49+
message:
50+
"Please do not return void - instead place the return statement on the following line.",
51+
excludePaths: ["excluded"],
52+
},
53+
],
54+
},
55+
],
56+
},
57+
],
58+
invalid: [
59+
{
60+
code: code("fixtures/invalid/test1.ts"),
61+
filename: "invalid/test1.ts",
62+
options: [
63+
{
64+
banned: [
65+
{
66+
snippets: ["return void reject", "return void resolve"],
67+
message:
68+
"Please do not return void - instead place the return statement on the following line.",
69+
},
70+
],
71+
},
72+
],
73+
errors: [
74+
{
75+
messageId: "BannedSnippetMessage",
76+
data: {
77+
name: 'return void resolve("error!");',
78+
message:
79+
"Please do not return void - instead place the return statement on the following line.",
80+
ruleName: "ts-ban-snippets",
81+
},
82+
},
83+
],
84+
},
85+
],
86+
});

tests/excludePaths/fixtures/file.tsx

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class MyClass2 {
2+
doSomethingResolved() {
3+
return new Promise((resolve) => {
4+
return void resolve("error!");
5+
});
6+
}
7+
}
8+
9+
new MyClass2();
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
4+
/* Basic Options */
5+
// "incremental": true, /* Enable incremental compilation */
6+
"target": "es2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
7+
"module": "system", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
8+
"lib": ["es2017"], /* Specify library files to be included in the compilation. */
9+
// "allowJs": true, /* Allow javascript files to be compiled. */
10+
// "checkJs": true, /* Report errors in .js files. */
11+
"jsx": "react-native", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
12+
// "declaration": true, /* Generates corresponding '.d.ts' file. */
13+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
14+
// "sourceMap": true, /* Generates corresponding '.map' file. */
15+
// "outFile": "./", /* Concatenate and emit output to single file. */
16+
//"outFile": "./dist/index.js", /* Redirect output structure to the directory. */
17+
"rootDir": ".", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
18+
// "composite": true, /* Enable project compilation */
19+
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
20+
// "removeComments": true, /* Do not emit comments to output. */
21+
"noEmit": true, /* Do not emit outputs. */
22+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
23+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
24+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
25+
/* Strict Type-Checking Options */
26+
"strict": true, /* Enable all strict type-checking options. */
27+
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
28+
"strictNullChecks": true, /* Enable strict null checks. */
29+
"strictFunctionTypes": true, /* Enable strict checking of function types. */
30+
"strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
31+
"strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
32+
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
33+
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
34+
/* Additional Checks */
35+
"noUnusedLocals": true, /* Report errors on unused locals. */
36+
"noUnusedParameters": true, /* Report errors on unused parameters. */
37+
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
38+
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
39+
"noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
40+
/* Module Resolution Options */
41+
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
42+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
43+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
44+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
45+
// "typeRoots": [], /* List of folders to include type definitions from. */
46+
// "types": [], /* Type declaration files to be included in compilation. */
47+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
48+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
49+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
50+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
51+
/* Source Map Options */
52+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
53+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
54+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
55+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
56+
/* Experimental Options */
57+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
58+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
59+
/* Advanced Options */
60+
"skipLibCheck": true, /* Skip type checking of declaration files. */
61+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
62+
}
63+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from "./test1";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const foo = 1;
2+
3+
export default foo;

0 commit comments

Comments
 (0)