question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Strict import scope and function signature semantics

See original GitHub issue

Summary of the new feature/enhancement

The current version of PowerShell is actually a very runtime driven framework. Function calls are only checked runtime whether the function is present and the parameters are met. Import-Module instructions only execute runtime and there is no guidance design time which functions will be pulled into the current scope.

What we like to see is a more design time driven experience such as frameworks as TypeScript Nodejs. The benefit of the EcmaScript module system is that you know design time which functions you are pulling into scope. Additionally, the import process of Nodejs is much faster than PowerShell. The benefit of TypeScript is that function calls are validated design time. When a parameter or return type is not met, the file simply does not get compiled. PowerShell should block any function use that is not coming from an import statement. TypeScript nodejs files simply don’t compile when unknown functions are being called.

E.g.

# Import specific functions / cmdlets
Import Get-AzResourceGroup, Get-AzKeyVault from Az
Get-AzResourceGroup -Name xyz

# Import the whole set of exported functions / cmdlets to a variable
Import * as $Az from Az
$Az.Get-AzResourceGroup -Name xyz

# This should not compile / execute and should throw a parameter mismatch error
Get-AzResourceGroup -RandomParameter

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
cveldcommented, Jun 18, 2021

@rjmholt Has there been any recent development? Many of our bugs are caused by insufficient static analysis. Would it be easy to start with some manual decoration language so that you can help the language service? If we were able to adopt something like the TypeScript module import semantics, then the language service can limit its analysis to the particular file.

1reaction
rjmholtcommented, Feb 25, 2021

I think if you want strict validation of function signatures, that might be better suited as a PSScriptAnalyzer rule cc @rjmholt @JamesWTruher

I’ve done some work toward this. Requires investment in a static parameter binding approximation algorithm. Which PSScriptAnalyzer has general need of. But this plus more performant profiles would be the silver bullet for static analysis.

Read more comments on GitHub >

github_iconTop Results From Across the Web

require() of ES modules is not supported when importing ...
In case someone is solving this issue in 2022: Recent versions of nodejs already includes global fetch api. It is still an experimental...
Read more >
Support for type hints — Python 3.11.4 documentation
This module provides runtime support for type hints. For the original specification of the typing system, see PEP 484. For a simplified introduction...
Read more >
ESLint - Error: Must use import to load ES Module
Run npm i from a terminal/command prompt in the folder; In .eslintrc, update the parser line "parser": "babel-eslint", to "parser" ...
Read more >
Module signature - Haskell « HaskellWiki
A module signature is a type signature for a module. ... and only those functions will be brought into scope when you import...
Read more >
function declaration - JavaScript - MDN Web Docs - Mozilla
The function declaration creates a binding of a new function to a given name. You can also define functions using the function expression....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found