Skip to content

Allow tools to add information about objects #53632

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

Closed
magwas opened this issue Apr 2, 2023 · 4 comments
Closed

Allow tools to add information about objects #53632

magwas opened this issue Apr 2, 2023 · 4 comments

Comments

@magwas
Copy link

magwas commented Apr 2, 2023

Suggestion

πŸ” Search Terms

label:"Domain: Declaration Emit"

βœ… Viability Checklist

My suggestion meets these guidelines:

  • [x ] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x ] This wouldn't change the runtime behavior of existing JavaScript code
  • [x ] This could be implemented without emitting different JS based on the types of the expressions
  • [x ] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • [x ] This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

NO, this is NOT a duplicate of #41165 in any way.

Clean Code rules say that comments should not be written, the purpose of classes/functions/etc should be apparent from name, and the use of them should be apparent from appropriately named /annotatedtests/contracts. This rule have very good reasons.

The UI helps coders by showing the comments in the declaration file.

That means that in a Clean Code project, important informations for the coder can be harder to retrieve.

A solution to this is to use information provided by testing/contract checking frameworks to pass information to the compiler, which it can emit as comment in the declaration file.

Probably a json file containing the generated description for the emitted items would be a nice interface.

As the developer of https://github.com/kode-konveyor/cdd-ts a contract-checking framework I would be happy to discuss how such an interface should look like, provide reference implementation generating it, and even make a pull request to the compiler if I get some hints on where to put the changes.

πŸ“ƒ Motivating Example

I have my "check" function, written in Clean code, about which vscode now tells me the following: (method) Check<T extends SutType>.check(contract: ContractEntity<T>, sut: T): number

Note that there is nothing there beyond the call signature.

I could add a comment saying something like the following, which would be dutifully displayed by vscode, and be outdated at the moment I write it down.:

/*
check checks whether the contract actually corresponds to the behaviour of the SUT
 * it returns the number of runs checked in the contract"
 * if there is no ifCalledWith, a 'no ifCalledWith' error is thrown
 * if the return value is not according to the contract a 'return value mismatch' error is thrown
 * if a return value check fails, a 'return value check did not hold' error is thrown
 * with a 'when' we can use an environment manipulator to set up the environment
*/

Meanwhile I have the following contract for my check function, containing all the above information, and checked in each build. And the contract checker could easily provide the content of the above comment, up to date and checked to be true at all times.

export const CheckContract = 
    new Contract<(contract: Contract<typeof testedFunction>,fun: typeof testedFunction) =>number>()
    .setTitle("check checks whether the contract actually corresponds to the behaviour of the SUT")

    .ifCalledWith(getContract(),testedFunction)
    .thenReturn("it returns the number of runs checked in the contract",1)

    .ifCalledWith(getContractWithoutIfcalledWith(),testedFunction)
    .thenThrow("if there is no ifCalledWith, a 'no ifCalledWith' error is thrown",RUN_IDENTIFICATION+" no ifcalledWith")


    .ifCalledWith(getContractWithOtherReturnValue(),testedFunction)
    .thenThrow("if the return value is not according to the contract a 'return value mismatch' error is thrown",RegExp(RUN_IDENTIFICATION+" return value mismatch:.*expected:.2.*actual  :.1","ms"))
 
    .ifCalledWith(getContractWithFailingReturnvalueCheck(),testedFunction)
    .thenThrow("if a return value check fails, a 'return value check did not hold' error is thrown",RUN_IDENTIFICATION+" fail: return value check did not hold")
 
    .ifCalledWith(getContractWithACase(),testedFunction)
    .thenReturn("with a 'when' we can use an environment manipulator to set up the environment",2)

πŸ’» Use Cases

I would like to use it to "end discrimination of Clean Code projects by the IDE" :), as today users of libraries of clean code projects have less information available than old-style projects with comments.

In the meantime I look up tests/contracts, try to keep the info in my head, or experiment with code as workarounds.

@MartinJohns
Copy link
Contributor

Duplicate of #53205.

@magwas
Copy link
Author

magwas commented Apr 2, 2023

Duplicate of #53205.

Which is erroneously closed, as a duplicate of #41165, which it is not.

@MartinJohns
Copy link
Contributor

What you want can already be achieved by writing a language service plugin.

@magwas
Copy link
Author

magwas commented Apr 2, 2023

Now, this is an answer. Closing as invalid.

@magwas magwas closed this as completed Apr 2, 2023
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