-
Notifications
You must be signed in to change notification settings - Fork 8
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: logic for new config #45
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1a42dd3
refactor: config structure
62f09ff
Merge branch 'main' into feat/new-config
excaliborr 9d5d69a
chore: fix comments
3c14067
chore: fix comments
406735d
feat: logic for new functions config
f87aca4
Merge branch 'main' into feat/new-config
8c3625a
feat: finishing logic for remaining identifiers
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
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 |
---|---|---|
@@ -1,5 +1,15 @@ | ||
import { Config, Natspec, NodeToProcess } from './types'; | ||
import { matchesFunctionKind, getElementFrequency } from './utils'; | ||
import { | ||
Config, | ||
FunctionConfig, | ||
Functions, | ||
HasVParameters, | ||
Natspec, | ||
NatspecDefinition, | ||
NodeToProcess, | ||
KeysForSupportedTags, | ||
Tags, | ||
} from './types'; | ||
import { matchesFunctionKind, getElementFrequency, isKeyForSupportedTags } from './utils'; | ||
import { | ||
EnumDefinition, | ||
ErrorDefinition, | ||
|
@@ -50,24 +60,86 @@ export class Validator { | |
} | ||
|
||
// Inheritdoc is not enforced nor present, and there is no other documentation, returning error | ||
if (!natspec.tags.length) return [`Natspec is missing`]; | ||
if (!natspec.tags.length) { | ||
let needsWarning = false; | ||
// If node is a function, check the user defined config | ||
if (node instanceof FunctionDefinition) { | ||
Object.keys(this.config.functions).forEach((key) => { | ||
Object.keys(this.config.functions[key as keyof Functions].tags).forEach((tag) => { | ||
if (this.config.functions[key as keyof Functions][tag as keyof FunctionConfig]) { | ||
needsWarning = true; | ||
} | ||
}); | ||
}); | ||
} else { | ||
// The other config rules use the same datatype so we can check them here | ||
Object.keys(this.config).forEach((key) => { | ||
if (isKeyForSupportedTags(key)) { | ||
const tagsConfig = this.config[key]?.tags; | ||
if (tagsConfig) { | ||
Object.values(tagsConfig).forEach((value) => { | ||
if (value) { | ||
needsWarning = true; | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
if (needsWarning) return [`Natspec is missing`]; | ||
} | ||
|
||
// Validate the completeness of the documentation | ||
let alerts: string[] = []; | ||
let isDevTagForced: boolean; | ||
let isNoticeTagForced: boolean; | ||
|
||
if (node instanceof EnumDefinition) { | ||
// TODO: Process enums | ||
} else if (node instanceof ErrorDefinition) { | ||
alerts = [...alerts, ...this.validateParameters(node, natspecParams)]; | ||
isDevTagForced = this.config.errors.tags.dev; | ||
isNoticeTagForced = this.config.errors.tags.notice; | ||
|
||
alerts = [ | ||
...alerts, | ||
...this.validateParameters(node, natspecParams, 'errors'), | ||
...this.validateTags(isDevTagForced, isNoticeTagForced, natspec.tags), | ||
]; | ||
} else if (node instanceof EventDefinition) { | ||
alerts = [...alerts, ...this.validateParameters(node, natspecParams)]; | ||
isDevTagForced = this.config.events.tags.dev; | ||
isNoticeTagForced = this.config.events.tags.notice; | ||
|
||
alerts = [ | ||
...alerts, | ||
...this.validateParameters(node, natspecParams, 'events'), | ||
...this.validateTags(isDevTagForced, isNoticeTagForced, natspec.tags), | ||
]; | ||
} else if (node instanceof FunctionDefinition) { | ||
const natspecReturns = natspec.returns.map((p) => p.name); | ||
alerts = [...alerts, ...this.validateParameters(node, natspecParams), ...this.validateReturnParameters(node, natspecReturns)]; | ||
isDevTagForced = this.config.functions[node.visibility as keyof Functions]?.tags.dev; | ||
isNoticeTagForced = this.config.functions[node.visibility as keyof Functions]?.tags.notice; | ||
|
||
alerts = [ | ||
...alerts, | ||
...this.validateParameters(node, natspecParams), | ||
...this.validateReturnParameters(node, natspecReturns), | ||
...this.validateTags(isDevTagForced, isNoticeTagForced, natspec.tags), | ||
]; | ||
} else if (node instanceof ModifierDefinition) { | ||
alerts = [...alerts, ...this.validateParameters(node, natspecParams)]; | ||
isDevTagForced = this.config.modifiers.tags.dev; | ||
isNoticeTagForced = this.config.modifiers.tags.notice; | ||
|
||
alerts = [ | ||
...alerts, | ||
...this.validateParameters(node, natspecParams, 'modifiers'), | ||
...this.validateTags(isDevTagForced, isNoticeTagForced, natspec.tags), | ||
]; | ||
} else if (node instanceof StructDefinition) { | ||
alerts = [...alerts, ...this.validateMembers(node, natspecParams)]; | ||
isDevTagForced = this.config.structs.tags.dev; | ||
isNoticeTagForced = this.config.structs.tags.notice; | ||
|
||
alerts = [...alerts, ...this.validateMembers(node, natspecParams), ...this.validateTags(isDevTagForced, isNoticeTagForced, natspec.tags)]; | ||
} else if (node instanceof VariableDeclaration) { | ||
// Only the presence of a notice is validated | ||
} | ||
|
@@ -82,11 +154,25 @@ export class Validator { | |
* @param {string[]} natspecParams - The list of parameters from the natspec | ||
* @returns {string[]} - The list of alerts | ||
*/ | ||
private validateParameters(node: ErrorDefinition | FunctionDefinition | ModifierDefinition, natspecParams: (string | undefined)[]): string[] { | ||
private validateParameters<T extends HasVParameters>( | ||
node: T, | ||
natspecParams: (string | undefined)[], | ||
key: KeysForSupportedTags | undefined = undefined | ||
): string[] { | ||
let definedParameters = node.vParameters.vParameters.map((p) => p.name); | ||
let alerts: string[] = []; | ||
const counter = getElementFrequency(natspecParams); | ||
|
||
if (node instanceof FunctionDefinition) { | ||
if (!this.config.functions[node.visibility as keyof Functions]?.tags.param) { | ||
return []; | ||
} | ||
} else if (key !== undefined) { | ||
if (!this.config[key]?.tags.param) { | ||
return []; | ||
} | ||
} | ||
|
||
for (let paramName of definedParameters) { | ||
if (!natspecParams.includes(paramName)) { | ||
alerts.push(`@param ${paramName} is missing`); | ||
|
@@ -105,6 +191,10 @@ export class Validator { | |
* @returns {string[]} - The list of alerts | ||
*/ | ||
private validateMembers(node: StructDefinition, natspecParams: (string | undefined)[]): string[] { | ||
if (!this.config.structs.tags.param) { | ||
return []; | ||
} | ||
|
||
let members = node.vMembers.map((p) => p.name); | ||
let alerts: string[] = []; | ||
const counter = getElementFrequency(natspecParams); | ||
|
@@ -128,6 +218,11 @@ export class Validator { | |
* @returns {string[]} - The list of alerts | ||
*/ | ||
private validateReturnParameters(node: FunctionDefinition, natspecReturns: (string | undefined)[]): string[] { | ||
// If return tags are not enforced, return no warnings | ||
if (!this.config.functions[node.visibility as keyof Functions]?.tags.return) { | ||
return []; | ||
} | ||
|
||
let alerts: string[] = []; | ||
let functionReturns = node.vReturnParameters.vParameters.map((p) => p.name); | ||
|
||
|
@@ -145,6 +240,46 @@ export class Validator { | |
return alerts; | ||
} | ||
|
||
private validateTags(isDevTagForced: boolean, isNoticeTagForced: boolean, natspecTags: NatspecDefinition[]): string[] { | ||
// If both are disabled no warnings should emit so we dont need to check anything | ||
if (!isDevTagForced && !isNoticeTagForced) { | ||
return []; | ||
} | ||
|
||
let alerts: string[] = []; | ||
|
||
let devCounter = 0; | ||
let noticeCounter = 0; | ||
|
||
for (const tag of natspecTags) { | ||
if (tag.name === 'dev') { | ||
devCounter++; | ||
} else if (tag.name === 'notice') { | ||
noticeCounter++; | ||
} | ||
} | ||
|
||
// Needs a dev tag | ||
// More then one dev tag is ok | ||
if (isDevTagForced && devCounter === 0) { | ||
alerts.push(`@dev is missing`); | ||
} | ||
|
||
if (isNoticeTagForced) { | ||
// Needs one notice tag | ||
if (noticeCounter === 0) { | ||
alerts.push(`@notice is missing`); | ||
} | ||
|
||
// Cant have more then one notice tag | ||
if (noticeCounter > 1) { | ||
alerts.push(`@notice is duplicated`); | ||
} | ||
} | ||
Comment on lines
+274
to
+277
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it's a valid scenario. At the same time, having duplicated |
||
|
||
return alerts; | ||
} | ||
|
||
/** | ||
* Checks if the node requires inheritdoc | ||
* @param {NodeToProcess} node - The node to process | ||
|
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.
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.
One thing to note here, we could avoid repeating
as keyof
by converting the config into a class and adding helpers to it. This approach will also help with retrieving the rest of the rules, let's discuss it later when the logic is fully developed.