-
Notifications
You must be signed in to change notification settings - Fork 13
feat: create linter system #191
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
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
63e0d97
feat: linter
flakey5 0036260
feat: wip
araujogui 4aa4f77
fix: optional location
araujogui 5e1c0ec
fix: create hasError getter
araujogui b396444
fix: optional location
araujogui b443a7a
test: update expected metadata
araujogui 5248f11
refactor: some changes
araujogui 7dfb6dc
fix: missing reporter option
araujogui 762adeb
fix: reporter node position
araujogui b7e8597
refactor: some improvements
araujogui 95f67ab
refactor: better docs
araujogui d7394b7
refactor: remove useless directive
araujogui 622e6a3
refactor: remove semver regex
araujogui a2dae43
refactor: add yaml_position description
araujogui 3e912e7
test: create rules tests
araujogui 7e9a3f4
test: create reporters tests
araujogui bcebcdd
fix: missing change version issue location
araujogui 881ce3c
feat: add disable rule cli option
araujogui 5aeccfe
test: create engine test
araujogui bf4d935
feat: create cli option
araujogui fab1c6a
test: oops
araujogui b1fa0a1
chore: add FORCE_COLOR env
araujogui 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,66 @@ | ||
// @ts-check | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'use strict'; | ||
|
||
import reporters from './reporters/index.mjs'; | ||
import { invalidChangeVersion } from './rules/invalid-change-version.mjs'; | ||
import { missingChangeVersion } from './rules/missing-change-version.mjs'; | ||
import { missingIntroducedIn } from './rules/missing-introduced-in.mjs'; | ||
|
||
/** | ||
* Lint issues in ApiDocMetadataEntry entries | ||
*/ | ||
export class Linter { | ||
/** | ||
* @type {Array<import('./types.d.ts').LintIssue>} | ||
*/ | ||
#issues = []; | ||
|
||
/** | ||
* @type {Array<import('./types.d.ts').LintRule>} | ||
*/ | ||
#rules = [missingIntroducedIn, missingChangeVersion, invalidChangeVersion]; | ||
|
||
/** | ||
* @param {ApiDocMetadataEntry} entry | ||
* @returns {void} | ||
*/ | ||
lint(entry) { | ||
for (const rule of this.#rules) { | ||
const issues = rule(entry); | ||
|
||
if (issues.length > 0) { | ||
this.#issues.push(...issues); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param {ApiDocMetadataEntry[]} entries | ||
* @returns {void} | ||
*/ | ||
lintAll(entries) { | ||
for (const entry of entries) { | ||
this.lint(entry); | ||
} | ||
} | ||
|
||
/** | ||
* @param {keyof reporters} reporterName | ||
*/ | ||
report(reporterName) { | ||
const reporter = reporters[reporterName]; | ||
|
||
for (const issue of this.#issues) { | ||
reporter(issue); | ||
} | ||
} | ||
|
||
/** | ||
* Returns whether there are any issues with a level of 'error' | ||
* | ||
* @returns {boolean} | ||
*/ | ||
get hasError() { | ||
return this.#issues.some(issue => issue.level === 'error'); | ||
} | ||
} |
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,31 @@ | ||
// @ts-check | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
'use strict'; | ||
|
||
import { styleText } from 'node:util'; | ||
|
||
/** | ||
* @type {Record<import('../types.d.ts').IssueLevel, string>} | ||
*/ | ||
const levelToColorMap = { | ||
info: 'gray', | ||
warn: 'yellow', | ||
error: 'red', | ||
}; | ||
|
||
/** | ||
* @type {import('../types.d.ts').Reporter} | ||
*/ | ||
export default issue => { | ||
const position = issue.location.position | ||
? ` (${issue.location.position.start}:${issue.location.position.end})` | ||
: ''; | ||
|
||
console.log( | ||
styleText( | ||
// @ts-expect-error ForegroundColors is not exported | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
levelToColorMap[issue.level], | ||
`${issue.message} at ${issue.location.path}${position}` | ||
) | ||
); | ||
}; |
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,24 @@ | ||
// @ts-check | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
'use strict'; | ||
|
||
import * as core from '@actions/core'; | ||
|
||
/** | ||
* GitHub action reporter for | ||
* | ||
* @type {import('../types.d.ts').Reporter} | ||
*/ | ||
export default issue => { | ||
const actions = { | ||
warn: core.warning, | ||
error: core.error, | ||
info: core.notice, | ||
}; | ||
|
||
(actions[issue.level] || core.notice)(issue.message, { | ||
araujogui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
file: issue.location.path, | ||
startLine: issue.location.position?.start.line, | ||
endLine: issue.location.position?.end.line, | ||
}); | ||
}; |
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,9 @@ | ||
'use strict'; | ||
|
||
import console from './console.mjs'; | ||
import github from './github.mjs'; | ||
|
||
export default /** @type {const} */ ({ | ||
console, | ||
github, | ||
}); |
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,33 @@ | ||
import { validateVersion } from '../utils/semver.mjs'; | ||
|
||
/** | ||
* Checks if any change version is invalid. | ||
* | ||
* @param {ApiDocMetadataEntry} entry | ||
* @returns {Array<import('../types').LintIssue>} | ||
*/ | ||
export const invalidChangeVersion = entry => { | ||
if (entry.changes.length === 0) { | ||
return []; | ||
} | ||
|
||
const allVersions = entry.changes | ||
.filter(change => change.version) | ||
.flatMap(change => | ||
Array.isArray(change.version) ? change.version : [change.version] | ||
); | ||
|
||
const invalidVersions = allVersions.filter( | ||
version => !validateVersion(version.substring(1)) // Trim the leading 'v' from the version string | ||
); | ||
|
||
return invalidVersions.map(version => ({ | ||
level: 'warn', | ||
message: `Invalid version number: ${version}`, | ||
location: { | ||
path: entry.api_doc_source, | ||
line: entry.yaml_position.start.line, | ||
column: entry.yaml_position.start.column, | ||
}, | ||
})); | ||
}; |
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,23 @@ | ||
/** | ||
* Checks if any change version is missing. | ||
* | ||
* @param {ApiDocMetadataEntry} entry | ||
* @returns {Array<import('../types').LintIssue>} | ||
*/ | ||
export const missingChangeVersion = entry => { | ||
if (entry.changes.length === 0) { | ||
return []; | ||
} | ||
|
||
return entry.changes | ||
.filter(change => !change.version) | ||
.map(() => ({ | ||
level: 'warn', | ||
message: 'Missing change version', | ||
location: { | ||
path: entry.api_doc_source, | ||
line: entry.yaml_position.start.line, | ||
column: entry.yaml_position.start.column, | ||
}, | ||
})); | ||
}; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.