From 2bb3f0fffc9a57cde269d7a131284aa87a119b69 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Thu, 8 Dec 2022 12:17:57 -0800 Subject: [PATCH 1/3] Fix scalar migration --- packages/compiler/core/parser.ts | 3 ++ packages/compiler/test/parser.test.ts | 17 ++++++++ packages/migrate/src/cli.ts | 2 + packages/migrate/src/migrate.ts | 2 + .../src/migrations/v0.38/model-to-scalars.ts | 16 ++++++- .../migrate/test/model-to-scalars.test.ts | 42 ++++++++++++++++++- 6 files changed, 79 insertions(+), 3 deletions(-) diff --git a/packages/compiler/core/parser.ts b/packages/compiler/core/parser.ts index 4bbca6c5321..c98cabcfce2 100644 --- a/packages/compiler/core/parser.ts +++ b/packages/compiler/core/parser.ts @@ -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); diff --git a/packages/compiler/test/parser.test.ts b/packages/compiler/test/parser.test.ts index 198a993f5d0..5b95383ce2e 100644 --- a/packages/compiler/test/parser.test.ts +++ b/packages/compiler/test/parser.test.ts @@ -144,6 +144,23 @@ describe("compiler: parser", () => { ["model bar = 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 { }", diff --git a/packages/migrate/src/cli.ts b/packages/migrate/src/cli.ts index 176f4123cbf..309d9169ea9 100644 --- a/packages/migrate/src/cli.ts +++ b/packages/migrate/src/cli.ts @@ -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"; diff --git a/packages/migrate/src/migrate.ts b/packages/migrate/src/migrate.ts index a025b92cbf7..f419b99730e 100644 --- a/packages/migrate/src/migrate.ts +++ b/packages/migrate/src/migrate.ts @@ -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 { diff --git a/packages/migrate/src/migrations/v0.38/model-to-scalars.ts b/packages/migrate/src/migrations/v0.38/model-to-scalars.ts index 98018490992..8c8c727aa25 100644 --- a/packages/migrate/src/migrations/v0.38/model-to-scalars.ts +++ b/packages/migrate/src/migrations/v0.38/model-to-scalars.ts @@ -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, @@ -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 && @@ -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", diff --git a/packages/migrate/test/model-to-scalars.test.ts b/packages/migrate/test/model-to-scalars.test.ts index 0d64d43e39b..88186ce65b2 100644 --- a/packages/migrate/test/model-to-scalars.test.ts +++ b/packages/migrate/test/model-to-scalars.test.ts @@ -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; @@ -35,4 +35,44 @@ scalar Resource 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() + ); + }); }); From 88b4a1b7fce65bc217943b985e801b14b432d81a Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Thu, 8 Dec 2022 12:19:53 -0800 Subject: [PATCH 2/3] changelog --- .../fix-scalar-migration_2022-12-08-20-18.json | 11 +++++++++++ .../fix-scalar-migration_2022-12-08-20-18.json | 15 +++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 common/changes/@cadl-lang/compiler/fix-scalar-migration_2022-12-08-20-18.json create mode 100644 common/changes/@cadl-lang/migrate/fix-scalar-migration_2022-12-08-20-18.json diff --git a/common/changes/@cadl-lang/compiler/fix-scalar-migration_2022-12-08-20-18.json b/common/changes/@cadl-lang/compiler/fix-scalar-migration_2022-12-08-20-18.json new file mode 100644 index 00000000000..e786a831f93 --- /dev/null +++ b/common/changes/@cadl-lang/compiler/fix-scalar-migration_2022-12-08-20-18.json @@ -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" +} diff --git a/common/changes/@cadl-lang/migrate/fix-scalar-migration_2022-12-08-20-18.json b/common/changes/@cadl-lang/migrate/fix-scalar-migration_2022-12-08-20-18.json new file mode 100644 index 00000000000..4bb9e207049 --- /dev/null +++ b/common/changes/@cadl-lang/migrate/fix-scalar-migration_2022-12-08-20-18.json @@ -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" +} From 51945f436dc5eb41bc5800386d6b4ac519708540 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Thu, 8 Dec 2022 12:48:54 -0800 Subject: [PATCH 3/3] fix whitespace issue in test --- packages/migrate/test/model-to-scalars.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/migrate/test/model-to-scalars.test.ts b/packages/migrate/test/model-to-scalars.test.ts index 88186ce65b2..8dbe8d1f2a0 100644 --- a/packages/migrate/test/model-to-scalars.test.ts +++ b/packages/migrate/test/model-to-scalars.test.ts @@ -49,7 +49,7 @@ namespace MyService { strictEqual( result.trim(), ` -namespace MyService { +namespace MyService { scalar foo extends string; } `.trim()