@typespec/eslint-plugin 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,22 @@
1
+ # TypeSpec Eslint Plugin
2
+
3
+ ## Installation
4
+
5
+ Install the package as a dev dependency.
6
+
7
+ ```
8
+ npm install -D @typespec/eslint-plugin
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Add the following to your eslint config
14
+
15
+ ```yaml
16
+ plugins: ["@typespec/eslint-plugin"],
17
+ extends: ["plugin:@typespec/eslint-plugin/recommended"],
18
+ ```
19
+
20
+ ## Rules
21
+
22
+ - [call-decorator](./docs/rules/call-decorator.md)
@@ -0,0 +1,11 @@
1
+ export declare const rules: {
2
+ "call-decorator": import("@typescript-eslint/utils/dist/ts-eslint/Rule.js").RuleModule<"default" | "suggestReplaceWithContextCall", never[], import("@typescript-eslint/utils/dist/ts-eslint/Rule.js").RuleListener>;
3
+ };
4
+ export declare const configs: {
5
+ recommended: {
6
+ rules: {
7
+ "@typespec/call-decorator": string;
8
+ };
9
+ };
10
+ };
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,KAAK;;CAEjB,CAAC;AAEF,eAAO,MAAM,OAAO;;;;;;CAMnB,CAAC"}
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.configs = exports.rules = void 0;
4
+ const call_decorator_js_1 = require("./rules/call-decorator.js");
5
+ exports.rules = {
6
+ "call-decorator": call_decorator_js_1.callDecoratorRule,
7
+ };
8
+ exports.configs = {
9
+ recommended: {
10
+ rules: {
11
+ "@typespec/call-decorator": "warn",
12
+ },
13
+ },
14
+ };
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,iEAA8D;AAEjD,QAAA,KAAK,GAAG;IACnB,gBAAgB,EAAE,qCAAiB;CACpC,CAAC;AAEW,QAAA,OAAO,GAAG;IACrB,WAAW,EAAE;QACX,KAAK,EAAE;YACL,0BAA0B,EAAE,MAAM;SACnC;KACF;CACF,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { TSESLint } from "@typescript-eslint/utils";
2
+ export declare const callDecoratorRule: TSESLint.RuleModule<"default" | "suggestReplaceWithContextCall", never[], TSESLint.RuleListener>;
3
+ //# sourceMappingURL=call-decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"call-decorator.d.ts","sourceRoot":"","sources":["../../../src/rules/call-decorator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,QAAQ,EAAY,MAAM,0BAA0B,CAAC;AAS3E,eAAO,MAAM,iBAAiB,kGAkD5B,CAAC"}
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.callDecoratorRule = void 0;
7
+ const utils_1 = require("@typescript-eslint/utils");
8
+ const typescript_1 = __importDefault(require("typescript"));
9
+ const utils_js_1 = require("../utils.js");
10
+ const messages = {
11
+ default: "Use context.call to call a TypeSpec decorator function.",
12
+ suggestReplaceWithContextCall: "Replace with context.call",
13
+ };
14
+ exports.callDecoratorRule = (0, utils_js_1.createRule)({
15
+ create(context) {
16
+ const parserServices = utils_1.ESLintUtils.getParserServices(context);
17
+ const checker = parserServices.program.getTypeChecker();
18
+ return {
19
+ CallExpression(node) {
20
+ if (node.callee.type === utils_1.TSESTree.AST_NODE_TYPES.Identifier) {
21
+ const functionName = node.callee.name;
22
+ if (functionName.startsWith("$")) {
23
+ const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
24
+ const signature = checker.getResolvedSignature(tsNode);
25
+ if (signature === undefined) {
26
+ return;
27
+ }
28
+ if (isTypeSpecFunctionSignature(checker, signature, tsNode)) {
29
+ context.report({
30
+ messageId: "default",
31
+ node,
32
+ suggest: [
33
+ {
34
+ messageId: "suggestReplaceWithContextCall",
35
+ fix: (fixer) => {
36
+ return [
37
+ fixer.replaceText(node.callee, `context.call`),
38
+ fixer.replaceText(node.arguments[0], functionName),
39
+ ];
40
+ },
41
+ },
42
+ ],
43
+ });
44
+ }
45
+ }
46
+ }
47
+ },
48
+ };
49
+ },
50
+ name: "call-decorator",
51
+ meta: {
52
+ docs: {
53
+ description: "Calling a TypeSpec decorator from JS/TS code should be done with context.call",
54
+ recommended: "warn",
55
+ },
56
+ hasSuggestions: true,
57
+ messages,
58
+ type: "suggestion",
59
+ schema: [],
60
+ },
61
+ defaultOptions: [],
62
+ });
63
+ function isTypeSpecFunctionSignature(checker, signature, tsNode) {
64
+ if (signature.parameters.length < 2) {
65
+ return false;
66
+ }
67
+ const contextParameter = signature.parameters[0];
68
+ const contextParamType = checker.getTypeOfSymbolAtLocation(contextParameter, tsNode);
69
+ if (contextParamType.flags & typescript_1.default.TypeFlags.StructuredType &&
70
+ contextParamType.symbol.name === "DecoratorContext") {
71
+ return true;
72
+ }
73
+ return false;
74
+ }
75
+ //# sourceMappingURL=call-decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"call-decorator.js","sourceRoot":"","sources":["../../../src/rules/call-decorator.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA2E;AAC3E,4DAA4B;AAC5B,0CAAyC;AAEzC,MAAM,QAAQ,GAAG;IACf,OAAO,EAAE,yDAAyD;IAClE,6BAA6B,EAAE,2BAA2B;CAC3D,CAAC;AAEW,QAAA,iBAAiB,GAAG,IAAA,qBAAU,EAAiC;IAC1E,MAAM,CAAC,OAAO;QACZ,MAAM,cAAc,GAAG,mBAAW,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;QAC9D,MAAM,OAAO,GAAG,cAAc,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC;QACxD,OAAO;YACL,cAAc,CAAC,IAAI;gBACjB,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,gBAAQ,CAAC,cAAc,CAAC,UAAU,EAAE;oBAC3D,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC;oBACtC,IAAI,YAAY,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;wBAChC,MAAM,MAAM,GAAG,cAAc,CAAC,qBAAqB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;wBAE9D,MAAM,SAAS,GAAG,OAAO,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;wBACvD,IAAI,SAAS,KAAK,SAAS,EAAE;4BAC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ACD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { ESLintUtils } from "@typescript-eslint/utils";
2
+ export declare const createRule: <TOptions extends readonly unknown[], TMessageIds extends string, TRuleListener extends import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleListener = import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleListener>({ name, meta, ...rule }: Readonly<ESLintUtils.RuleWithMetaAndName<TOptions, TMessageIds, TRuleListener>>) => import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleModule<TMessageIds, TOptions, TRuleListener>;
3
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAGvD,eAAO,MAAM,UAAU,ybAGtB,CAAC"}
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createRule = void 0;
4
+ const utils_1 = require("@typescript-eslint/utils");
5
+ // TODO get rule url
6
+ exports.createRule = utils_1.ESLintUtils.RuleCreator((name) => `https://github.com/microsoft/typespec/tree/main/packages/eslint-plugin-typespec/docs/rules/${name}.md`);
7
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;AAAA,oDAAuD;AAEvD,oBAAoB;AACP,QAAA,UAAU,GAAG,mBAAW,CAAC,WAAW,CAC/C,CAAC,IAAI,EAAE,EAAE,CACP,8FAA8F,IAAI,KAAK,CAC1G,CAAC"}
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@typespec/eslint-plugin",
3
+ "version": "0.41.0",
4
+ "author": "Microsoft Corporation",
5
+ "description": "Eslint plugin providing set of rules to be used in the JS/TS code of TypeSpec libraries",
6
+ "homepage": "https://microsoft.github.io/typespec",
7
+ "readme": "https://github.com/Microsoft/typespec/blob/main/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": "commonjs",
20
+ "main": "dist/src/index.js",
21
+ "engines": {
22
+ "node": ">=14.0.0"
23
+ },
24
+ "files": [
25
+ "lib/*.tsp",
26
+ "dist/**",
27
+ "!dist/test/**"
28
+ ],
29
+ "peerDependencies": {
30
+ "eslint": ">=0.8.0"
31
+ },
32
+ "devDependencies": {
33
+ "@types/mocha": "~10.0.0",
34
+ "@types/node": "~18.11.9",
35
+ "@typespec/eslint-config-typespec": "~0.6.0",
36
+ "@typescript-eslint/parser": "^5.30.7",
37
+ "eslint": "^8.12.0",
38
+ "mocha": "~10.1.0",
39
+ "mocha-junit-reporter": "~2.2.0",
40
+ "mocha-multi-reporters": "~1.5.1",
41
+ "c8": "~7.12.0",
42
+ "rimraf": "~3.0.2",
43
+ "typescript": "~4.9.3"
44
+ },
45
+ "dependencies": {
46
+ "@typescript-eslint/utils": "~5.26.0"
47
+ },
48
+ "scripts": {
49
+ "clean": "rimraf ./dist ./temp",
50
+ "build": "tsc -p .",
51
+ "watch": "tsc -p . --watch",
52
+ "test": "mocha",
53
+ "test-official": "c8 mocha --forbid-only --reporter mocha-multi-reporters",
54
+ "lint": "eslint . --ext .ts --max-warnings=0",
55
+ "lint:fix": "eslint . --fix --ext .ts"
56
+ }
57
+ }