Skip to content

feat(spec): eslint with custom rules APIC-387 #304

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

Merged
merged 10 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
{
files: ['*.yml'],
parser: 'yaml-eslint-parser',
plugins: ["automation-custom"],
rules: {
'@typescript-eslint/naming-convention': 0,
'yml/quotes': [
Expand All @@ -31,10 +32,14 @@ module.exports = {
},
],
'yml/require-string-key': 2,

// Should be removed once the specs are finished
'yml/no-empty-document': 0,
},
overrides: [{
files: ['specs/**/*.yml'],
rules: {
"automation-custom/description-dot": "error",
}
}
]
},
],

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ jobs:
- name: Test scripts
run: yarn scripts:test

- name: Test custom eslint plugin
run: yarn workspace eslint test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be yarn workspace eslint-plugin-automation-custom test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep I forgot to change it, thanks !


specs:
runs-on: ubuntu-20.04
timeout-minutes: 10
Expand Down
8 changes: 8 additions & 0 deletions eslint/jest.config.ts
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;
22 changes: 22 additions & 0 deletions eslint/package.json
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"
}
}
7 changes: 7 additions & 0 deletions eslint/src/index.ts
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 };
38 changes: 38 additions & 0 deletions eslint/src/rules/descriptionDot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { Rule } from 'eslint';
import type { AST } from 'yaml-eslint-parser';

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 (node.key.value !== 'description') {
return;
}
const value: AST.YAMLScalar = node.value.value;
if (typeof value.value !== 'string' || value.value.endsWith('.')) {
return;
}
context.report({
node,
messageId: 'descriptionNoDot',
fix(fixer) {
return fixer.insertTextAfterRange(value.range, '.');
},
});
},
};
},
};
73 changes: 73 additions & 0 deletions eslint/tests/descriptionDot.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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.
`,
`
single:
can: have
description: simple.
` /*
`
multi:
description: >
Multiline comment
on description.
`,*/,
`
multiStrip:
description: >-
Multiline comment
on description.
`,
],
invalid: [
{
code: `
simple:
description: a number
`,
errors: [{ messageId: 'descriptionNoDot' }],
output: `
simple:
description: a number.
`,
},
{
code: `
single:
description: simple
`,
errors: [{ messageId: 'descriptionNoDot' }],
output: `
single:
description: simple.
`,
} /*
{
code: `
multi:
description: >
Multiline comment
on description
`,
errors: [{ messageId: 'descriptionNoDot' }],
output: `
multi:
description: >
Multiline comment
on description.
`,
},*/,
],
});
13 changes: 13 additions & 0 deletions eslint/tsconfig.json
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"
]
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"playground/javascript/browser/",
"scripts/",
"tests/output/javascript",
"website/"
"website/",
"eslint"
],
"scripts": {
"cli": "yarn workspace scripts ts-node --transpile-only ./index.ts",
Expand All @@ -19,6 +20,8 @@
"docker": "docker exec -it dev yarn cli $*",
"github-actions:lint": "eslint --ext=yml .github/",
"playground:browser": "yarn workspace javascript-browser-playground start",
"postinstall": "yarn workspace eslint-plugin-automation-custom build",
"build:eslint": "yarn workspace eslint-plugin-automation-custom build && yarn install",
"release": "yarn workspace scripts createReleaseIssue",
"scripts:lint": "eslint --ext=ts scripts/",
"scripts:test": "yarn workspace scripts test",
Expand All @@ -37,6 +40,7 @@
"eslint-config-algolia": "20.0.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-algolia": "2.0.0",
"eslint-plugin-automation-custom": "1.0.0",
"eslint-plugin-eslint-comments": "3.2.0",
"eslint-plugin-import": "2.25.4",
"eslint-plugin-jsdoc": "37.9.7",
Expand Down
113 changes: 113 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ __metadata:
eslint-config-algolia: 20.0.0
eslint-config-prettier: 8.5.0
eslint-plugin-algolia: 2.0.0
eslint-plugin-automation-custom: 1.0.0
eslint-plugin-eslint-comments: 3.2.0
eslint-plugin-import: 2.25.4
eslint-plugin-jsdoc: 37.9.7
Expand Down Expand Up @@ -10077,6 +10078,19 @@ __metadata:
languageName: node
linkType: hard

"[email protected], eslint-plugin-automation-custom@workspace:eslint":
version: 0.0.0-use.local
resolution: "eslint-plugin-automation-custom@workspace:eslint"
dependencies:
"@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
languageName: unknown
linkType: soft

"eslint-plugin-eslint-comments@npm:3.2.0":
version: 3.2.0
resolution: "eslint-plugin-eslint-comments@npm:3.2.0"
Expand Down Expand Up @@ -10233,6 +10247,51 @@ __metadata:
languageName: node
linkType: hard

"eslint@npm:8.11.0":
version: 8.11.0
resolution: "eslint@npm:8.11.0"
dependencies:
"@eslint/eslintrc": ^1.2.1
"@humanwhocodes/config-array": ^0.9.2
ajv: ^6.10.0
chalk: ^4.0.0
cross-spawn: ^7.0.2
debug: ^4.3.2
doctrine: ^3.0.0
escape-string-regexp: ^4.0.0
eslint-scope: ^7.1.1
eslint-utils: ^3.0.0
eslint-visitor-keys: ^3.3.0
espree: ^9.3.1
esquery: ^1.4.0
esutils: ^2.0.2
fast-deep-equal: ^3.1.3
file-entry-cache: ^6.0.1
functional-red-black-tree: ^1.0.1
glob-parent: ^6.0.1
globals: ^13.6.0
ignore: ^5.2.0
import-fresh: ^3.0.0
imurmurhash: ^0.1.4
is-glob: ^4.0.0
js-yaml: ^4.1.0
json-stable-stringify-without-jsonify: ^1.0.1
levn: ^0.4.1
lodash.merge: ^4.6.2
minimatch: ^3.0.4
natural-compare: ^1.4.0
optionator: ^0.9.1
regexpp: ^3.2.0
strip-ansi: ^6.0.1
strip-json-comments: ^3.1.0
text-table: ^0.2.0
v8-compile-cache: ^2.0.3
bin:
eslint: bin/eslint.js
checksum: a06a2ea37002d6c0a4f462fe31b4411185dc3da7857fafb896eb392ba95a1289cc3538056474b2f44f08012f265bede01a39d46df4ac39ebc6d7be90e2c8f9fa
languageName: node
linkType: hard

"eslint@npm:8.12.0":
version: 8.12.0
resolution: "eslint@npm:8.12.0"
Expand Down Expand Up @@ -20295,6 +20354,40 @@ __metadata:
languageName: node
linkType: hard

"ts-jest@npm:27.1.3":
version: 27.1.3
resolution: "ts-jest@npm:27.1.3"
dependencies:
bs-logger: 0.x
fast-json-stable-stringify: 2.x
jest-util: ^27.0.0
json5: 2.x
lodash.memoize: 4.x
make-error: 1.x
semver: 7.x
yargs-parser: 20.x
peerDependencies:
"@babel/core": ">=7.0.0-beta.0 <8"
"@types/jest": ^27.0.0
babel-jest: ">=27.0.0 <28"
esbuild: ~0.14.0
jest: ^27.0.0
typescript: ">=3.8 <5.0"
peerDependenciesMeta:
"@babel/core":
optional: true
"@types/jest":
optional: true
babel-jest:
optional: true
esbuild:
optional: true
bin:
ts-jest: cli.js
checksum: eb54e5b8fc5f06e4cc20ecec7891201ddc78a3537d5eb3775e29ffbc7e83fd2a68f91db801b6a8c820c872060b24dc41fb6decac800b76256d3cdda6520b5c4f
languageName: node
linkType: hard

"ts-jest@npm:27.1.4":
version: 27.1.4
resolution: "ts-jest@npm:27.1.4"
Expand Down Expand Up @@ -20554,6 +20647,16 @@ __metadata:
languageName: node
linkType: hard

"typescript@npm:4.5.4":
version: 4.5.4
resolution: "typescript@npm:4.5.4"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 59f3243f9cd6fe3161e6150ff6bf795fc843b4234a655dbd938a310515e0d61afd1ac942799e7415e4334255e41c2c49b7dd5d9fd38a17acd25a6779ca7e0961
languageName: node
linkType: hard

"typescript@npm:4.6.3":
version: 4.6.3
resolution: "typescript@npm:4.6.3"
Expand All @@ -20564,6 +20667,16 @@ __metadata:
languageName: node
linkType: hard

"typescript@patch:[email protected]#~builtin<compat/typescript>":
version: 4.5.4
resolution: "typescript@patch:typescript@npm%3A4.5.4#~builtin<compat/typescript>::version=4.5.4&hash=493e53"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 2e488dde7d2c4a2fa2e79cf2470600f8ce81bc0563c276b72df8ff412d74456ae532ba824650ae936ce207440c79720ddcfaa25e3cb4477572b8534fa4e34d49
languageName: node
linkType: hard

"typescript@patch:[email protected]#~builtin<compat/typescript>":
version: 4.6.3
resolution: "typescript@patch:typescript@npm%3A4.6.3#~builtin<compat/typescript>::version=4.6.3&hash=493e53"
Expand Down