@typespec/library-linter 0.41.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Microsoft Corporation. All rights reserved.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # TypeSpec Library Linter
2
+
3
+ ## Installation
4
+
5
+ Install the package as a dev dependency.
6
+
7
+ ```
8
+ npm install -D @typespec/library-linter
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Compile your library package. Any errors or warnings will be reported as typespec diagnostics.
14
+
15
+ ```bash
16
+ # At the root of your typespec library.
17
+ tsp compile . --import @typespec/library-linter
18
+ ```
19
+
20
+ ## TypeSpec Library Best rules and best practices
21
+
22
+ | Rule name | Description |
23
+ | ------------------- | ---------------------------------------------------------------------------------------------------------------- |
24
+ | `missing-namespace` | Validate that every exported element from the library(Models, JS functions, operations, etc.) is in a namespace. |
@@ -0,0 +1,2 @@
1
+ export * from "./linter.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./linter.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
@@ -0,0 +1,28 @@
1
+ export declare const libDef: {
2
+ readonly name: "@typespec/library-linter";
3
+ readonly diagnostics: {
4
+ readonly "missing-namespace": {
5
+ readonly severity: "warning";
6
+ readonly messages: {
7
+ readonly default: import("@typespec/compiler").CallableMessage<["type", "name"]>;
8
+ };
9
+ };
10
+ };
11
+ };
12
+ declare const lib: import("@typespec/compiler").TypeSpecLibrary<{
13
+ "missing-namespace": {
14
+ readonly default: import("@typespec/compiler").CallableMessage<["type", "name"]>;
15
+ };
16
+ }, Record<string, any>>;
17
+ export declare const reportDiagnostic: <C extends "missing-namespace", M extends keyof {
18
+ "missing-namespace": {
19
+ readonly default: import("@typespec/compiler").CallableMessage<["type", "name"]>;
20
+ };
21
+ }[C]>(program: import("@typespec/compiler").Program, diag: import("@typespec/compiler").DiagnosticReport<{
22
+ "missing-namespace": {
23
+ readonly default: import("@typespec/compiler").CallableMessage<["type", "name"]>;
24
+ };
25
+ }, C, M>) => void;
26
+ export type OpenAPILibrary = typeof lib;
27
+ export {};
28
+ //# sourceMappingURL=lib.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["../../src/lib.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,MAAM;;;;;;;;;;CAUT,CAAC;AACX,QAAA,MAAM,GAAG;;;;uBAAgC,CAAC;AAC1C,eAAO,MAAQ,gBAAgB;;;;;;;;iBAAQ,CAAC;AAExC,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,CAAC"}
@@ -0,0 +1,15 @@
1
+ import { createTypeSpecLibrary, paramMessage } from "@typespec/compiler";
2
+ export const libDef = {
3
+ name: "@typespec/library-linter",
4
+ diagnostics: {
5
+ "missing-namespace": {
6
+ severity: "warning",
7
+ messages: {
8
+ default: paramMessage `${"type"} '${"name"}' is not in a namespace. This is bad practice for a published library.`,
9
+ },
10
+ },
11
+ },
12
+ };
13
+ const lib = createTypeSpecLibrary(libDef);
14
+ export const { reportDiagnostic } = lib;
15
+ //# sourceMappingURL=lib.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lib.js","sourceRoot":"","sources":["../../src/lib.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEzE,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB,IAAI,EAAE,0BAA0B;IAChC,WAAW,EAAE;QACX,mBAAmB,EAAE;YACnB,QAAQ,EAAE,SAAS;YACnB,QAAQ,EAAE;gBACR,OAAO,EAAE,YAAY,CAAA,GAAG,MAAM,KAAK,MAAM,wEAAwE;aAClH;SACF;KACF;CACO,CAAC;AACX,MAAM,GAAG,GAAG,qBAAqB,CAAC,MAAM,CAAC,CAAC;AAC1C,MAAM,CAAC,MAAM,EAAE,gBAAgB,EAAE,GAAG,GAAG,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { Program } from "@typespec/compiler";
2
+ export declare function $onValidate(program: Program): void;
3
+ //# sourceMappingURL=linter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"linter.d.ts","sourceRoot":"","sources":["../../src/linter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,OAAO,EAAqB,MAAM,oBAAoB,CAAC;AAG3E,wBAAgB,WAAW,CAAC,OAAO,EAAE,OAAO,QAI3C"}
@@ -0,0 +1,40 @@
1
+ import { reportDiagnostic } from "./lib.js";
2
+ export function $onValidate(program) {
3
+ const root = program.checker.getGlobalNamespaceType();
4
+ validateNoExportAtRoot(program, root);
5
+ }
6
+ function validateNoExportAtRoot(program, root) {
7
+ var _a, _b;
8
+ function validateFor(items) {
9
+ var _a;
10
+ for (const type of items.values()) {
11
+ reportDiagnostic(program, {
12
+ code: "missing-namespace",
13
+ format: { type: type.kind, name: (_a = type.name) !== null && _a !== void 0 ? _a : "" },
14
+ target: type,
15
+ });
16
+ }
17
+ }
18
+ validateFor(root.models);
19
+ validateFor(root.interfaces);
20
+ validateFor(root.enums);
21
+ validateFor(root.operations);
22
+ validateFor(root.unions);
23
+ for (const [name, sym] of (_b = (_a = root.node.symbol.exports) === null || _a === void 0 ? void 0 : _a.entries()) !== null && _b !== void 0 ? _b : []) {
24
+ if (sym.flags & 16384 /* SymbolFlags.Decorator */) {
25
+ reportDiagnostic(program, {
26
+ code: "missing-namespace",
27
+ format: { type: "Decorator", name },
28
+ target: sym,
29
+ });
30
+ }
31
+ else if (sym.flags & 131072 /* SymbolFlags.Function */) {
32
+ reportDiagnostic(program, {
33
+ code: "missing-namespace",
34
+ format: { type: "Function", name },
35
+ target: sym,
36
+ });
37
+ }
38
+ }
39
+ }
40
+ //# sourceMappingURL=linter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"linter.js","sourceRoot":"","sources":["../../src/linter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE5C,MAAM,UAAU,WAAW,CAAC,OAAgB;IAC1C,MAAM,IAAI,GAAG,OAAO,CAAC,OAAQ,CAAC,sBAAsB,EAAE,CAAC;IAEvD,sBAAsB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AACxC,CAAC;AAED,SAAS,sBAAsB,CAAC,OAAgB,EAAE,IAAe;;IAC/D,SAAS,WAAW,CAAC,KAA4C;;QAC/D,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,MAAM,EAAE,EAAE;YACjC,gBAAgB,CAAC,OAAO,EAAE;gBACxB,IAAI,EAAE,mBAAmB;gBACzB,MAAM,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,MAAA,IAAI,CAAC,IAAI,mCAAI,EAAE,EAAE;gBAClD,MAAM,EAAE,IAAI;aACb,CAAC,CAAC;SACJ;IACH,CAAC;IACD,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACzB,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC7B,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACxB,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC7B,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAEzB,KAAK,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,IAAI,MAAA,MAAA,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,0CAAE,OAAO,EAAE,mCAAI,EAAE,EAAE;QACnE,IAAI,GAAG,CAAC,KAAK,oCAAwB,EAAE;YACrC,gBAAgB,CAAC,OAAO,EAAE;gBACxB,IAAI,EAAE,mBAAmB;gBACzB,MAAM,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE;gBACnC,MAAM,EAAE,GAAG;aACZ,CAAC,CAAC;SACJ;aAAM,IAAI,GAAG,CAAC,KAAK,oCAAuB,EAAE;YAC3C,gBAAgB,CAAC,OAAO,EAAE;gBACxB,IAAI,EAAE,mBAAmB;gBACzB,MAAM,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE;gBAClC,MAAM,EAAE,GAAG;aACZ,CAAC,CAAC;SACJ;KACF;AACH,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { TypeSpecTestLibrary } from "@typespec/compiler/testing";
2
+ export declare const LibraryLinterTestLibrary: TypeSpecTestLibrary;
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/testing/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAqB,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAGpF,eAAO,MAAM,wBAAwB,EAAE,mBAGrC,CAAC"}
@@ -0,0 +1,8 @@
1
+ import { resolvePath } from "@typespec/compiler";
2
+ import { createTestLibrary } from "@typespec/compiler/testing";
3
+ import { fileURLToPath } from "url";
4
+ export const LibraryLinterTestLibrary = createTestLibrary({
5
+ name: "@typespec/library-linter",
6
+ packageRoot: resolvePath(fileURLToPath(import.meta.url), "../../../../"),
7
+ });
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/testing/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAuB,MAAM,4BAA4B,CAAC;AACpF,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AAEpC,MAAM,CAAC,MAAM,wBAAwB,GAAwB,iBAAiB,CAAC;IAC7E,IAAI,EAAE,0BAA0B;IAChC,WAAW,EAAE,WAAW,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,cAAc,CAAC;CACzE,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@typespec/library-linter",
3
+ "version": "0.41.0",
4
+ "author": "Microsoft Corporation",
5
+ "description": "TypeSpec library for linting another library.",
6
+ "homepage": "https://microsoft.github.io/typespec",
7
+ "readme": "https://github.com/Microsoft/typespec/blob/master/README.md",
8
+ "license": "MIT",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/Microsoft/typespec.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/Microsoft/typespec/issues"
15
+ },
16
+ "keywords": [
17
+ "typespec"
18
+ ],
19
+ "type": "module",
20
+ "main": "dist/src/index.js",
21
+ "exports": {
22
+ ".": "./dist/src/index.js",
23
+ "./testing": "./dist/src/testing/index.js"
24
+ },
25
+ "typesVersions": {
26
+ "*": {
27
+ "*": [
28
+ "./dist/src/index.d.ts"
29
+ ],
30
+ "testing": [
31
+ "./dist/src/testing/index.d.ts"
32
+ ]
33
+ }
34
+ },
35
+ "tspMain": "dist/src/index.js",
36
+ "engines": {
37
+ "node": ">=14.0.0"
38
+ },
39
+ "files": [
40
+ "lib/*.tsp",
41
+ "dist/**",
42
+ "!dist/test/**"
43
+ ],
44
+ "peerDependencies": {
45
+ "@typespec/compiler": "~0.41.0"
46
+ },
47
+ "devDependencies": {
48
+ "@types/mocha": "~10.0.0",
49
+ "@types/node": "~18.11.9",
50
+ "@typespec/compiler": "~0.41.0",
51
+ "@typespec/eslint-config-typespec": "~0.6.0",
52
+ "eslint": "^8.12.0",
53
+ "mocha": "~10.1.0",
54
+ "mocha-junit-reporter": "~2.2.0",
55
+ "mocha-multi-reporters": "~1.5.1",
56
+ "c8": "~7.12.0",
57
+ "rimraf": "~3.0.2",
58
+ "typescript": "~4.9.3"
59
+ },
60
+ "scripts": {
61
+ "clean": "rimraf ./dist ./temp",
62
+ "build": "tsc -p .",
63
+ "watch": "tsc -p . --watch",
64
+ "test": "mocha",
65
+ "test-official": "c8 mocha --forbid-only --reporter mocha-multi-reporters",
66
+ "lint": "eslint . --ext .ts --max-warnings=0",
67
+ "lint:fix": "eslint . --fix --ext .ts"
68
+ }
69
+ }