-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support custom lint rules (#4118)
* feature: support custom lint rules - Adds a custom lint rule local package. - Tests for custom lint rules (not ran with main tests). - One initial rule to throw an error on any it.skip() or describe.skip() calls. - Updated docs. * refactor(lint): rename lint plugin and format
- Loading branch information
Showing
11 changed files
with
208 additions
and
7 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 |
---|---|---|
|
@@ -9,3 +9,4 @@ src/testFixtures/** | |
dist/** | ||
types/*.d.ts | ||
src.gen/** | ||
plugins/*/dist/** |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 @@ | ||
/*! | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import NoOnlyInTests from './lib/rules/no-only-in-tests' | ||
|
||
const rules = { | ||
'no-only-in-tests': NoOnlyInTests, | ||
} | ||
|
||
export { rules } |
65 changes: 65 additions & 0 deletions
65
plugins/eslint-plugin-aws-toolkits/lib/rules/no-only-in-tests.ts
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,65 @@ | ||
/*! | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { ESLintUtils } from '@typescript-eslint/utils' | ||
import { AST_NODE_TYPES } from '@typescript-eslint/types' | ||
import { CallExpression, Identifier, MemberExpression } from '@typescript-eslint/types/dist/generated/ast-spec' | ||
import { Rule } from 'eslint' | ||
|
||
function isValidExpression(node: CallExpression): MemberExpression | undefined { | ||
const isValid = | ||
node.callee.type === AST_NODE_TYPES.MemberExpression && | ||
node.callee.object.type === AST_NODE_TYPES.Identifier && | ||
node.callee.property.type === AST_NODE_TYPES.Identifier | ||
|
||
return isValid ? (node.callee as MemberExpression) : undefined | ||
} | ||
|
||
export const describeOnlyErrMsg = 'mocha test `.only()` not allowed for `describe`' | ||
export const itOnlyErrMsg = 'mocha test `.only()` not allowed for `it`' | ||
|
||
export default ESLintUtils.RuleCreator.withoutDocs({ | ||
meta: { | ||
docs: { | ||
description: "disallow mocha's only() from being published in test code", | ||
recommended: 'error', | ||
}, | ||
messages: { | ||
describeOnlyErrMsg, | ||
itOnlyErrMsg, | ||
}, | ||
type: 'problem', | ||
fixable: 'code', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (!isValidExpression(node)) { | ||
return | ||
} | ||
const expr = node.callee as MemberExpression | ||
const property = expr.property as Identifier | ||
const object = expr.object as Identifier | ||
|
||
if (property.name !== 'only') { | ||
return | ||
} | ||
|
||
if (object.name === 'describe' || object.name === 'it') { | ||
return context.report({ | ||
node: node.callee, | ||
messageId: `${object.name}OnlyErrMsg`, | ||
fix: fixer => { | ||
// Range - 1 removes the period in `it.only()` | ||
return fixer.removeRange([property.range[0] - 1, property.range[1]]) | ||
}, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
}) as unknown as Rule.RuleModule |
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,19 @@ | ||
{ | ||
"name": "eslint-plugin-aws-toolkits", | ||
"version": "1.0.0", | ||
"description": "Local custom lint rules for AWS Toolkit VSCode", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"build": "tsc", | ||
"test": "npm run build && mocha dist/test --recursive" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^8.26.0", | ||
"mocha": "^10.1.0", | ||
"typescript": "^5.0.4" | ||
}, | ||
"engines": { | ||
"npm": "^10.1.0" | ||
}, | ||
"license": "Apache-2.0" | ||
} |
39 changes: 39 additions & 0 deletions
39
plugins/eslint-plugin-aws-toolkits/test/rules/no-only-in-tests.test.ts
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,39 @@ | ||
/*! | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { rules } from '../..' | ||
import { describeOnlyErrMsg, itOnlyErrMsg } from '../../lib/rules/no-only-in-tests' | ||
import { getRuleTester } from '../testUtil' | ||
|
||
getRuleTester().run('no-only-in-tests', rules['no-only-in-tests'], { | ||
valid: [ | ||
"describe('my suite', function () {})", | ||
"describe('my suite', function () { it('does things', () => {})})", | ||
"it('does things', async function () {})", | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: "describe.only('mySuite', function () { it('does things', async function () {} ) })", | ||
errors: [describeOnlyErrMsg], | ||
output: "describe('mySuite', function () { it('does things', async function () {} ) })", | ||
}, | ||
{ | ||
code: "describe('mySuite', function() { it.only('does things', async function () { console.log('did things') })})", | ||
errors: [itOnlyErrMsg], | ||
output: "describe('mySuite', function() { it('does things', async function () { console.log('did things') })})", | ||
}, | ||
{ | ||
code: "describe.only('mySuite', function() { it.only('does things', async function () { console.log('did things') })})", | ||
errors: [describeOnlyErrMsg, itOnlyErrMsg], | ||
output: "describe('mySuite', function() { it('does things', async function () { console.log('did things') })})", | ||
}, | ||
{ | ||
code: "it.only('does things', async function () { console.log('did things') })", | ||
errors: [itOnlyErrMsg], | ||
output: "it('does things', async function () { console.log('did things') })", | ||
}, | ||
], | ||
}) |
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,18 @@ | ||
/*! | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { RuleTester } from 'eslint' | ||
|
||
export function getRuleTester() { | ||
return new RuleTester({ | ||
// TODO: For tests that need to access TS types, we will need to pass a parser: | ||
// parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
project: './tsconfig.json', | ||
tsconfigRootDir: __dirname, | ||
ecmaVersion: 2021, | ||
}, | ||
}) | ||
} |
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,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"incremental": true, | ||
"module": "commonjs", | ||
"target": "es2021", | ||
"outDir": "dist", | ||
"lib": ["dom", "es2021"], | ||
"sourceMap": true, | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"strict": true, | ||
"noUnusedLocals": true, | ||
"skipLibCheck": true | ||
}, | ||
"include": ["lib/**/*", "test/**/*", "index.ts"] | ||
} |