File tree Expand file tree Collapse file tree 4 files changed +209
-2
lines changed
internal/plugins/typescript/rules/no_misused_new
packages/rslint-test-tools/tests/typescript-eslint/rules Expand file tree Collapse file tree 4 files changed +209
-2
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package no_misused_new
33import (
44 "github.com/microsoft/typescript-go/shim/ast"
55 "github.com/web-infra-dev/rslint/internal/rule"
6+ "github.com/web-infra-dev/rslint/internal/utils"
67)
78
89/**
@@ -34,7 +35,8 @@ var NoMisusedNewRule = rule.CreateRule(rule.Rule{
3435 return
3536 }
3637
37- if node .Name ().Text () != "new" {
38+ nodeNameText , _ := utils .GetNameFromMember (ctx .SourceFile , node .Name ())
39+ if nodeNameText != "new" {
3840 return
3941 }
4042 // If the function body exists, it's valid for this rule.
@@ -62,7 +64,8 @@ var NoMisusedNewRule = rule.CreateRule(rule.Rule{
6264 }
6365 },
6466 ast .KindMethodSignature : func (node * ast.Node ) {
65- if node .Name ().Text () == "constructor" {
67+ nodeNameText , _ := utils .GetNameFromMember (ctx .SourceFile , node .Name ())
68+ if nodeNameText == "constructor" {
6669 ctx .ReportNode (node , rule.RuleMessage {
6770 Id : "errorMessageInterface" ,
6871 Description : "interfaces cannot be constructed, only classes." ,
Original file line number Diff line number Diff line change @@ -170,5 +170,62 @@ func TestNoMisusedNewRule(t *testing.T) {
170170 },
171171 },
172172 },
173+ {
174+ Code : `
175+ class C {
176+ ['constructor']() {};
177+ }
178+ ` ,
179+ },
180+ {
181+ Code : `
182+ class C {
183+ ['new'](): C;
184+ }
185+ ` ,
186+ Errors : []rule_tester.InvalidTestCaseError {
187+ {
188+ MessageId : "errorMessageClass" ,
189+ },
190+ },
191+ },
192+ {
193+ Code : `
194+ declare abstract class C {
195+ ['constructor']() {};
196+ }
197+ ` ,
198+ },
199+ {
200+ Code : `
201+ declare abstract class C {
202+ ['new'](): C;
203+ }
204+ ` ,
205+ Errors : []rule_tester.InvalidTestCaseError {
206+ {
207+ MessageId : "errorMessageClass" ,
208+ },
209+ },
210+ },
211+ {
212+ Code : `
213+ interface I {
214+ ['new'](): I;
215+ }
216+ ` ,
217+ },
218+ {
219+ Code : `
220+ interface I {
221+ ['constructor'](): '';
222+ }
223+ ` ,
224+ Errors : []rule_tester.InvalidTestCaseError {
225+ {
226+ MessageId : "errorMessageInterface" ,
227+ },
228+ },
229+ },
173230 })
174231}
Original file line number Diff line number Diff line change @@ -195,3 +195,93 @@ interface I {
195195 "ruleCount": 1,
196196}
197197`;
198+
199+ exports[`no-misused-new > invalid 7`] = `
200+ {
201+ " code" : "
202+ class C {
203+ [' new' ](): C ;
204+ }
205+ ",
206+ "diagnostics": [
207+ {
208+ " message" : " Class cannot have method named \` new\` ." ,
209+ " messageId" : " errorMessageClass" ,
210+ " range" : {
211+ " end" : {
212+ " column" : 16 ,
213+ " line" : 3 ,
214+ },
215+ " start" : {
216+ " column" : 3 ,
217+ " line" : 3 ,
218+ },
219+ },
220+ " ruleName" : " @typescript-eslint/no-misused-new" ,
221+ } ,
222+ ],
223+ "errorCount": 1,
224+ "fileCount": 1,
225+ "ruleCount": 1,
226+ }
227+ `;
228+
229+ exports[`no-misused-new > invalid 8`] = `
230+ {
231+ " code" : "
232+ declare abstract class C {
233+ [' new' ](): C ;
234+ }
235+ ",
236+ "diagnostics": [
237+ {
238+ " message" : " Class cannot have method named \` new\` ." ,
239+ " messageId" : " errorMessageClass" ,
240+ " range" : {
241+ " end" : {
242+ " column" : 16 ,
243+ " line" : 3 ,
244+ },
245+ " start" : {
246+ " column" : 3 ,
247+ " line" : 3 ,
248+ },
249+ },
250+ " ruleName" : " @typescript-eslint/no-misused-new" ,
251+ } ,
252+ ],
253+ "errorCount": 1,
254+ "fileCount": 1,
255+ "ruleCount": 1,
256+ }
257+ `;
258+
259+ exports[`no-misused-new > invalid 9`] = `
260+ {
261+ " code" : "
262+ interface I {
263+ [' constructor' ](): ' ' ;
264+ }
265+ ",
266+ "diagnostics": [
267+ {
268+ " message" : " interfaces cannot be constructed, only classes." ,
269+ " messageId" : " errorMessageInterface" ,
270+ " range" : {
271+ " end" : {
272+ " column" : 25 ,
273+ " line" : 3 ,
274+ },
275+ " start" : {
276+ " column" : 3 ,
277+ " line" : 3 ,
278+ },
279+ },
280+ " ruleName" : " @typescript-eslint/no-misused-new" ,
281+ } ,
282+ ],
283+ "errorCount": 1,
284+ "fileCount": 1,
285+ "ruleCount": 1,
286+ }
287+ `;
Original file line number Diff line number Diff line change @@ -70,6 +70,21 @@ interface foo {
7070 `
7171interface foo {
7272 new <T>(): 'x';
73+ }
74+ ` ,
75+ `
76+ class C {
77+ ['constructor']() {};
78+ }
79+ ` ,
80+ `
81+ declare abstract class C {
82+ ['constructor']() {};
83+ }
84+ ` ,
85+ `
86+ interface I {
87+ ['new'](): I;
7388}
7489 ` ,
7590 ] ,
@@ -156,6 +171,48 @@ declare abstract class C {
156171 code : `
157172interface I {
158173 constructor(): '';
174+ }
175+ ` ,
176+ errors : [
177+ {
178+ column : 3 ,
179+ line : 3 ,
180+ messageId : 'errorMessageInterface' ,
181+ } ,
182+ ] ,
183+ } ,
184+ {
185+ code : `
186+ class C {
187+ ['new'](): C;
188+ }
189+ ` ,
190+ errors : [
191+ {
192+ column : 3 ,
193+ line : 3 ,
194+ messageId : 'errorMessageClass' ,
195+ } ,
196+ ] ,
197+ } ,
198+ {
199+ code : `
200+ declare abstract class C {
201+ ['new'](): C;
202+ }
203+ ` ,
204+ errors : [
205+ {
206+ column : 3 ,
207+ line : 3 ,
208+ messageId : 'errorMessageClass' ,
209+ } ,
210+ ] ,
211+ } ,
212+ {
213+ code : `
214+ interface I {
215+ ['constructor'](): '';
159216}
160217 ` ,
161218 errors : [
You can’t perform that action at this time.
0 commit comments