@valbuild/eslint-plugin 0.22.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/CHANGELOG.md ADDED
File without changes
@@ -0,0 +1,75 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var path = require('path');
6
+
7
+ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
8
+
9
+ var path__default = /*#__PURE__*/_interopDefault(path);
10
+
11
+ // @ts-check
12
+ var getExpectedValModuleName = function getExpectedValModuleName( /** @type {{ report?: (arg0: { node: any; message: string; fix: ((fixer: any) => any) | ((fixer: any) => any); }) => void; getFilename?: any; cwd?: any; }} */context) {
13
+ var filename = context.getFilename();
14
+ if (filename.endsWith(".val.ts") || filename.endsWith(".val.js")) {
15
+ var root = context.cwd || process.cwd();
16
+ var relativePath = path__default["default"].relative(root, filename);
17
+ var expectedValue = relativePath.replace(/\.val\.(ts|js)$/, "");
18
+ return "/".concat(expectedValue);
19
+ }
20
+ };
21
+ var noIllegalModuleIds = {
22
+ meta: {
23
+ type: "problem",
24
+ docs: {
25
+ description: "Check that the first argument of export default declaration matches the string from val.config.ts file.",
26
+ category: "Best Practices",
27
+ recommended: "error"
28
+ },
29
+ fixable: "code",
30
+ schema: []
31
+ },
32
+ create: function create( /** @type {{ report: (arg0: { node: any; message: string; fix: ((fixer: any) => any) | ((fixer: any) => any); }) => void; }} */context) {
33
+ var expectedValue = getExpectedValModuleName(context);
34
+ return {
35
+ /**
36
+ * @param {{ declaration: { arguments: string | any[]; }; }} node
37
+ */
38
+ ExportDefaultDeclaration: function ExportDefaultDeclaration(node) {
39
+ if (!expectedValue) {
40
+ return;
41
+ }
42
+ if (node.declaration && node.declaration.arguments && node.declaration.arguments.length > 0) {
43
+ var firstArg = node.declaration.arguments[0];
44
+ if (firstArg.type === "TemplateLiteral") {
45
+ context.report({
46
+ node: firstArg,
47
+ message: "val.content first argument should not be a template literal",
48
+ fix: function fix(fixer) {
49
+ return fixer.replaceText(firstArg, "\"".concat(expectedValue, "\""));
50
+ }
51
+ });
52
+ }
53
+ if (firstArg.type === "Literal" && typeof firstArg.value === "string") {
54
+ if (firstArg.value !== expectedValue) {
55
+ context.report({
56
+ node: firstArg,
57
+ message: "val.content first argument must match filename",
58
+ fix: function fix(fixer) {
59
+ return fixer.replaceText(firstArg, "".concat(firstArg.raw[0]).concat(expectedValue).concat(firstArg.raw[0]));
60
+ }
61
+ });
62
+ }
63
+ }
64
+ }
65
+ }
66
+ };
67
+ }
68
+ };
69
+
70
+ // @ts-check
71
+ var rules = {
72
+ "no-illegal-module-ids": noIllegalModuleIds
73
+ };
74
+
75
+ exports.rules = rules;
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ if (process.env.NODE_ENV === "production") {
4
+ module.exports = require("./valbuild-eslint-plugin.cjs.prod.js");
5
+ } else {
6
+ module.exports = require("./valbuild-eslint-plugin.cjs.dev.js");
7
+ }
@@ -0,0 +1,75 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var path = require('path');
6
+
7
+ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
8
+
9
+ var path__default = /*#__PURE__*/_interopDefault(path);
10
+
11
+ // @ts-check
12
+ var getExpectedValModuleName = function getExpectedValModuleName( /** @type {{ report?: (arg0: { node: any; message: string; fix: ((fixer: any) => any) | ((fixer: any) => any); }) => void; getFilename?: any; cwd?: any; }} */context) {
13
+ var filename = context.getFilename();
14
+ if (filename.endsWith(".val.ts") || filename.endsWith(".val.js")) {
15
+ var root = context.cwd || process.cwd();
16
+ var relativePath = path__default["default"].relative(root, filename);
17
+ var expectedValue = relativePath.replace(/\.val\.(ts|js)$/, "");
18
+ return "/".concat(expectedValue);
19
+ }
20
+ };
21
+ var noIllegalModuleIds = {
22
+ meta: {
23
+ type: "problem",
24
+ docs: {
25
+ description: "Check that the first argument of export default declaration matches the string from val.config.ts file.",
26
+ category: "Best Practices",
27
+ recommended: "error"
28
+ },
29
+ fixable: "code",
30
+ schema: []
31
+ },
32
+ create: function create( /** @type {{ report: (arg0: { node: any; message: string; fix: ((fixer: any) => any) | ((fixer: any) => any); }) => void; }} */context) {
33
+ var expectedValue = getExpectedValModuleName(context);
34
+ return {
35
+ /**
36
+ * @param {{ declaration: { arguments: string | any[]; }; }} node
37
+ */
38
+ ExportDefaultDeclaration: function ExportDefaultDeclaration(node) {
39
+ if (!expectedValue) {
40
+ return;
41
+ }
42
+ if (node.declaration && node.declaration.arguments && node.declaration.arguments.length > 0) {
43
+ var firstArg = node.declaration.arguments[0];
44
+ if (firstArg.type === "TemplateLiteral") {
45
+ context.report({
46
+ node: firstArg,
47
+ message: "val.content first argument should not be a template literal",
48
+ fix: function fix(fixer) {
49
+ return fixer.replaceText(firstArg, "\"".concat(expectedValue, "\""));
50
+ }
51
+ });
52
+ }
53
+ if (firstArg.type === "Literal" && typeof firstArg.value === "string") {
54
+ if (firstArg.value !== expectedValue) {
55
+ context.report({
56
+ node: firstArg,
57
+ message: "val.content first argument must match filename",
58
+ fix: function fix(fixer) {
59
+ return fixer.replaceText(firstArg, "".concat(firstArg.raw[0]).concat(expectedValue).concat(firstArg.raw[0]));
60
+ }
61
+ });
62
+ }
63
+ }
64
+ }
65
+ }
66
+ };
67
+ }
68
+ };
69
+
70
+ // @ts-check
71
+ var rules = {
72
+ "no-illegal-module-ids": noIllegalModuleIds
73
+ };
74
+
75
+ exports.rules = rules;
@@ -0,0 +1,67 @@
1
+ import path from 'path';
2
+
3
+ // @ts-check
4
+ var getExpectedValModuleName = function getExpectedValModuleName( /** @type {{ report?: (arg0: { node: any; message: string; fix: ((fixer: any) => any) | ((fixer: any) => any); }) => void; getFilename?: any; cwd?: any; }} */context) {
5
+ var filename = context.getFilename();
6
+ if (filename.endsWith(".val.ts") || filename.endsWith(".val.js")) {
7
+ var root = context.cwd || process.cwd();
8
+ var relativePath = path.relative(root, filename);
9
+ var expectedValue = relativePath.replace(/\.val\.(ts|js)$/, "");
10
+ return "/".concat(expectedValue);
11
+ }
12
+ };
13
+ var noIllegalModuleIds = {
14
+ meta: {
15
+ type: "problem",
16
+ docs: {
17
+ description: "Check that the first argument of export default declaration matches the string from val.config.ts file.",
18
+ category: "Best Practices",
19
+ recommended: "error"
20
+ },
21
+ fixable: "code",
22
+ schema: []
23
+ },
24
+ create: function create( /** @type {{ report: (arg0: { node: any; message: string; fix: ((fixer: any) => any) | ((fixer: any) => any); }) => void; }} */context) {
25
+ var expectedValue = getExpectedValModuleName(context);
26
+ return {
27
+ /**
28
+ * @param {{ declaration: { arguments: string | any[]; }; }} node
29
+ */
30
+ ExportDefaultDeclaration: function ExportDefaultDeclaration(node) {
31
+ if (!expectedValue) {
32
+ return;
33
+ }
34
+ if (node.declaration && node.declaration.arguments && node.declaration.arguments.length > 0) {
35
+ var firstArg = node.declaration.arguments[0];
36
+ if (firstArg.type === "TemplateLiteral") {
37
+ context.report({
38
+ node: firstArg,
39
+ message: "val.content first argument should not be a template literal",
40
+ fix: function fix(fixer) {
41
+ return fixer.replaceText(firstArg, "\"".concat(expectedValue, "\""));
42
+ }
43
+ });
44
+ }
45
+ if (firstArg.type === "Literal" && typeof firstArg.value === "string") {
46
+ if (firstArg.value !== expectedValue) {
47
+ context.report({
48
+ node: firstArg,
49
+ message: "val.content first argument must match filename",
50
+ fix: function fix(fixer) {
51
+ return fixer.replaceText(firstArg, "".concat(firstArg.raw[0]).concat(expectedValue).concat(firstArg.raw[0]));
52
+ }
53
+ });
54
+ }
55
+ }
56
+ }
57
+ }
58
+ };
59
+ }
60
+ };
61
+
62
+ // @ts-check
63
+ var rules = {
64
+ "no-illegal-module-ids": noIllegalModuleIds
65
+ };
66
+
67
+ export { rules };
package/jest.config.js ADDED
@@ -0,0 +1,4 @@
1
+ /** @type {import("jest").Config} */
2
+ module.exports = {
3
+ preset: "../../jest.preset",
4
+ };
package/jsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "compilerOptions": {
3
+ "strict": true,
4
+ "isolatedModules": true,
5
+ "noEmit": true,
6
+ "skipLibCheck": true
7
+ },
8
+ "exclude": ["dist/**/*"]
9
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@valbuild/eslint-plugin",
3
+ "version": "0.22.0",
4
+ "description": "ESLint rules for val",
5
+ "keywords": [
6
+ "eslint",
7
+ "eslintplugin",
8
+ "eslint-plugin",
9
+ "val",
10
+ "valbuild"
11
+ ],
12
+ "main": "dist/valbuild-eslint-plugin.cjs.js",
13
+ "module": "dist/valbuild-eslint-plugin.esm.js",
14
+ "exports": {
15
+ ".": {
16
+ "module": "./dist/valbuild-eslint-plugin.esm.js",
17
+ "default": "./dist/valbuild-eslint-plugin.cjs.js"
18
+ },
19
+ "./package.json": "./package.json"
20
+ },
21
+ "engines": {
22
+ "node": ">=6"
23
+ },
24
+ "license": "MIT",
25
+ "peerDependencies": {
26
+ "eslint": "6 || 7 || 8"
27
+ },
28
+ "devDependencies": {
29
+ "eslint": "^7.10.0"
30
+ },
31
+ "scripts": {
32
+ "typecheck": "tsc -p jsconfig.json"
33
+ }
34
+ }
package/src/index.js ADDED
@@ -0,0 +1,7 @@
1
+ // @ts-check
2
+
3
+ import noIllegalModuleIds from "./rules/noIllegalModuleIds";
4
+
5
+ export let rules = {
6
+ "no-illegal-module-ids": noIllegalModuleIds,
7
+ };
@@ -0,0 +1,74 @@
1
+ // @ts-check
2
+ import path from "path";
3
+
4
+ const getExpectedValModuleName = (
5
+ /** @type {{ report?: (arg0: { node: any; message: string; fix: ((fixer: any) => any) | ((fixer: any) => any); }) => void; getFilename?: any; cwd?: any; }} */ context
6
+ ) => {
7
+ const filename = context.getFilename();
8
+ if (filename.endsWith(".val.ts") || filename.endsWith(".val.js")) {
9
+ const root = context.cwd || process.cwd();
10
+ const relativePath = path.relative(root, filename);
11
+ const expectedValue = relativePath.replace(/\.val\.(ts|js)$/, "");
12
+ return `/${expectedValue}`;
13
+ }
14
+ };
15
+
16
+ export default {
17
+ meta: {
18
+ type: "problem",
19
+ docs: {
20
+ description:
21
+ "Check that the first argument of export default declaration matches the string from val.config.ts file.",
22
+ category: "Best Practices",
23
+ recommended: "error",
24
+ },
25
+ fixable: "code",
26
+ schema: [],
27
+ },
28
+ create: function (
29
+ /** @type {{ report: (arg0: { node: any; message: string; fix: ((fixer: any) => any) | ((fixer: any) => any); }) => void; }} */ context
30
+ ) {
31
+ const expectedValue = getExpectedValModuleName(context);
32
+ return {
33
+ /**
34
+ * @param {{ declaration: { arguments: string | any[]; }; }} node
35
+ */
36
+ ExportDefaultDeclaration(node) {
37
+ if (!expectedValue) {
38
+ return;
39
+ }
40
+ if (
41
+ node.declaration &&
42
+ node.declaration.arguments &&
43
+ node.declaration.arguments.length > 0
44
+ ) {
45
+ const firstArg = node.declaration.arguments[0];
46
+ if (firstArg.type === "TemplateLiteral") {
47
+ context.report({
48
+ node: firstArg,
49
+ message:
50
+ "val.content first argument should not be a template literal",
51
+ fix: (fixer) => fixer.replaceText(firstArg, `"${expectedValue}"`),
52
+ });
53
+ }
54
+ if (
55
+ firstArg.type === "Literal" &&
56
+ typeof firstArg.value === "string"
57
+ ) {
58
+ if (firstArg.value !== expectedValue) {
59
+ context.report({
60
+ node: firstArg,
61
+ message: "val.content first argument must match filename",
62
+ fix: (fixer) =>
63
+ fixer.replaceText(
64
+ firstArg,
65
+ `${firstArg.raw[0]}${expectedValue}${firstArg.raw[0]}`
66
+ ),
67
+ });
68
+ }
69
+ }
70
+ }
71
+ },
72
+ };
73
+ },
74
+ };
@@ -0,0 +1,73 @@
1
+ import { RuleTester } from "eslint";
2
+ import { rules as valRules } from "@valbuild/eslint-plugin";
3
+ import path from "path";
4
+
5
+ const rule = valRules["no-illegal-module-ids"];
6
+
7
+ RuleTester.setDefaultConfig({
8
+ parserOptions: {
9
+ ecmaVersion: 2018,
10
+ sourceType: "module",
11
+ ecmaFeatures: {
12
+ jsx: true,
13
+ },
14
+ },
15
+ });
16
+
17
+ const ruleTester = new RuleTester();
18
+
19
+ ruleTester.run("no-illegal-module-ids", rule, {
20
+ valid: [
21
+ {
22
+ filename: path.join(process.cwd(), "./foo/test.val.ts"),
23
+ code: `import { val, s } from '../val.config.ts';
24
+ export const schema = s.string();
25
+ export default val.content('/foo/test', schema, 'String')`,
26
+ },
27
+ ],
28
+ invalid: [
29
+ {
30
+ filename: path.join(process.cwd(), "./foo/test.val.ts"),
31
+ code: `import { val, s } from '../val.config.ts';
32
+ export const schema = s.string();
33
+ export default val.content('foo', schema, 'String')`,
34
+ errors: [
35
+ {
36
+ message: "val.content first argument must match filename",
37
+ },
38
+ ],
39
+ output: `import { val, s } from '../val.config.ts';
40
+ export const schema = s.string();
41
+ export default val.content('/foo/test', schema, 'String')`,
42
+ },
43
+ {
44
+ filename: path.join(process.cwd(), "./foo/test.val.ts"),
45
+ code: `import { val, s } from "../val.config.ts";
46
+ export const schema = s.string();
47
+ export default val.content("foo", schema, 'String')`,
48
+ errors: [
49
+ {
50
+ message: "val.content first argument must match filename",
51
+ },
52
+ ],
53
+ output: `import { val, s } from "../val.config.ts";
54
+ export const schema = s.string();
55
+ export default val.content("/foo/test", schema, 'String')`,
56
+ },
57
+ {
58
+ filename: path.join(process.cwd(), "./foo/test.val.ts"),
59
+ code: `import { val, s } from "../val.config.ts";
60
+ export const schema = s.string();
61
+ export default val.content(\`foo\`, schema, 'String')`,
62
+ errors: [
63
+ {
64
+ message:
65
+ "val.content first argument should not be a template literal",
66
+ },
67
+ ],
68
+ output: `import { val, s } from "../val.config.ts";
69
+ export const schema = s.string();
70
+ export default val.content("/foo/test", schema, 'String')`,
71
+ },
72
+ ],
73
+ });