Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split intrinsic from std lib #3170

Merged
merged 18 commits into from
Apr 18, 2024
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
8 changes: 8 additions & 0 deletions .chronus/changes/actual-std-lib-2024-3-15-15-53-0.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: fix
packages:
- "@typespec/compiler"
---

`--nostdlib` flag will now work by only applying to optional standard library types
8 changes: 8 additions & 0 deletions .chronus/changes/actual-std-lib-2024-3-15-15-55-33.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: fix
packages:
- "@typespec/playground"
---

Add support for new intrinsic vs std lib split in the compiler.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import "../dist/src/lib/intrinsic-decorators.js";

// This file contains all the intrinsic types of typespec. Everything here will always be loaded
namespace TypeSpec;

/**
Expand Down Expand Up @@ -122,20 +125,6 @@ scalar duration;
*/
scalar boolean;

/**
* Represent a 32-bit unix timestamp datetime with 1s of granularity.
* It measures time by the number of seconds that have elapsed since 00:00:00 UTC on 1 January 1970.
*/
@encode("unixTimestamp", int32)
scalar unixTimestamp32 extends utcDateTime;

/**
* Represent a model
*/
// Deprecated June 2023 sprint
#deprecated "object is deprecated. Please use {} for an empty model, `Record<unknown>` for a record with unknown property types, `unknown[]` for an array."
model object {}

/**
* @dev Array model type, equivalent to `Element[]`
* @template Element The type of the array elements
Expand All @@ -149,64 +138,3 @@ model Array<Element> {}
*/
@indexer(string, Element)
model Record<Element> {}

/**
* Represent a URL string as described by https://url.spec.whatwg.org/
*/
scalar url extends string;

/**
* Represents a collection of optional properties.
*
* @template Source An object whose spread properties are all optional.
*/
@doc("The template for adding optional properties.")
@withOptionalProperties
model OptionalProperties<Source> {
...Source;
}

/**
* Represents a collection of updateable properties.
*
* @template Source An object whose spread properties are all updateable.
*/
@doc("The template for adding updateable properties.")
@withUpdateableProperties
model UpdateableProperties<Source> {
...Source;
}

/**
* Represents a collection of omitted properties.
*
* @template Source An object whose properties are spread.
* @template Keys The property keys to omit.
*/
@doc("The template for omitting properties.")
@withoutOmittedProperties(Keys)
model OmitProperties<Source, Keys extends string> {
...Source;
}

/**
* Represents a collection of properties with default values omitted.
*
* @template Source An object whose spread property defaults are all omitted.
*/
@withoutDefaultValues
model OmitDefaults<Source> {
...Source;
}

/**
* Applies a visibility setting to a collection of properties.
*
* @template Source An object whose properties are spread.
* @template Visibility The visibility to apply to all properties.
*/
@doc("The template for setting the default visibility of key properties.")
@withDefaultKeyVisibility(Visibility)
model DefaultKeyVisibility<Source, Visibility extends valueof string> {
...Source;
}
4 changes: 0 additions & 4 deletions packages/compiler/lib/main.tsp

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "../dist/src/lib/decorators.js";
import "../../dist/src/lib/decorators.js";

using TypeSpec.Reflection;

Expand Down
5 changes: 5 additions & 0 deletions packages/compiler/lib/std/main.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// TypeSpec standard library. Everything in here can be omitted by using `--nostdlib` cli flag or `nostdlib` in the config.
import "./types.tsp";
import "./decorators.tsp";
import "./reflection.tsp";
import "./projected-names.tsp";
76 changes: 76 additions & 0 deletions packages/compiler/lib/std/types.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace TypeSpec;

/**
* Represent a 32-bit unix timestamp datetime with 1s of granularity.
* It measures time by the number of seconds that have elapsed since 00:00:00 UTC on 1 January 1970.
*/
@encode("unixTimestamp", int32)
scalar unixTimestamp32 extends utcDateTime;

/**
* Represent a model
*/
// Deprecated June 2023 sprint
#deprecated "object is deprecated. Please use {} for an empty model, `Record<unknown>` for a record with unknown property types, `unknown[]` for an array."
model object {}

/**
* Represent a URL string as described by https://url.spec.whatwg.org/
*/
scalar url extends string;

/**
* Represents a collection of optional properties.
*
* @template Source An object whose spread properties are all optional.
*/
@doc("The template for adding optional properties.")
@withOptionalProperties
model OptionalProperties<Source> {
...Source;
}

/**
* Represents a collection of updateable properties.
*
* @template Source An object whose spread properties are all updateable.
*/
@doc("The template for adding updateable properties.")
@withUpdateableProperties
model UpdateableProperties<Source> {
...Source;
}

/**
* Represents a collection of omitted properties.
*
* @template Source An object whose properties are spread.
* @template Keys The property keys to omit.
*/
@doc("The template for omitting properties.")
@withoutOmittedProperties(Keys)
model OmitProperties<Source, Keys extends string> {
...Source;
}

/**
* Represents a collection of properties with default values omitted.
*
* @template Source An object whose spread property defaults are all omitted.
*/
@withoutDefaultValues
model OmitDefaults<Source> {
...Source;
}

/**
* Applies a visibility setting to a collection of properties.
*
* @template Source An object whose properties are spread.
* @template Visibility The visibility to apply to all properties.
*/
@doc("The template for setting the default visibility of key properties.")
@withDefaultKeyVisibility(Visibility)
model DefaultKeyVisibility<Source, Visibility extends valueof string> {
...Source;
}
4 changes: 2 additions & 2 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"type": "module",
"main": "dist/src/index.js",
"tspMain": "lib/main.tsp",
"tspMain": "lib/std/main.tsp",
"exports": {
".": {
"types": "./dist/src/index.d.ts",
Expand Down Expand Up @@ -54,7 +54,7 @@
"tsp-server": "cmd/tsp-server.js"
},
"files": [
"lib/*.tsp",
"lib/**/*.tsp",
"dist/**",
"templates/**",
"entrypoints",
Expand Down
6 changes: 2 additions & 4 deletions packages/compiler/src/core/checker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { $docFromComment, getIndexer, isArrayModelType } from "../lib/decorators.js";
import { $docFromComment, isArrayModelType } from "../lib/decorators.js";
import { getIndexer } from "../lib/intrinsic-decorators.js";
import { MultiKeyMap, Mutable, createRekeyableMap, isArray, mutate } from "../utils/misc.js";
import { createSymbol, createSymbolTable } from "./binder.js";
import { createChangeIdentifierCodeFix } from "./compiler-code-fixes/change-identifier.codefix.js";
Expand Down Expand Up @@ -356,9 +357,6 @@ export function createChecker(program: Program): Checker {

const typespecNamespaceBinding = globalNamespaceNode.symbol.exports!.get("TypeSpec");
if (typespecNamespaceBinding) {
// the TypeSpec namespace binding will be absent if we've passed
// the no-std-lib option.
// the first declaration here is the JS file for the TypeSpec script.
initializeTypeSpecIntrinsics();
for (const file of program.sourceFiles.values()) {
addUsingSymbols(typespecNamespaceBinding.exports!, file.locals);
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/core/node-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const NodeHost: CompilerHost = {
getJsImport: (path: string) => import(pathToFileURL(path).href),
getLibDirs() {
const rootDir = this.getExecutionRoot();
return [joinPaths(rootDir, "lib")];
return [joinPaths(rootDir, "lib/std")];
},
stat(path: string) {
return stat(path);
Expand Down
16 changes: 13 additions & 3 deletions packages/compiler/src/core/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
} from "./module-resolver.js";
import { CompilerOptions } from "./options.js";
import { isImportStatement, parse, parseStandaloneTypeReference } from "./parser.js";
import { getDirectoryPath, joinPaths } from "./path-utils.js";
import { getDirectoryPath, joinPaths, resolvePath } from "./path-utils.js";
import { createProjector } from "./projector.js";
import { createSourceFile } from "./source-file.js";
import {
Expand Down Expand Up @@ -332,8 +332,9 @@ export async function compile(
}
const binder = createBinder(program);

await loadIntrinsicTypes();
if (!options?.nostdlib) {
await loadStandardLibrary(program);
await loadStandardLibrary();
}

// Load additional imports prior to compilation
Expand Down Expand Up @@ -458,7 +459,16 @@ export async function compile(
}
}

async function loadStandardLibrary(program: Program) {
async function loadIntrinsicTypes() {
const locationContext: LocationContext = { type: "compiler" };
await loadTypeSpecFile(
resolvePath(host.getExecutionRoot(), "lib/intrinsics.tsp"),
locationContext,
NoTarget
);
}

async function loadStandardLibrary() {
const locationContext: LocationContext = { type: "compiler" };
for (const dir of host.getLibDirs()) {
await loadDirectory(dir, locationContext, NoTarget);
Expand Down
11 changes: 0 additions & 11 deletions packages/compiler/src/lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import {
EnumMember,
Interface,
Model,
ModelIndexer,
ModelProperty,
Namespace,
Operation,
Expand Down Expand Up @@ -287,16 +286,6 @@ export const $inspectTypeName: InspectTypeNameDecorator = (context, target: Type
console.log(getTypeName(target));
};

const indexTypeKey = createStateSymbol("index");
export const $indexer = (context: DecoratorContext, target: Type, key: Scalar, value: Type) => {
const indexer: ModelIndexer = { key, value };
context.program.stateMap(indexTypeKey).set(target, indexer);
};

export function getIndexer(program: Program, target: Type): ModelIndexer | undefined {
return program.stateMap(indexTypeKey).get(target);
}

export function isStringType(program: Program | ProjectedProgram, target: Type): target is Scalar {
const coreType = program.checker.getStdType("string");
const stringType = target.projector ? target.projector.projectType(coreType) : coreType;
Expand Down
14 changes: 14 additions & 0 deletions packages/compiler/src/lib/intrinsic-decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Program } from "../core/program.js";
import { DecoratorContext, ModelIndexer, Scalar, Type } from "../core/types.js";

export const namespace = "TypeSpec";

const indexTypeKey = Symbol.for(`TypeSpec.index`);
export const $indexer = (context: DecoratorContext, target: Type, key: Scalar, value: Type) => {
const indexer: ModelIndexer = { key, value };
context.program.stateMap(indexTypeKey).set(target, indexer);
};

export function getIndexer(program: Program, target: Type): ModelIndexer | undefined {
return program.stateMap(indexTypeKey).get(target);
}
4 changes: 2 additions & 2 deletions packages/compiler/src/testing/test-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function createTestCompilerHost(
jsImports: Map<string, Record<string, any>>,
options?: TestHostOptions
): CompilerHost {
const libDirs = [resolveVirtualPath(".tsp/lib")];
const libDirs = [resolveVirtualPath(".tsp/lib/std")];
if (!options?.excludeTestLib) {
libDirs.push(resolveVirtualPath(".tsp/test-lib"));
}
Expand Down Expand Up @@ -224,7 +224,7 @@ export const StandardTestLibrary: TypeSpecTestLibrary = {
packageRoot: await findTestPackageRoot(import.meta.url),
files: [
{ virtualPath: "./.tsp/dist/src/lib", realDir: "./dist/src/lib", pattern: "*" },
{ virtualPath: "./.tsp/lib", realDir: "./lib", pattern: "*" },
{ virtualPath: "./.tsp/lib", realDir: "./lib", pattern: "**" },
],
};

Expand Down
10 changes: 9 additions & 1 deletion packages/compiler/test/semantic-walker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,15 @@ describe("compiler: semantic walker", () => {

deepStrictEqual(
result.namespaces.map((x) => getNamespaceFullName(x)),
["", "Global", "Global.My", "Global.My.Simple", "Global.My.Parent", "Global.My.Parent.Child"]
[
"",
"TypeSpec",
"Global",
"Global.My",
"Global.My.Simple",
"Global.My.Parent",
"Global.My.Parent.Child",
]
);
});

Expand Down
Loading
Loading