Skip to content
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

Documentation of global variables #43

Closed
barakman opened this issue Aug 28, 2019 · 2 comments
Closed

Documentation of global variables #43

barakman opened this issue Aug 28, 2019 · 2 comments

Comments

@barakman
Copy link
Contributor

barakman commented Aug 28, 2019

As you know, the compiler implicitly creates a getter function for each global variable which is declared public.

Ideally, I'd like each one of these getter functions added to solidity-docgen's output (provided that I document them of course).

I assume that it might be a little difficult to actually display them as functions, because the solidity-parser (AST builder) adds them to the tree as variables.

So I would settle for having them displayed as variables.

For this purpose, I believe that you'd need to handle the VariableDeclaration node-type.

This means that in file solidity.js, you'd need to add:

  1. Inside class SolidityContract:
    get ownVariables() {
        return this.astNode.nodes
            .filter(isVariableDeclaration)
            .filter(n => n.visibility === 'public')
            .map(n => new SolidityVariable(this, n));
    }
  1. Outside class SolidityContract:
class SolidityVariable extends SolidityContractItem {
    constructor(contract, astNode) {
        super(contract, astNode);
        this.astNode = astNode;
    }
    get name() {
        return this.astNode.name;
    }
    get outputs() {
        return this.astNode.type;
    }
    get visibility() {
        return this.astNode.visibility;
    }
}
  1. Outside class SolidityContract:
function isVariableDeclaration(node) {
    return node.nodeType === 'VariableDeclaration';
}

Thanks

@frangio
Copy link
Contributor

frangio commented Aug 30, 2019

Thanks for the suggestion! Agree that we need to support this. Feel free to submit a pull request with these changes.

barakman added a commit to barakman/solidity-docgen that referenced this issue Sep 1, 2019
Following [this feature-request](OpenZeppelin#43), extending the `solidity-docgen` package to support the documentation of global variables, which are implicitly added by the compiler as getter functions in the contract.

This should allow users to get the natspec-documentation of their global variables added into the `solidity-docgen` output by extending the input template (hbs) file.

For example:
```
{{#if ownVariables}}
# Variables:
{{#ownVariables}}
- [`{{type}} {{name}}`](#{{anchor}})
{{/ownVariables}}
{{/if}}

{{#ownVariables}}
# Variable `{{type}} {{name}}` {#{{anchor~}} }
{{#if natspec.devdoc}}{{natspec.devdoc}}{{else}}No description{{/if}}
{{/ownVariables}}
```
Note that this PR is incomplete, since the `devdoc` of a global variable is always `undefined`.

For this reason, I also had to override function `SolidityContractItem.natspec()` in class `SolidityVariable` with a slightly different implementation (since `this.astNode.documentation === undefined`).
@frangio frangio changed the title Feature Request: support the documentation of global variables Documentation of global variables Oct 29, 2019
frangio pushed a commit that referenced this issue Nov 6, 2019
* Support the documentation of global variables

Following [this feature-request](#43), extending the `solidity-docgen` package to support the documentation of global variables, which are implicitly added by the compiler as getter functions in the contract.

This should allow users to get the natspec-documentation of their global variables added into the `solidity-docgen` output by extending the input template (hbs) file.

For example:
```
{{#if ownVariables}}
# Variables:
{{#ownVariables}}
- [`{{type}} {{name}}`](#{{anchor}})
{{/ownVariables}}
{{/if}}

{{#ownVariables}}
# Variable `{{type}} {{name}}` {#{{anchor~}} }
{{#if natspec.devdoc}}{{natspec.devdoc}}{{else}}No description{{/if}}
{{/ownVariables}}
```
Note that this PR is incomplete, since the `devdoc` of a global variable is always `undefined`.

For this reason, I also had to override function `SolidityContractItem.natspec()` in class `SolidityVariable` with a slightly different implementation (since `this.astNode.documentation === undefined`).

* Fix function `SolidityVariable.type()`

* Throw error in function SolidityVariable.natspec

Since NatSpec currently does NOT apply to public state variables (see ethereum/solidity#3418).

* Add `this.variables` to the array returned by `linkable`

As requested at #44 (comment).

* Update src/solidity.ts

Co-Authored-By: Francisco Giordano <frangio.1@gmail.com>

* Add the definition of `interface VariableDeclaration`

As requested at #44 (comment).

* Fix the `VariableDeclaration` interface

Remove the n/a `documentation` field.

* Fix class `SolidityVariable`

Let this class implement `Linkable` instead of extending `SolidityContractItem`.

* add missing semicolons and commas

* rename typeName to type

* add necessary members in variable ast node interface

* remove unused constructor argument

* finish implementation of state variable documentation

* add tests for state variables

* add warning for state variable natspec
@frangio
Copy link
Contributor

frangio commented Nov 6, 2019

Fixed in #44.

@frangio frangio closed this as completed Nov 6, 2019
douglasletz added a commit to douglasletz/pickle-solidity-pro that referenced this issue Sep 15, 2021
* Support the documentation of global variables

Following [this feature-request](OpenZeppelin/solidity-docgen#43), extending the `solidity-docgen` package to support the documentation of global variables, which are implicitly added by the compiler as getter functions in the contract.

This should allow users to get the natspec-documentation of their global variables added into the `solidity-docgen` output by extending the input template (hbs) file.

For example:
```
{{#if ownVariables}}
# Variables:
{{#ownVariables}}
- [`{{type}} {{name}}`](#{{anchor}})
{{/ownVariables}}
{{/if}}

{{#ownVariables}}
# Variable `{{type}} {{name}}` {#{{anchor~}} }
{{#if natspec.devdoc}}{{natspec.devdoc}}{{else}}No description{{/if}}
{{/ownVariables}}
```
Note that this PR is incomplete, since the `devdoc` of a global variable is always `undefined`.

For this reason, I also had to override function `SolidityContractItem.natspec()` in class `SolidityVariable` with a slightly different implementation (since `this.astNode.documentation === undefined`).

* Fix function `SolidityVariable.type()`

* Throw error in function SolidityVariable.natspec

Since NatSpec currently does NOT apply to public state variables (see ethereum/solidity#3418).

* Add `this.variables` to the array returned by `linkable`

As requested at OpenZeppelin/solidity-docgen#44 (comment).

* Update src/solidity.ts

Co-Authored-By: Francisco Giordano <frangio.1@gmail.com>

* Add the definition of `interface VariableDeclaration`

As requested at OpenZeppelin/solidity-docgen#44 (comment).

* Fix the `VariableDeclaration` interface

Remove the n/a `documentation` field.

* Fix class `SolidityVariable`

Let this class implement `Linkable` instead of extending `SolidityContractItem`.

* add missing semicolons and commas

* rename typeName to type

* add necessary members in variable ast node interface

* remove unused constructor argument

* finish implementation of state variable documentation

* add tests for state variables

* add warning for state variable natspec
Ishanamgai added a commit to Ishanamgai/F-Solidity that referenced this issue Feb 4, 2022
* Support the documentation of global variables

Following [this feature-request](OpenZeppelin/solidity-docgen#43), extending the `solidity-docgen` package to support the documentation of global variables, which are implicitly added by the compiler as getter functions in the contract.

This should allow users to get the natspec-documentation of their global variables added into the `solidity-docgen` output by extending the input template (hbs) file.

For example:
```
{{#if ownVariables}}
# Variables:
{{#ownVariables}}
- [`{{type}} {{name}}`](#{{anchor}})
{{/ownVariables}}
{{/if}}

{{#ownVariables}}
# Variable `{{type}} {{name}}` {#{{anchor~}} }
{{#if natspec.devdoc}}{{natspec.devdoc}}{{else}}No description{{/if}}
{{/ownVariables}}
```
Note that this PR is incomplete, since the `devdoc` of a global variable is always `undefined`.

For this reason, I also had to override function `SolidityContractItem.natspec()` in class `SolidityVariable` with a slightly different implementation (since `this.astNode.documentation === undefined`).

* Fix function `SolidityVariable.type()`

* Throw error in function SolidityVariable.natspec

Since NatSpec currently does NOT apply to public state variables (see ethereum/solidity#3418).

* Add `this.variables` to the array returned by `linkable`

As requested at OpenZeppelin/solidity-docgen#44 (comment).

* Update src/solidity.ts

Co-Authored-By: Francisco Giordano <frangio.1@gmail.com>

* Add the definition of `interface VariableDeclaration`

As requested at OpenZeppelin/solidity-docgen#44 (comment).

* Fix the `VariableDeclaration` interface

Remove the n/a `documentation` field.

* Fix class `SolidityVariable`

Let this class implement `Linkable` instead of extending `SolidityContractItem`.

* add missing semicolons and commas

* rename typeName to type

* add necessary members in variable ast node interface

* remove unused constructor argument

* finish implementation of state variable documentation

* add tests for state variables

* add warning for state variable natspec
topsuperdev added a commit to topsuperdev/Solidity-Pro that referenced this issue Jun 14, 2022
* Support the documentation of global variables

Following [this feature-request](OpenZeppelin/solidity-docgen#43), extending the `solidity-docgen` package to support the documentation of global variables, which are implicitly added by the compiler as getter functions in the contract.

This should allow users to get the natspec-documentation of their global variables added into the `solidity-docgen` output by extending the input template (hbs) file.

For example:
```
{{#if ownVariables}}
# Variables:
{{#ownVariables}}
- [`{{type}} {{name}}`](#{{anchor}})
{{/ownVariables}}
{{/if}}

{{#ownVariables}}
# Variable `{{type}} {{name}}` {#{{anchor~}} }
{{#if natspec.devdoc}}{{natspec.devdoc}}{{else}}No description{{/if}}
{{/ownVariables}}
```
Note that this PR is incomplete, since the `devdoc` of a global variable is always `undefined`.

For this reason, I also had to override function `SolidityContractItem.natspec()` in class `SolidityVariable` with a slightly different implementation (since `this.astNode.documentation === undefined`).

* Fix function `SolidityVariable.type()`

* Throw error in function SolidityVariable.natspec

Since NatSpec currently does NOT apply to public state variables (see ethereum/solidity#3418).

* Add `this.variables` to the array returned by `linkable`

As requested at OpenZeppelin/solidity-docgen#44 (comment).

* Update src/solidity.ts

Co-Authored-By: Francisco Giordano <frangio.1@gmail.com>

* Add the definition of `interface VariableDeclaration`

As requested at OpenZeppelin/solidity-docgen#44 (comment).

* Fix the `VariableDeclaration` interface

Remove the n/a `documentation` field.

* Fix class `SolidityVariable`

Let this class implement `Linkable` instead of extending `SolidityContractItem`.

* add missing semicolons and commas

* rename typeName to type

* add necessary members in variable ast node interface

* remove unused constructor argument

* finish implementation of state variable documentation

* add tests for state variables

* add warning for state variable natspec
yon3030 added a commit to yon3030/yantoColor that referenced this issue Oct 24, 2024
* Support the documentation of global variables

Following [this feature-request](OpenZeppelin/solidity-docgen#43), extending the `solidity-docgen` package to support the documentation of global variables, which are implicitly added by the compiler as getter functions in the contract.

This should allow users to get the natspec-documentation of their global variables added into the `solidity-docgen` output by extending the input template (hbs) file.

For example:
```
{{#if ownVariables}}
# Variables:
{{#ownVariables}}
- [`{{type}} {{name}}`](#{{anchor}})
{{/ownVariables}}
{{/if}}

{{#ownVariables}}
# Variable `{{type}} {{name}}` {#{{anchor~}} }
{{#if natspec.devdoc}}{{natspec.devdoc}}{{else}}No description{{/if}}
{{/ownVariables}}
```
Note that this PR is incomplete, since the `devdoc` of a global variable is always `undefined`.

For this reason, I also had to override function `SolidityContractItem.natspec()` in class `SolidityVariable` with a slightly different implementation (since `this.astNode.documentation === undefined`).

* Fix function `SolidityVariable.type()`

* Throw error in function SolidityVariable.natspec

Since NatSpec currently does NOT apply to public state variables (see ethereum/solidity#3418).

* Add `this.variables` to the array returned by `linkable`

As requested at OpenZeppelin/solidity-docgen#44 (comment).

* Update src/solidity.ts

Co-Authored-By: Francisco Giordano <frangio.1@gmail.com>

* Add the definition of `interface VariableDeclaration`

As requested at OpenZeppelin/solidity-docgen#44 (comment).

* Fix the `VariableDeclaration` interface

Remove the n/a `documentation` field.

* Fix class `SolidityVariable`

Let this class implement `Linkable` instead of extending `SolidityContractItem`.

* add missing semicolons and commas

* rename typeName to type

* add necessary members in variable ast node interface

* remove unused constructor argument

* finish implementation of state variable documentation

* add tests for state variables

* add warning for state variable natspec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants