Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@cadl-lang/compiler",
"comment": "Fix: `scalar` can be used inside of namespace block",
"type": "patch"
}

],
"packageName": "@cadl-lang/compiler"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"changes": [
{
"packageName": "@cadl-lang/migrate",
"comment": "Fix missing file env header",
"type": "patch"
},
{
"packageName": "@cadl-lang/migrate",
"comment": "Fix issue with end of file getting dropped.",
"type": "patch"
}
],
"packageName": "@cadl-lang/migrate"
}
3 changes: 3 additions & 0 deletions packages/compiler/core/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,9 @@ function createParser(code: string | SourceFile, options: ParseOptions = {}): Pa
case Token.ModelKeyword:
item = parseModelStatement(pos, decorators);
break;
case Token.ScalarKeyword:
item = parseScalarStatement(pos, decorators);
break;
case Token.NamespaceKeyword:
const ns = parseNamespaceStatement(pos, decorators);

Expand Down
17 changes: 17 additions & 0 deletions packages/compiler/test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ describe("compiler: parser", () => {
["model bar<a, b> = a | b;", [/'{' expected/]],
]);
});

describe("scalar statements", () => {
parseEach([
"scalar uuid extends string;",
`@foo()
scalar uuid extends string;`,
`namespace Foo {
scalar uuid extends string;}
`,
]);

parseErrorEach([
["scalar uuid extends string { }", [/Statement expected./]],
["scalar uuid is string;", [/Statement expected./]],
]);
});

describe("interface statements", () => {
parseEach([
"interface Foo { }",
Expand Down
2 changes: 2 additions & 0 deletions packages/migrate/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

/* eslint-disable no-console */
import { migrateCadlFiles } from "./migrate.js";
import { migrateModelToScalar } from "./migrations/v0.38/model-to-scalars.js";
Expand Down
2 changes: 2 additions & 0 deletions packages/migrate/src/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ function migrateCadlContentInternal(
segments.push(action.content);
last = action.target.end;
}
segments.push(content.slice(last, -1));

const newContent = segments.join("");

try {
Expand Down
16 changes: 14 additions & 2 deletions packages/migrate/src/migrations/v0.38/model-to-scalars.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { CadlScriptNode, TemplateParameterDeclarationNode } from "@cadl-lang/compiler-v0.37";
import type {
CadlScriptNode,
Node,
TemplateParameterDeclarationNode,
} from "@cadl-lang/compiler-v0.37";
import {
CadlCompilerV0_37,
createMigration,
Expand All @@ -24,7 +28,7 @@ export const migrateModelToScalar = createMigration({
}

const actions: MigrateAction[] = [];
compilerV37.visitChildren(root, (node) => {
visitRecursive(compilerV37, root, (node) => {
if (
node.kind === compilerV37.SyntaxKind.ModelStatement &&
node.is &&
Expand All @@ -47,6 +51,14 @@ export const migrateModelToScalar = createMigration({
},
});

function visitRecursive(compiler: any, root: Node, callback: (node: Node) => void) {
const visit = (node: Node) => {
callback(node);
compiler.visitChildren(node, visit);
};
visit(root);
}

const builtInTypes = new Set([
"bytes",
"numeric",
Expand Down
42 changes: 41 additions & 1 deletion packages/migrate/test/model-to-scalars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { migrateCadlContent } from "../src/migrate.js";
import { migrateModelToScalar } from "../src/migrations/v0.38/model-to-scalars.js";

describe("migration: model to scalars", () => {
it("convert", async () => {
it("various models", async () => {
const [result] = await migrateCadlContent(
`
model foo is string;
Expand Down Expand Up @@ -35,4 +35,44 @@ scalar Resource<T> extends int32;
`.trim()
);
});

it("inside namespace", async () => {
const [result] = await migrateCadlContent(
`
namespace MyService {
model foo is string;
}
`,
migrateModelToScalar
);

strictEqual(
result.trim(),
`
namespace MyService {
scalar foo extends string;
}
`.trim()
);
});

it("with operations", async () => {
const [result] = await migrateCadlContent(
`
model foo is string;

op test(): string;
`,
migrateModelToScalar
);

strictEqual(
result.trim(),
`
scalar foo extends string;

op test(): string;
`.trim()
);
});
});