-
Notifications
You must be signed in to change notification settings - Fork 17
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(spec): eslint with custom rules APIC-387 #304
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8a6345c
feat(specs): eslint with custom rules
millotp 0d4375d
with postinstall
millotp ea6f121
test
millotp 17fb537
done
millotp 4045e94
Merge branch 'main' into feat/eslint-yml
millotp 63e77ce
fix for responses
millotp e187353
fix spec
millotp b0a5678
Merge branch 'main' into feat/eslint-yml
millotp 2c174d2
fix test
millotp a2cff83
comment
millotp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,8 @@ | ||
import type { Config } from '@jest/types'; | ||
|
||
const config: Config.InitialOptions = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
}; | ||
|
||
export default config; |
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,22 @@ | ||
{ | ||
"name": "eslint-plugin-automation-custom", | ||
"version": "1.0.0", | ||
"description": "Custom rules for eslint", | ||
"packageManager": "[email protected]", | ||
"main": "dist/index.js", | ||
"files": [ | ||
"src/**.ts" | ||
], | ||
"scripts": { | ||
"build": "tsc", | ||
"test": "jest" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "27.4.1", | ||
"eslint": "8.11.0", | ||
"jest": "27.5.1", | ||
"ts-jest": "27.1.3", | ||
"ts-node": "10.7.0", | ||
"typescript": "4.5.4" | ||
} | ||
} |
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,7 @@ | ||
import { descriptionDot } from './rules/descriptionDot'; | ||
|
||
const rules = { | ||
'description-dot': descriptionDot, | ||
}; | ||
|
||
export { rules }; |
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,60 @@ | ||
/* eslint-disable no-console */ | ||
import type { Rule } from 'eslint'; | ||
|
||
import { isBLockScalar, isPairWithKey, isScalar } from '../utils'; | ||
|
||
export const descriptionDot: Rule.RuleModule = { | ||
meta: { | ||
docs: { | ||
description: 'description must end with a dot', | ||
}, | ||
messages: { | ||
descriptionNoDot: 'description does not end with a dot', | ||
}, | ||
fixable: 'code', | ||
}, | ||
create(context) { | ||
if (!context.parserServices.isYAML) { | ||
return {}; | ||
} | ||
|
||
return { | ||
YAMLPair(node): void { | ||
if (!isPairWithKey(node, 'description')) { | ||
return; | ||
} | ||
if (!isScalar(node.value)) { | ||
return; | ||
} | ||
const value = node.value; | ||
if ( | ||
typeof value.value !== 'string' || | ||
value.value.trim().endsWith('.') || | ||
!value.value.trim().includes(' ') | ||
) { | ||
// The rule is respected if: | ||
// the description is not a string | ||
// or it ends with a dot | ||
// or it's a single word (like 'OK' or 'Success', it's not a sentence) | ||
return; | ||
} | ||
|
||
// trim the whitespaces at the end before adding the dot. This assume the indent is 2 | ||
let toTrim = value.value.length - value.value.trimEnd().length; | ||
if (isBLockScalar(value)) { | ||
toTrim += node.key!.loc.start.column + 2; | ||
} | ||
context.report({ | ||
node: node as any, | ||
messageId: 'descriptionNoDot', | ||
fix(fixer) { | ||
return fixer.insertTextAfterRange( | ||
[0, value.range[1] - toTrim], | ||
'.' | ||
); | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
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,20 @@ | ||
import type { AST } from 'yaml-eslint-parser'; | ||
|
||
export function isScalar(node: AST.YAMLNode | null): node is AST.YAMLScalar { | ||
return node !== null && node.type === 'YAMLScalar'; | ||
} | ||
|
||
export function isBLockScalar( | ||
node: AST.YAMLNode | null | ||
): node is AST.YAMLBlockFoldedScalar | AST.YAMLBlockLiteralScalar { | ||
return isScalar(node) && 'chomping' in node; | ||
} | ||
|
||
export function isPairWithKey( | ||
node: AST.YAMLNode | null, | ||
key: string | ||
): node is AST.YAMLPair { | ||
if (node === null || node.type !== 'YAMLPair' || node.key === null) | ||
return false; | ||
return isScalar(node.key) && node.key.value === key; | ||
} |
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,63 @@ | ||
import { RuleTester } from 'eslint'; | ||
|
||
import { descriptionDot } from '../src/rules/descriptionDot'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: require.resolve('yaml-eslint-parser'), | ||
}); | ||
|
||
ruleTester.run('description-dot', descriptionDot, { | ||
valid: [ | ||
` | ||
simple: | ||
type: number | ||
description: a number. | ||
`, | ||
` | ||
multi: | ||
description: > | ||
Creates a new A/B test with provided configuration. | ||
|
||
You can set an A/B test on two different indices with different settings, or on the same index with different search parameters by providing a customSearchParameters setting on one of the variants. | ||
`, | ||
` | ||
multiStrip: | ||
description: >- | ||
Multiline comment | ||
on description. | ||
`, | ||
` | ||
responses: | ||
'200': | ||
description: OK | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
simple: | ||
description: a number | ||
`, | ||
errors: [{ messageId: 'descriptionNoDot' }], | ||
output: ` | ||
simple: | ||
description: a number. | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
multi: | ||
description: > | ||
Multiline comment | ||
on description | ||
`, | ||
errors: [{ messageId: 'descriptionNoDot' }], | ||
output: ` | ||
multi: | ||
description: > | ||
Multiline comment | ||
on description. | ||
`, | ||
}, | ||
], | ||
}); |
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,13 @@ | ||
{ | ||
"extends": "../config/base.tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
}, | ||
"include": [ | ||
"src/**/*.ts", | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
"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
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
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
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
Oops, something went wrong.
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.
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.
what does it mean?
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.
I've added a comment for clarity