microsoft / microsoft/TypeScript
Invalid recursive methods or functions
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.4k
- Merge trung bình
- 1 ngày 19 giờ
- Pull request đã merge (30 ngày)
- 117
Mô tả
🔍 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));
-
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 . -
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 . -
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.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Issue không nêu rõ tệp, bài kiểm thử hay các điểm vào của trình biên dịch. Hãy bắt đầu bằng việc xác định thế nào được xem là đệ quy không hợp lệ ngoài các ví dụ tự gọi chính nó, sau đó xác định cách các chẩn đoán có thể từ chối những trường hợp đó trong khi vẫn cho phép ví dụ tính giai thừa; để hoàn thành sẽ cần có hành vi được thống nhất và các bài kiểm thử trình biên dịch.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- typescript
- Lĩnh vực
- compilers
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 28/100