@valbuild/eslint-plugin 0.60.24 → 0.60.26

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.
@@ -3,41 +3,19 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var path = require('path');
6
+ var fs = require('fs');
6
7
 
7
8
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
8
9
 
9
10
  var path__default = /*#__PURE__*/_interopDefault(path);
11
+ var fs__default = /*#__PURE__*/_interopDefault(fs);
10
12
 
11
- function _arrayLikeToArray(arr, len) {
12
- if (len == null || len > arr.length) len = arr.length;
13
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
14
- return arr2;
15
- }
16
-
17
- function _arrayWithoutHoles(arr) {
18
- if (Array.isArray(arr)) return _arrayLikeToArray(arr);
19
- }
20
-
21
- function _iterableToArray(iter) {
22
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
23
- }
24
-
25
- function _unsupportedIterableToArray(o, minLen) {
26
- if (!o) return;
27
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
28
- var n = Object.prototype.toString.call(o).slice(8, -1);
29
- if (n === "Object" && o.constructor) n = o.constructor.name;
30
- if (n === "Map" || n === "Set") return Array.from(o);
31
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
32
- }
33
-
34
- function _nonIterableSpread() {
35
- throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
36
- }
37
-
38
- function _toConsumableArray(arr) {
39
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
40
- }
13
+ // @ts-check
14
+
15
+ /**
16
+ * @type {Record<string, string>}
17
+ */
18
+ var PACKAGE_JSON_DIRS_CACHE = {}; // we cache to avoid having to do as many fs operations. however, it will fail if the user moves package.json / root directories. For now, we accept that bug since performance is considered more important. Maybe there is a way to know when that happens so we avoid those bugs.
41
19
 
42
20
  /**
43
21
  * @type {import('eslint').Rule.RuleModule}
@@ -64,21 +42,19 @@ var noIllegalModuleIds = {
64
42
  return n.type === "ImportDeclaration" && typeof n.source.value === "string" && (n.source.value.endsWith("val.config") || n.source.value.endsWith("val.config.ts") || n.source.value.endsWith("val.config.js")) ? n.source.value : false;
65
43
  });
66
44
  if ((maybeValConfigImportDeclaration === null || maybeValConfigImportDeclaration === void 0 ? void 0 : maybeValConfigImportDeclaration.type) === "ImportDeclaration" && typeof maybeValConfigImportDeclaration.source.value === "string") {
67
- var valConfigImportSource = maybeValConfigImportDeclaration.source.value;
68
45
  var filename = context.filename || context.getFilename();
69
46
  if (filename !== null && filename !== void 0 && filename.endsWith(".val.ts") || filename !== null && filename !== void 0 && filename.endsWith(".val.js")) {
70
- var root = context.cwd || process.cwd();
71
- var relativePath = path__default["default"].relative(root, filename);
72
- expectedValue = relativePath.replace(/\.val\.(ts|js)$/, "");
73
- // TODO: this feels like a weird way to figure out the correct relative path,
74
- // in a monorepo, the root dir will be the root of the monorepo, not the root of the package
75
- // so we need to account for that
76
- // Assume the import of the val.config is correct and that it is in the root folder
77
- var numberOfDirsToRoot = valConfigImportSource.split(".." + path__default["default"].sep).reduce(function (acc, curr) {
78
- return curr === "" ? acc + 1 : acc;
79
- }, 0);
80
- var pathSegments = expectedValue.split(path__default["default"].sep);
81
- expectedValue = "/".concat(path__default["default"].join.apply(path__default["default"], _toConsumableArray(pathSegments.slice(pathSegments.length - (numberOfDirsToRoot + 1), pathSegments.length))));
47
+ var packageJsonDir = PACKAGE_JSON_DIRS_CACHE[path__default["default"].dirname(filename)];
48
+ if (!packageJsonDir) {
49
+ var runtimeRoot = path__default["default"].resolve(context.cwd || process.cwd());
50
+ packageJsonDir = path__default["default"].resolve(path__default["default"].dirname(filename));
51
+ while (!fs__default["default"].existsSync(path__default["default"].join(packageJsonDir, "package.json")) && packageJsonDir !== runtimeRoot) {
52
+ packageJsonDir = path__default["default"].dirname(packageJsonDir);
53
+ }
54
+ PACKAGE_JSON_DIRS_CACHE[path__default["default"].dirname(filename)] = packageJsonDir;
55
+ }
56
+ var relativePath = path__default["default"].relative(packageJsonDir, filename);
57
+ expectedValue = "/".concat(relativePath.replace(/\.val\.(ts|js)$/, ""));
82
58
  }
83
59
  }
84
60
  } else {
@@ -3,41 +3,19 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var path = require('path');
6
+ var fs = require('fs');
6
7
 
7
8
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
8
9
 
9
10
  var path__default = /*#__PURE__*/_interopDefault(path);
11
+ var fs__default = /*#__PURE__*/_interopDefault(fs);
10
12
 
11
- function _arrayLikeToArray(arr, len) {
12
- if (len == null || len > arr.length) len = arr.length;
13
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
14
- return arr2;
15
- }
16
-
17
- function _arrayWithoutHoles(arr) {
18
- if (Array.isArray(arr)) return _arrayLikeToArray(arr);
19
- }
20
-
21
- function _iterableToArray(iter) {
22
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
23
- }
24
-
25
- function _unsupportedIterableToArray(o, minLen) {
26
- if (!o) return;
27
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
28
- var n = Object.prototype.toString.call(o).slice(8, -1);
29
- if (n === "Object" && o.constructor) n = o.constructor.name;
30
- if (n === "Map" || n === "Set") return Array.from(o);
31
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
32
- }
33
-
34
- function _nonIterableSpread() {
35
- throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
36
- }
37
-
38
- function _toConsumableArray(arr) {
39
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
40
- }
13
+ // @ts-check
14
+
15
+ /**
16
+ * @type {Record<string, string>}
17
+ */
18
+ var PACKAGE_JSON_DIRS_CACHE = {}; // we cache to avoid having to do as many fs operations. however, it will fail if the user moves package.json / root directories. For now, we accept that bug since performance is considered more important. Maybe there is a way to know when that happens so we avoid those bugs.
41
19
 
42
20
  /**
43
21
  * @type {import('eslint').Rule.RuleModule}
@@ -64,21 +42,19 @@ var noIllegalModuleIds = {
64
42
  return n.type === "ImportDeclaration" && typeof n.source.value === "string" && (n.source.value.endsWith("val.config") || n.source.value.endsWith("val.config.ts") || n.source.value.endsWith("val.config.js")) ? n.source.value : false;
65
43
  });
66
44
  if ((maybeValConfigImportDeclaration === null || maybeValConfigImportDeclaration === void 0 ? void 0 : maybeValConfigImportDeclaration.type) === "ImportDeclaration" && typeof maybeValConfigImportDeclaration.source.value === "string") {
67
- var valConfigImportSource = maybeValConfigImportDeclaration.source.value;
68
45
  var filename = context.filename || context.getFilename();
69
46
  if (filename !== null && filename !== void 0 && filename.endsWith(".val.ts") || filename !== null && filename !== void 0 && filename.endsWith(".val.js")) {
70
- var root = context.cwd || process.cwd();
71
- var relativePath = path__default["default"].relative(root, filename);
72
- expectedValue = relativePath.replace(/\.val\.(ts|js)$/, "");
73
- // TODO: this feels like a weird way to figure out the correct relative path,
74
- // in a monorepo, the root dir will be the root of the monorepo, not the root of the package
75
- // so we need to account for that
76
- // Assume the import of the val.config is correct and that it is in the root folder
77
- var numberOfDirsToRoot = valConfigImportSource.split(".." + path__default["default"].sep).reduce(function (acc, curr) {
78
- return curr === "" ? acc + 1 : acc;
79
- }, 0);
80
- var pathSegments = expectedValue.split(path__default["default"].sep);
81
- expectedValue = "/".concat(path__default["default"].join.apply(path__default["default"], _toConsumableArray(pathSegments.slice(pathSegments.length - (numberOfDirsToRoot + 1), pathSegments.length))));
47
+ var packageJsonDir = PACKAGE_JSON_DIRS_CACHE[path__default["default"].dirname(filename)];
48
+ if (!packageJsonDir) {
49
+ var runtimeRoot = path__default["default"].resolve(context.cwd || process.cwd());
50
+ packageJsonDir = path__default["default"].resolve(path__default["default"].dirname(filename));
51
+ while (!fs__default["default"].existsSync(path__default["default"].join(packageJsonDir, "package.json")) && packageJsonDir !== runtimeRoot) {
52
+ packageJsonDir = path__default["default"].dirname(packageJsonDir);
53
+ }
54
+ PACKAGE_JSON_DIRS_CACHE[path__default["default"].dirname(filename)] = packageJsonDir;
55
+ }
56
+ var relativePath = path__default["default"].relative(packageJsonDir, filename);
57
+ expectedValue = "/".concat(relativePath.replace(/\.val\.(ts|js)$/, ""));
82
58
  }
83
59
  }
84
60
  } else {
@@ -1,35 +1,12 @@
1
1
  import path from 'path';
2
+ import fs from 'fs';
2
3
 
3
- function _arrayLikeToArray(arr, len) {
4
- if (len == null || len > arr.length) len = arr.length;
5
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
6
- return arr2;
7
- }
8
-
9
- function _arrayWithoutHoles(arr) {
10
- if (Array.isArray(arr)) return _arrayLikeToArray(arr);
11
- }
12
-
13
- function _iterableToArray(iter) {
14
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
15
- }
16
-
17
- function _unsupportedIterableToArray(o, minLen) {
18
- if (!o) return;
19
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
20
- var n = Object.prototype.toString.call(o).slice(8, -1);
21
- if (n === "Object" && o.constructor) n = o.constructor.name;
22
- if (n === "Map" || n === "Set") return Array.from(o);
23
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
24
- }
25
-
26
- function _nonIterableSpread() {
27
- throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
28
- }
4
+ // @ts-check
29
5
 
30
- function _toConsumableArray(arr) {
31
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
32
- }
6
+ /**
7
+ * @type {Record<string, string>}
8
+ */
9
+ var PACKAGE_JSON_DIRS_CACHE = {}; // we cache to avoid having to do as many fs operations. however, it will fail if the user moves package.json / root directories. For now, we accept that bug since performance is considered more important. Maybe there is a way to know when that happens so we avoid those bugs.
33
10
 
34
11
  /**
35
12
  * @type {import('eslint').Rule.RuleModule}
@@ -56,21 +33,19 @@ var noIllegalModuleIds = {
56
33
  return n.type === "ImportDeclaration" && typeof n.source.value === "string" && (n.source.value.endsWith("val.config") || n.source.value.endsWith("val.config.ts") || n.source.value.endsWith("val.config.js")) ? n.source.value : false;
57
34
  });
58
35
  if ((maybeValConfigImportDeclaration === null || maybeValConfigImportDeclaration === void 0 ? void 0 : maybeValConfigImportDeclaration.type) === "ImportDeclaration" && typeof maybeValConfigImportDeclaration.source.value === "string") {
59
- var valConfigImportSource = maybeValConfigImportDeclaration.source.value;
60
36
  var filename = context.filename || context.getFilename();
61
37
  if (filename !== null && filename !== void 0 && filename.endsWith(".val.ts") || filename !== null && filename !== void 0 && filename.endsWith(".val.js")) {
62
- var root = context.cwd || process.cwd();
63
- var relativePath = path.relative(root, filename);
64
- expectedValue = relativePath.replace(/\.val\.(ts|js)$/, "");
65
- // TODO: this feels like a weird way to figure out the correct relative path,
66
- // in a monorepo, the root dir will be the root of the monorepo, not the root of the package
67
- // so we need to account for that
68
- // Assume the import of the val.config is correct and that it is in the root folder
69
- var numberOfDirsToRoot = valConfigImportSource.split(".." + path.sep).reduce(function (acc, curr) {
70
- return curr === "" ? acc + 1 : acc;
71
- }, 0);
72
- var pathSegments = expectedValue.split(path.sep);
73
- expectedValue = "/".concat(path.join.apply(path, _toConsumableArray(pathSegments.slice(pathSegments.length - (numberOfDirsToRoot + 1), pathSegments.length))));
38
+ var packageJsonDir = PACKAGE_JSON_DIRS_CACHE[path.dirname(filename)];
39
+ if (!packageJsonDir) {
40
+ var runtimeRoot = path.resolve(context.cwd || process.cwd());
41
+ packageJsonDir = path.resolve(path.dirname(filename));
42
+ while (!fs.existsSync(path.join(packageJsonDir, "package.json")) && packageJsonDir !== runtimeRoot) {
43
+ packageJsonDir = path.dirname(packageJsonDir);
44
+ }
45
+ PACKAGE_JSON_DIRS_CACHE[path.dirname(filename)] = packageJsonDir;
46
+ }
47
+ var relativePath = path.relative(packageJsonDir, filename);
48
+ expectedValue = "/".concat(relativePath.replace(/\.val\.(ts|js)$/, ""));
74
49
  }
75
50
  }
76
51
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@valbuild/eslint-plugin",
3
- "version": "0.60.24",
3
+ "version": "0.60.26",
4
4
  "description": "ESLint rules for val",
5
5
  "keywords": [
6
6
  "eslint",
@@ -1,5 +1,11 @@
1
1
  // @ts-check
2
2
  import path from "path";
3
+ import fs from "fs";
4
+
5
+ /**
6
+ * @type {Record<string, string>}
7
+ */
8
+ const PACKAGE_JSON_DIRS_CACHE = {}; // we cache to avoid having to do as many fs operations. however, it will fail if the user moves package.json / root directories. For now, we accept that bug since performance is considered more important. Maybe there is a way to know when that happens so we avoid those bugs.
3
9
 
4
10
  /**
5
11
  * @type {import('eslint').Rule.RuleModule}
@@ -36,30 +42,27 @@ export default {
36
42
  maybeValConfigImportDeclaration?.type === "ImportDeclaration" &&
37
43
  typeof maybeValConfigImportDeclaration.source.value === "string"
38
44
  ) {
39
- const valConfigImportSource =
40
- maybeValConfigImportDeclaration.source.value;
41
45
  const filename = context.filename || context.getFilename();
42
46
  if (
43
47
  filename?.endsWith(".val.ts") ||
44
48
  filename?.endsWith(".val.js")
45
49
  ) {
46
- const root = context.cwd || process.cwd();
47
- const relativePath = path.relative(root, filename);
48
- expectedValue = relativePath.replace(/\.val\.(ts|js)$/, "");
49
- // TODO: this feels like a weird way to figure out the correct relative path,
50
- // in a monorepo, the root dir will be the root of the monorepo, not the root of the package
51
- // so we need to account for that
52
- // Assume the import of the val.config is correct and that it is in the root folder
53
- const numberOfDirsToRoot = valConfigImportSource
54
- .split(".." + path.sep)
55
- .reduce((acc, curr) => (curr === "" ? acc + 1 : acc), 0);
56
- const pathSegments = expectedValue.split(path.sep);
57
- expectedValue = `/${path.join(
58
- ...pathSegments.slice(
59
- pathSegments.length - (numberOfDirsToRoot + 1),
60
- pathSegments.length
61
- )
62
- )}`;
50
+ let packageJsonDir =
51
+ PACKAGE_JSON_DIRS_CACHE[path.dirname(filename)];
52
+ if (!packageJsonDir) {
53
+ const runtimeRoot = path.resolve(context.cwd || process.cwd());
54
+ packageJsonDir = path.resolve(path.dirname(filename));
55
+ while (
56
+ !fs.existsSync(path.join(packageJsonDir, "package.json")) &&
57
+ packageJsonDir !== runtimeRoot
58
+ ) {
59
+ packageJsonDir = path.dirname(packageJsonDir);
60
+ }
61
+ PACKAGE_JSON_DIRS_CACHE[path.dirname(filename)] =
62
+ packageJsonDir;
63
+ }
64
+ const relativePath = path.relative(packageJsonDir, filename);
65
+ expectedValue = `/${relativePath.replace(/\.val\.(ts|js)$/, "")}`;
63
66
  }
64
67
  }
65
68
  } else {
@@ -67,24 +67,25 @@ export default c.define(
67
67
  '"/content/stuff/with/all/test"'
68
68
  );
69
69
  });
70
- test("no illegal modules for monorepos (projects that are not at root) - src", async () => {
71
- const code = `import { s, c } from "../../../../val.config";
70
+ // TODO: we can't test this anymore because we do not know the root dir - perhaps the
71
+ // test("no illegal modules for monorepos (projects that are not at root) - src", async () => {
72
+ // const code = `import { s, c } from "../../../../val.config";
72
73
 
73
- export const schema = s.string();
74
+ // export const schema = s.string();
74
75
 
75
- export default c.define(
76
- "/something",
77
- schema,
78
- "React Server components also works"
79
- );`;
80
- const results = await eslint.lintText(code, {
81
- filePath: "./src/content/stuff/with/all/test.val.ts",
82
- });
76
+ // export default c.define(
77
+ // "/something",
78
+ // schema,
79
+ // "React Server components also works"
80
+ // );`;
81
+ // const results = await eslint.lintText(code, {
82
+ // filePath: "./src/content/stuff/with/all/test.val.ts",
83
+ // });
83
84
 
84
- expect(results).toHaveLength(1);
85
- expect(results[0].messages).toHaveLength(1);
86
- expect(results[0].messages[0].fix?.text).toEqual(
87
- '"/content/stuff/with/all/test"'
88
- );
89
- });
85
+ // expect(results).toHaveLength(1);
86
+ // expect(results[0].messages).toHaveLength(1);
87
+ // expect(results[0].messages[0].fix?.text).toEqual(
88
+ // '"/content/stuff/with/all/test"'
89
+ // );
90
+ // });
90
91
  });