Skip to content

Commit

Permalink
Symbol refactor (#4849)
Browse files Browse the repository at this point in the history
### Augment decorator issues
Fix #4749 Augment/ref alias
properties
Fix #2867 Augment/ref `model
is` properties
Fix #4818 Augment/ref nested
model properties

### Circular reference issues
Fix #4908 freeze when using
circular model extends with aliases

### Others
FIx #4915 (Duplicate usings
not scoped per namespace)

Doesn't resolve the alias circular issue
#2824 but I think setup the
solution for fixing it later in the name resolver where we could detect
that cycle instead of the checker

---------

Co-authored-by: Brian Terlson <[email protected]>
  • Loading branch information
timotheeguerin and bterlson authored Nov 4, 2024
1 parent 49c0527 commit b39abe5
Show file tree
Hide file tree
Showing 47 changed files with 4,604 additions and 1,592 deletions.
8 changes: 8 additions & 0 deletions .chronus/changes/symbol-refactor-2-2024-9-29-20-20-29.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@typespec/compiler"
---

Overall of the symbol resolution. TypeSpec is able to resolve anything that can be statically linked. Augment decorators in turn are able to target any statically linkable types.
7 changes: 7 additions & 0 deletions .chronus/changes/symbol-refactor-2-2024-9-29-20-20-30.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: internal
packages:
- "@typespec/openapi3"
---
Fix test host
1 change: 1 addition & 0 deletions cspell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ words:
- Jacoco
- jdwp
- jobject
- Johan
- jsyaml
- keyer
- killpg
Expand Down
30 changes: 28 additions & 2 deletions packages/compiler/.scripts/gen-extern-signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
import { format, resolveConfig } from "prettier";
import { fileURLToPath } from "url";
import { generateExternDecorators } from "../../tspd/dist/src/gen-extern-signatures/gen-extern-signatures.js";
import { NodeHost, compile, resolvePath } from "../dist/src/index.js";
import {
Namespace,
NodeHost,
compile,
formatDiagnostic,
logDiagnostics,
resolvePath,
} from "../dist/src/index.js";

const root = fileURLToPath(new URL("..", import.meta.url).href);
const outDir = resolvePath(root, "generated-defs");
Expand All @@ -12,8 +19,27 @@ try {
await NodeHost.mkdirp(outDir);

const program = await compile(NodeHost, root, {});
logDiagnostics(program.diagnostics, NodeHost.logSink);
if (program.hasError()) {
console.log("Has error not continuing");
process.exit(1);
}

const resolved = [
program.resolveTypeReference("TypeSpec"),
program.resolveTypeReference("TypeSpec.Prototypes"),
];
const namespaces: Namespace[] = [];
for (const [namespace, diagnostics] of resolved) {
if (namespace === undefined) {
throw new Error(`Cannot resolve namespace: \n${diagnostics.map(formatDiagnostic).join("\n")}`);
} else if (namespace.kind !== "Namespace") {
throw new Error(`Expected namespace but got ${namespace.kind}`);
}
namespaces.push(namespace);
}

const files = await generateExternDecorators(program, "@typespec/compiler");
const files = await generateExternDecorators(program, "@typespec/compiler", { namespaces });
for (const [name, content] of Object.entries(files)) {
const updatedContent = content.replace(/from "\@typespec\/compiler"/g, `from "../src/index.js"`);
const prettierConfig = await resolveConfig(root);
Expand Down
7 changes: 7 additions & 0 deletions packages/compiler/generated-defs/TypeSpec.Prototypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { DecoratorContext, Type } from "../src/index.js";

export type GetterDecorator = (context: DecoratorContext, target: Type) => void;

export type TypeSpecPrototypesDecorators = {
getter: GetterDecorator;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** An error here would mean that the decorator is not exported or doesn't have the right name. */
import { $decorators } from "../src/index.js";
import type { TypeSpecPrototypesDecorators } from "./TypeSpec.Prototypes.js";
/** An error here would mean that the exported decorator is not using the same signature. Make sure to have export const $decName: DecNameDecorator = (...) => ... */
const _: TypeSpecPrototypesDecorators = $decorators["TypeSpec.Prototypes"];
3 changes: 2 additions & 1 deletion packages/compiler/lib/intrinsics.tsp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "../dist/src/lib/intrinsic-decorators.js";
import "../dist/src/lib/intrinsic/tsp-index.js";
import "./prototypes.tsp";

// This file contains all the intrinsic types of typespec. Everything here will always be loaded
namespace TypeSpec;
Expand Down
18 changes: 18 additions & 0 deletions packages/compiler/lib/prototypes.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace TypeSpec.Prototypes;

extern dec getter(target: unknown);

namespace Types {
interface ModelProperty {
@getter type(): unknown;
}

interface Operation {
@getter returnType(): unknown;
@getter parameters(): unknown;
}

interface Array<TElementType> {
@getter elementType(): TElementType;
}
}
5 changes: 2 additions & 3 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@
],
"scripts": {
"clean": "rimraf ./dist ./temp",
"build:init-templates-index": "node --no-warnings --experimental-specifier-resolution=node --loader ts-node/esm ./.scripts/build-init-templates.ts",
"build:init-templates-index": "tsx ./.scripts/build-init-templates.ts",
"build": "npm run gen-manifest && npm run build:init-templates-index && npm run compile && npm run generate-tmlanguage",
"compile": "tsc -p .",
"watch": "tsc -p . --watch",
"watch-tmlanguage": "node scripts/watch-tmlanguage.js",
"generate-tmlanguage": "node scripts/generate-tmlanguage.js",
"gen-extern-signature": "node --no-warnings --experimental-specifier-resolution=node --loader ts-node/esm ./.scripts/gen-extern-signature.ts",
"gen-extern-signature": "tsx ./.scripts/gen-extern-signature.ts",
"dogfood": "node scripts/dogfood.js",
"test": "vitest run",
"test:ui": "vitest --ui",
Expand Down Expand Up @@ -117,7 +117,6 @@
"rimraf": "~6.0.1",
"source-map-support": "~0.5.21",
"tmlanguage-generator": "workspace:~",
"ts-node": "~10.9.2",
"typescript": "~5.6.3",
"vitest": "^2.1.2",
"vscode-oniguruma": "~2.0.1",
Expand Down
Loading

0 comments on commit b39abe5

Please sign in to comment.