Skip to content

Commit 1e49dd1

Browse files
amv146Alex ValloneCopilot
authored
Allow defined tables without exact rows (#415)
* Allow defined tables without exact rows Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 946bd35e-0c84-49f0-90ff-a15d522d34f8 * Make defined table rows optional Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 946bd35e-0c84-49f0-90ff-a15d522d34f8 * Assign optional table rows directly Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 946bd35e-0c84-49f0-90ff-a15d522d34f8 * Bump parser version to 2.0.0 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 946bd35e-0c84-49f0-90ff-a15d522d34f8 --------- Co-authored-by: Alex Vallone <alexvallone@microsoft.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 946bd35e-0c84-49f0-90ff-a15d522d34f8
1 parent baef833 commit 1e49dd1

7 files changed

Lines changed: 34 additions & 11 deletions

File tree

‎package-lock.json‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@microsoft/powerquery-parser",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "A parser for the Power Query/M formula language.",
55
"author": "Microsoft",
66
"license": "MIT",

‎src/powerquery-parser/language/type/type.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ export type DefinedTable = IExtendedType &
307307
FieldSpecificationList<OrderedFields> & {
308308
readonly kind: TypeKind.Table;
309309
readonly extendedKind: ExtendedTypeKind.DefinedTable;
310-
readonly rows: ReadonlyArray<UnorderedFields>;
310+
readonly rows?: ReadonlyArray<UnorderedFields>;
311311
};
312312

313313
export type FunctionType = IExtendedType &

‎src/powerquery-parser/language/type/typeUtils/factories.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,20 @@ export function definedRecord(
9797
}
9898

9999
/**
100-
* Creates a defined table with exact rows.
100+
* Creates a defined table with known fields and optional exact rows.
101101
*
102-
* Each row is asserted to contain exactly the declared fields and field types compatible with the table definition.
102+
* Omitted rows indicate that the table's rows are unknown, while an empty array represents an exact empty table.
103+
* Each supplied row is asserted to contain exactly the declared fields and field types compatible with the table definition.
103104
* @throws CommonError.InvariantError if a row does not satisfy those requirements.
104105
*/
105106
export function definedTable(
106107
isNullable: boolean,
107108
fields: Type.OrderedFields,
108-
rows: ReadonlyArray<Type.UnorderedFields>,
109+
rows?: ReadonlyArray<Type.UnorderedFields>,
109110
): Type.DefinedTable {
110111
const fieldNames: ReadonlyArray<string> = [...fields.keys()];
111112

112-
for (const [rowIndex, row] of rows.entries()) {
113+
for (const [rowIndex, row] of rows?.entries() ?? []) {
113114
Assert.isTrue(
114115
MapUtils.hasKeys(row, fieldNames) && row.size === fields.size,
115116
`row fields do not match table fields`,

‎src/powerquery-parser/language/type/typeUtils/isEqualType.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@ export function isEqualDefinedTable(left: Type.DefinedTable, right: Type.Defined
183183
}
184184

185185
function isEqualDefinedTableRows(
186-
left: ReadonlyArray<Type.UnorderedFields>,
187-
right: ReadonlyArray<Type.UnorderedFields>,
186+
left: ReadonlyArray<Type.UnorderedFields> | undefined,
187+
right: ReadonlyArray<Type.UnorderedFields> | undefined,
188188
): boolean {
189189
if (left === right) {
190190
return true;
191-
} else if (left.length !== right.length) {
191+
} else if (left === undefined || right === undefined || left.length !== right.length) {
192192
return false;
193193
}
194194

‎src/test/libraryTest/language/typeUtils/isEqualType.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,21 @@ describe(`TypeUtils.isEqualType`, () => {
9999
expected: false,
100100
});
101101
});
102+
103+
it(`unknown rows equal unknown rows`, () => {
104+
runTest({
105+
left: TypeUtils.definedTable(false, fields),
106+
right: TypeUtils.definedTable(false, fields),
107+
expected: true,
108+
});
109+
});
110+
111+
it(`unknown rows differ from exact empty rows`, () => {
112+
runTest({
113+
left: TypeUtils.definedTable(false, fields),
114+
right: TypeUtils.definedTable(false, fields, []),
115+
expected: false,
116+
});
117+
});
102118
});
103119
});

‎src/test/libraryTest/language/typeUtils/typeUtils.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ describe(`TypeUtils`, () => {
247247
expect(type.rows).to.equal(rows);
248248
});
249249

250+
it(`allows unknown rows`, () => {
251+
const type: Type.DefinedTable = TypeUtils.definedTable(false, fields);
252+
253+
expect(type.rows).to.be.undefined;
254+
});
255+
250256
it(`accepts null for a nullable column`, () => {
251257
const nullableFields: Type.OrderedFields = new OrderedMap([["Value", Type.NullableNumberInstance]]);
252258

0 commit comments

Comments
 (0)