Skip to content

Enhancement: optimize the test for function parameter parser #2297

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2095,7 +2095,7 @@ export class Parser extends DiagnosticEmitter {
} else if (flags & CommonFlags.PRIVATE) {
this.error(
DiagnosticCode._0_modifier_cannot_be_used_here,
tn.range(accessStart, accessEnd), "protected"
tn.range(accessStart, accessEnd), "private"
); // recoverable
}
if (flags & CommonFlags.STATIC) {
Expand Down
4 changes: 4 additions & 0 deletions tests/parser/arrow-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ x => x;
// not an array function
(b ? x : y);
(b ? f : g)();

(b ? x | y);
// ERROR 1005: "')' expected."
(x: i32 => x;
1 change: 1 addition & 0 deletions tests/parser/arrow-functions.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
x => x;
(b ? x : y);
(b ? f : g)();
// ERROR 1005: "':' expected." in arrow-functions.ts(13,10+1)
4 changes: 4 additions & 0 deletions tests/parser/call-function-return.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
a = fn()(1, 2);
// ERROR 1042: "'declare' modifier cannot be used here."
declare b = fn()(1, 2);
// ERROR 1005: "')' expected."
a = fn()(1, 2;
4 changes: 4 additions & 0 deletions tests/parser/call-function-return.ts.fixture.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
a = fn()(1, 2);
b = fn()(1, 2);
a = fn();
// ERROR 1042: "'declare' modifier cannot be used here." in call-function-return.ts(3,1+7)
// ERROR 1005: "')' expected." in call-function-return.ts(5,13+1)
2 changes: 2 additions & 0 deletions tests/parser/calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ obj.a(1);

// Everything
id(a)[0](b)(c).a(d)(e)[1](f)(g);
// ERROR 1003: "Identifier expected."
obj.(b ? f : g)();
1 change: 1 addition & 0 deletions tests/parser/calls.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ arr[0](1);
obj.a();
obj.a(1);
id(a)[0](b)(c).a(d)(e)[1](f)(g);
// ERROR 1003: "Identifier expected." in calls.ts(18,5+13)
16 changes: 16 additions & 0 deletions tests/parser/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,20 @@ export class Invalid<T> {
// 1031: 'declare' modifier cannot appear on class elements of this kind.
// 1183: An implementation cannot be declared in ambient contexts.
declare declareMethod(): i32 {}

// ERROR 1042: "'readonly' modifier cannot be used here."
readonly get name():i32 {}
// ERROR 1049: "A 'set' accessor must have exactly one parameter."
// ERROR 1042: "'readonly' modifier cannot be used here."
// ERROR 1095: "A 'set' accessor cannot have a return type annotation."
readonly set name():i32 {}

// ERROR 1042: "'static' modifier cannot be used here."
static constructor(a: i32) {}

// ERROR 1042: "'abstract' modifier cannot be used here."
abstract constructor(a: i32) {}

// ERROR 1042: "'readonly' modifier cannot be used here."
readonly constructor(a: i32) {}
}
12 changes: 12 additions & 0 deletions tests/parser/class.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export class Invalid<T> {
declare declareField: i32;
declare declareInitializer: i32 = 0;
declare declareMethod(): i32 {}
readonly get name(): i32 {}
readonly set name() {}
static constructor(a: i32) {}
constructor(a: i32) {}
readonly constructor(a: i32) {}
}
// ERROR 1092: "Type parameters cannot appear on a constructor declaration." in class.ts(15,14+3)
// ERROR 1110: "Type expected." in class.ts(18,21+0)
Expand All @@ -31,3 +36,10 @@ export class Invalid<T> {
// ERROR 1039: "Initializers are not allowed in ambient contexts." in class.ts(35,35+1)
// ERROR 1031: "'declare' modifier cannot appear on class elements of this kind." in class.ts(39,3+7)
// ERROR 1183: "An implementation cannot be declared in ambient contexts." in class.ts(39,32+1)
// ERROR 1042: "'readonly' modifier cannot be used here." in class.ts(42,3+8)
// ERROR 1042: "'readonly' modifier cannot be used here." in class.ts(46,3+8)
// ERROR 1049: "A 'set' accessor must have exactly one parameter." in class.ts(46,16+4)
// ERROR 1095: "A 'set' accessor cannot have a return type annotation." in class.ts(46,22+1)
// ERROR 1042: "'static' modifier cannot be used here." in class.ts(49,3+6)
// ERROR 1042: "'abstract' modifier cannot be used here." in class.ts(52,3+8)
// ERROR 1042: "'readonly' modifier cannot be used here." in class.ts(55,3+8)
3 changes: 3 additions & 0 deletions tests/parser/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ class MyClass {
constructor() {}
constructor(a: i32) {}
constructor(a: i32, b: i32) {}
constructor(a: i32, b: i32, protected c:f32) {}
//ERROR 1317: "A parameter property cannot be declared using a rest parameter."
constructor(a: i32, b: i32, protected c:f32, protected ...d:i32[]) {}
}

class MyClassImplicit {
Expand Down
3 changes: 3 additions & 0 deletions tests/parser/constructor.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ class MyClass {
constructor() {}
constructor(a: i32) {}
constructor(a: i32, b: i32) {}
constructor(a: i32, b: i32, protected c: f32) {}
constructor(a: i32, b: i32, protected c: f32, protected ...d: Array<i32>) {}
}
class MyClassImplicit {
constructor(public a: i32, private readonly b: i32 = 2, c: i32 = 3) {}
}
// ERROR 1317: "A parameter property cannot be declared using a rest parameter." in constructor.ts(7,58+3)
1 change: 1 addition & 0 deletions tests/parser/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
@external("a", "b")
@custom
function a(): void {}
@operator. // ERROR 1126: "Unexpected end of text."
2 changes: 2 additions & 0 deletions tests/parser/decorators.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
@external("a", "b")
@custom
function a(): void {}
// ERROR 1003: "Identifier expected." in decorators.ts(12,10+1)
// ERROR 1126: "Unexpected end of text." in decorators.ts(12,12+0)
2 changes: 2 additions & 0 deletions tests/parser/do.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ do {
} while (a != b);
do b;
while (a);
// ERROR 1005: "'(' expected."
do{} while;
1 change: 1 addition & 0 deletions tests/parser/do.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ do {
} while (a != b);
do b;
while (a);
// ERROR 1005: "'(' expected." in do.ts(7,6+5)
8 changes: 8 additions & 0 deletions tests/parser/export-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ export default class TheClass {}
export default enum TheEnum {}
export default namespace theNamespace {}
export default something;
// ERROR 1109: "Expression expected."
export import A = B? ;

export import A = :B? ;
// ERROR 1005: "'=' expected."
export import A (= B)? ;
// ERROR 1003: "Identifier expected."
export import: A (= B)? ;
4 changes: 4 additions & 0 deletions tests/parser/export-default.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ export default namespace theNamespace {}
export {
something as default
};
export import A = B;
// ERROR 1109: "Expression expected." in export-default.ts(8,20+1)
// ERROR 1005: "'=' expected." in export-default.ts(12,15+1)
// ERROR 1003: "Identifier expected." in export-default.ts(14,8+6)
4 changes: 4 additions & 0 deletions tests/parser/for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ for (i = 0; i < 10; ++i) {
for (;;) {
;
}
// ERROR 1155: "'const' declarations must be initialized."
for (const i; i < 10; i++) {

}
2 changes: 2 additions & 0 deletions tests/parser/for.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ for (i = 0; i < 10; ++i) {
for (;;) {
;
}
for (const i; i < 10; i++) {}
// ERROR 1155: "'const' declarations must be initialized." in for.ts(11,12+1)
2 changes: 2 additions & 0 deletions tests/parser/forof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ for (foo of bar) ;
for (var foo of bar) ;
for (let foo of bar) ;
for (const foo of bar) ;
// ERROR 1155: "'const' declarations must be initialized."
for (const foo from bar) ;
2 changes: 2 additions & 0 deletions tests/parser/forof.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ for (foo of bar) ;
for (var foo of bar) ;
for (let foo of bar) ;
for (const foo of bar) ;
// ERROR 1155: "'const' declarations must be initialized." in forof.ts(18,12+3)
// ERROR 1005: "';' expected." in forof.ts(18,12+3)
100 changes: 100 additions & 0 deletions tests/parser/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,103 @@ function typeparams<T, V extends T>(a: V | null = null): void {}
function withdecorator(): void {}
function withthis(this: i32): i32 { return this; }
function withthisp(this: i32, a: f32, b: f64): i32 { return this; }

// 1003: Identifier expected.
function typevoid<T extends () => void>():void{}

// 1003: Identifier expected.
function extendsfunctiontype(this: ()=>void):void {}

// 1005: ':' expected.
function extendsinsteadofcolon(this extends i32):void {}

// 1003: Identifier expected.
function typeNone<void>():void{}

// 1005: ')' expected.
function functionforgetcomma(this: ()=>void a: i32):void {}

function trycatchFunction(): void {
try {
}
catch (exception_var) {
}
finally {
}
}
// ERROR 1003: "Identifier expected."
function deeperFunction<void>():void{{++a;}}
// ERROR 1003: "Identifier expected."
function identifierFunction<void>():void{var a = 0;}
// ERROR 1003: "Identifier expected."
function stringFunction<void>():void{"string"}
// ERROR 1005: "'(' expected."
function backtick Function<void>():void{`${a}bc`}
// ERROR 1003: "Identifier expected."
// ERROR 1351: "An identifier or keyword cannot immediately follow a numeric literal."
function deeperFunction<void>():void{1.23f}

function errorTrycatchFunction(): void {
// ERROR 1005: "'(' expected."
try {
}
catch {
}
finally {
}
// ERROR 1003: "Identifier expected."
try {
}
catch (var) {
}
finally {
}
// ERROR 1005: "'{' expected."
try {
}
catch (exception_var)
finally {
}
// ERROR 1005: "'(' expected."
try {
} catch
// ERROR 1005: "'{' expected."
try {
}
catch (exception_var) {}
finally()
// ERROR 1005: "'{' expected."
try(a())
// ERROR 1005: "'catch' expected."
try{a()}
}

function switchError(i: i32): void{

switch(i) {
case constant-expression1: {
break;
}
default: {
break;
}
}
// ERROR 1005: "'(' expected."
switch {i} {

}
// ERROR 1005: "'{' expected."
switch (i) []

}

function ifError(i: i32): void{
// ERROR 1005: "'(' expected."
if {i}

}

function doError(i: i32): void{
// ERROR 1005: "'while' expected."
do {}
}
55 changes: 55 additions & 0 deletions tests/parser/function.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,58 @@ function withthis(this: i32): i32 {
function withthisp(this: i32, a: f32, b: f64): i32 {
return this;
}
function extendsfunctiontype(): void {}
function trycatchFunction(): void {
try {
} catch (exception_var) {
} finally {
}
}
function errorTrycatchFunction(): void {}
function switchError(i: i32): void {
switch (i) {
case constant - expression1:
{
break;
}

default:
{
break;
}

}
}
function ifError(i: i32): void {}
function doError(i: i32): void {}
// ERROR 1003: "Identifier expected." in function.ts(9,29+10)
// ERROR 1003: "Identifier expected." in function.ts(12,36+8)
// ERROR 1005: "':' expected." in function.ts(15,32+4)
// ERROR 1003: "Identifier expected." in function.ts(18,19+4)
// ERROR 1003: "Identifier expected." in function.ts(21,36+8)
// ERROR 1005: "')' expected." in function.ts(21,40+4)
// ERROR 1003: "Identifier expected." in function.ts(32,25+4)
// ERROR 1003: "Identifier expected." in function.ts(34,29+4)
// ERROR 1003: "Identifier expected." in function.ts(36,25+4)
// ERROR 1005: "'(' expected." in function.ts(38,18+0)
// ERROR 1003: "Identifier expected." in function.ts(41,25+4)
// ERROR 1351: "An identifier or keyword cannot immediately follow a numeric literal." in function.ts(41,42+0)
// ERROR 1005: "'(' expected." in function.ts(47,3+5)
// ERROR 1109: "Expression expected." in function.ts(47,3+5)
// ERROR 1109: "Expression expected." in function.ts(49,3+7)
// ERROR 1003: "Identifier expected." in function.ts(54,9+1)
// ERROR 1109: "Expression expected." in function.ts(54,3+5)
// ERROR 1109: "Expression expected." in function.ts(56,3+7)
// ERROR 1005: "'{' expected." in function.ts(61,23+1)
// ERROR 1109: "Expression expected." in function.ts(61,3+5)
// ERROR 1109: "Expression expected." in function.ts(62,3+7)
// ERROR 1005: "'(' expected." in function.ts(66,5+5)
// ERROR 1005: "'{' expected." in function.ts(71,3+7)
// ERROR 1109: "Expression expected." in function.ts(70,3+5)
// ERROR 1109: "Expression expected." in function.ts(71,3+7)
// ERROR 1005: "'{' expected." in function.ts(73,3+3)
// ERROR 1005: "'catch' expected." in function.ts(75,10+1)
// ERROR 1005: "'(' expected." in function.ts(89,3+6)
// ERROR 1005: "'{' expected." in function.ts(93,12+1)
// ERROR 1005: "'(' expected." in function.ts(99,3+2)
// ERROR 1005: "'while' expected." in function.ts(105,7+1)
14 changes: 14 additions & 0 deletions tests/parser/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ import {
} from "./other";
import * as A from "./other";
import "./other";
// ERROR 1005: "'}' expected."
import {A from "./other";
// ERROR 1003: "Identifier expected."
import * as =A from "./other";
// ERROR 1005: "'as' expected."
import * A from "./other";

import A from "./other"
// ERROR 100: "Not implemented: Mixed default and named imports"
import A, B from "./other"

import {A} from B;

import {A} by "./other";
9 changes: 9 additions & 0 deletions tests/parser/import.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ import {
} from "./other";
import * as A from "./other";
import "./other";
import {
default as A
} from "./other";
// ERROR 1005: "'}' expected." in import.ts(18,9+1)
// ERROR 1003: "Identifier expected." in import.ts(20,10+2)
// ERROR 1005: "'as' expected." in import.ts(22,8+1)
// ERROR 100: "Not implemented: Mixed default and named imports" in import.ts(26,9+1)
// ERROR 1141: "String literal expected." in import.ts(28,12+4)
// ERROR 1005: "'from' expected." in import.ts(30,10+1)
Loading