microsoft / microsoft/TypeScript

Invalid recursive methods or functions

Ouverte
#55,643 6 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Needs Proposal Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

🔍 Search Terms

Invalid recursive methods or functions not detected as error by typescript compiler during compile time

✅ Viability Checklist
  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion

// TypeScript compiler should detect invalid recursive functions and methods

// Invalid Recursions should not compile and should be reported as error during compile time

// Examples :
function testFunction(i:number) {
return testFunction(i); // compiler should warn this as invalid recursive function
}

let testFunction2 = function (i:number) {
return testFunction2(i); // compiler should warn this as invalid recursive function
}

let testFunction3 :(i:number)=>number = (i:number)=>{
return testFunction3(i); // compiler should warn this as invalid recursive function
}

testFunction(1); // compiler should warn this as invalid recursive function call
testFunction2(1); // compiler should warn this as invalid recursive function call
testFunction3(1); // compiler should warn this as invalid recursive function call

class Testing {
id:number=0;

test1():number{
return this.test1(); // compiler should warn this as invalid recursive method
}
}

const testing:Testing = new Testing();
console.log(testing.test1());// compiler should warn this as invalid recursive method call

//-----------------------------------------------------------------------------------------

// Correct Recursions should work fine and should not be reported as error by compiler

// Examples :
function factorial(x:number):number {

// if number is 0
if (x == 0) {
return 1;
}

// if number is positive
else {
return x * factorial(x - 1);
}
}

console.log(factorial(5));

📃 Motivating Example

// Invalid Recursions should not compile and should be reported as error during compile time

// Examples :
function testFunction(i:number) {
return testFunction(i); // compiler should warn this as invalid recursive function
}

let testFunction2 = function (i:number) {
return testFunction2(i); // compiler should warn this as invalid recursive function
}

let testFunction3 :(i:number)=>number = (i:number)=>{
return testFunction3(i); // compiler should warn this as invalid recursive function
}

testFunction(1); // compiler should warn this as invalid recursive function call
testFunction2(1); // compiler should warn this as invalid recursive function call
testFunction3(1); // compiler should warn this as invalid recursive function call

class Testing {
id:number=0;

test1():number{
return this.test1(); // compiler should warn this as invalid recursive method
}
}

const testing:Testing = new Testing();
console.log(testing.test1());// compiler should warn this as invalid recursive method call

//-----------------------------------------------------------------------------------------

// Correct Recursions should work fine and should not be reported as error by compiler

// Examples :
function factorial(x:number):number {

// if number is 0
if (x == 0) {
return 1;
}

// if number is positive
else {
return x * factorial(x - 1);
}
}

console.log(factorial(5));

💻 Use Cases

// Invalid Recursions should not compile and should be reported as error during compile time

// Examples :
function testFunction(i:number) {
return testFunction(i); // compiler should warn this as invalid recursive function
}

let testFunction2 = function (i:number) {
return testFunction2(i); // compiler should warn this as invalid recursive function
}

let testFunction3 :(i:number)=>number = (i:number)=>{
return testFunction3(i); // compiler should warn this as invalid recursive function
}

testFunction(1); // compiler should warn this as invalid recursive function call
testFunction2(1); // compiler should warn this as invalid recursive function call
testFunction3(1); // compiler should warn this as invalid recursive function call

class Testing {
id:number=0;

test1():number{
return this.test1(); // compiler should warn this as invalid recursive method
}
}

const testing:Testing = new Testing();
console.log(testing.test1());// compiler should warn this as invalid recursive method call

//-----------------------------------------------------------------------------------------

// Correct Recursions should work fine and should not be reported as error by compiler

// Examples :
function factorial(x:number):number {

// if number is 0
if (x == 0) {
return 1;
}

// if number is positive
else {
return x * factorial(x - 1);
}
}

console.log(factorial(5));

  1. What do you want to use this for?
    Java programmers can detect invalid recursive methods and functions in Intellij.
    Swift programmers can detect invalid recursive methods and functions in Xcode.
    But typescript programmers cannot detect invalid recursive methods and functions in VsCode or any other code editor.
    Invalid recursion check should exist in typescript as well which will allow to catch bugs during compile time .

  2. What shortcomings exist with current approaches?
    Java programmers can detect invalid recursive methods and functions in Intellij.
    Swift programmers can detect invalid recursive methods and functions in Xcode.
    But typescript programmers cannot detect invalid recursive methods and functions in VsCode or any other code editor.
    Invalid recursion check should exist in typescript as well which will allow to catch bugs during compile time .

  3. What workarounds are you using in the meantime?
    No workarounds at the moment for detecting typescript invalid recursive methods or functions during compile time .
    As a result runtime error happens for invalid recursive methods or functions in typescript.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

L’issue ne nomme aucun fichier, test ni point d’entrée du compilateur. Commencez par définir ce qui constitue une récursion invalide au-delà des exemples d’appels à soi-même, puis déterminez comment les diagnostics peuvent rejeter ces cas tout en autorisant l’exemple de factorielle ; la finalisation nécessiterait un comportement convenu et des tests du compilateur.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
28/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.