🤖 Merge PR #65868 [mvdan-sh] Add typings and tests for Parser's interactive methods by @Rossil2012

* Add missing typings for class Parser

* Add tests for Parser's interactive methods
This commit is contained in:
Rossil
2023-06-25 16:08:59 +08:00
committed by GitHub
parent fb01caea76
commit e9c922415b
2 changed files with 33 additions and 0 deletions

View File

@@ -3,6 +3,8 @@
// Definitions by: JounQin <https://github.com/JounQin>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
// eslint-disable-next-line no-const-enum
declare const enum LangVariant {
LangBash = 0,
@@ -70,6 +72,9 @@ declare namespace sh {
interface Parser {
Parse(text: string, path?: string): File;
Interactive(r: { read: (size?: number) => string | Buffer | null }, fn: (stmts: Stmt[]) => boolean): void;
InteractiveStep(line: string): Stmt[];
Incomplete(): boolean;
}
interface Printer {

View File

@@ -27,3 +27,31 @@ syntax
syntax.FunctionNextLine(false),
)
.Print(node);
// Test the methods of Parser.
// The following tests are ported from https://github.com/mvdan/sh/blob/master/_js/testmain.js
const parser = syntax.NewParser();
const lines = [
"foo\n",
"bar; baz\n"
];
// Test parser.InteractiveStep
for (const line of lines) {
const stmts = parser.InteractiveStep(line); // $ExpectType Stmt[]
}
const src = {
read: (size?: number): string | null => {
if (lines.length === 0) {
return null;
}
return lines.shift()!;
}
};
// Test parser.Interactive && parser.Incomplete
parser.Interactive(src, (stmts: sh.Stmt[]) => {
const incomplete = parser.Incomplete(); // $ExpectType boolean
return true;
});