@dloizides/frontend-devtools 1.7.0 → 1.9.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.
@@ -1890,6 +1890,175 @@ var no_product_imports_in_shared_default = {
1890
1890
  }
1891
1891
  };
1892
1892
 
1893
+ // src/rules/no-raw-backend-fetch.js
1894
+ var DEFAULT_ALLOWED_PATH_FRAGMENTS = [
1895
+ "/src/server/",
1896
+ "/src/http/",
1897
+ "/src/lib/http/",
1898
+ "/generated/",
1899
+ "/app/+html.",
1900
+ "service-worker",
1901
+ "serviceWorker",
1902
+ "/sw."
1903
+ ];
1904
+ var CLIENT_PACKAGE_PATH = /\/packages\/[^/]+-client\//;
1905
+ var TEST_FILE = /(?:\.(?:test|spec)\.[cm]?[jt]sx?$)|(?:\/__tests__\/)/;
1906
+ var EXEMPT_URL_SCHEME = /^\s*(?:data|blob):/i;
1907
+ var ABSOLUTE_HTTP_URL = /^https?:\/\/[^/\s]+\//i;
1908
+ var ABSOLUTE_HTTP_LITERAL = /^https?:\/\/[^/\s]+(?:[/?#]|$)/i;
1909
+ var URL_OBJECTS = /* @__PURE__ */ new Set(["URL", "webkitURL"]);
1910
+ var GLOBAL_OBJECTS = /* @__PURE__ */ new Set(["window", "globalThis", "self"]);
1911
+ var AXIOS_MODULE = "axios";
1912
+ function normalisePath(filename) {
1913
+ return typeof filename === "string" ? filename.split("\\").join("/") : "";
1914
+ }
1915
+ function isExemptFile(filename, allow) {
1916
+ const path2 = normalisePath(filename);
1917
+ if (!path2.includes("/src/")) return true;
1918
+ if (TEST_FILE.test(path2)) return true;
1919
+ if (CLIENT_PACKAGE_PATH.test(path2)) return true;
1920
+ return [...DEFAULT_ALLOWED_PATH_FRAGMENTS, ...allow].some((fragment) => path2.includes(fragment));
1921
+ }
1922
+ function isFetchCallee(callee) {
1923
+ if (callee.type === "Identifier") return callee.name === "fetch";
1924
+ if (callee.type !== "MemberExpression" || callee.computed) return false;
1925
+ const isFetchProperty = callee.property.type === "Identifier" && callee.property.name === "fetch";
1926
+ const isGlobalObject = callee.object.type === "Identifier" && GLOBAL_OBJECTS.has(callee.object.name);
1927
+ return isFetchProperty && isGlobalObject;
1928
+ }
1929
+ function leadingText(node) {
1930
+ if (node === void 0) return "";
1931
+ if (node.type === "Literal" && typeof node.value === "string") return node.value;
1932
+ if (node.type === "TemplateLiteral") return node.quasis[0].value.cooked ?? "";
1933
+ return "";
1934
+ }
1935
+ function isDataOrBlobUrl(node) {
1936
+ return EXEMPT_URL_SCHEME.test(leadingText(node));
1937
+ }
1938
+ function isThirdPartyLiteralUrl(node) {
1939
+ if (node === void 0) return false;
1940
+ if (node.type === "Literal") return typeof node.value === "string" && ABSOLUTE_HTTP_LITERAL.test(node.value);
1941
+ if (node.type !== "TemplateLiteral") return false;
1942
+ const head = node.quasis[0].value.cooked ?? "";
1943
+ return node.expressions.length === 0 ? ABSOLUTE_HTTP_LITERAL.test(head) : ABSOLUTE_HTTP_URL.test(head);
1944
+ }
1945
+ function unwrapExpression(node) {
1946
+ let current = node;
1947
+ while (current !== void 0 && current !== null && ["TSAsExpression", "TSNonNullExpression", "TSTypeAssertion", "TSSatisfiesExpression"].includes(current.type))
1948
+ current = current.expression;
1949
+ return current;
1950
+ }
1951
+ function memberName(node) {
1952
+ return node.type === "MemberExpression" && !node.computed && node.property.type === "Identifier" ? node.property.name : "";
1953
+ }
1954
+ function isUrlObject(node) {
1955
+ if (node.type === "Identifier") return URL_OBJECTS.has(node.name);
1956
+ return memberName(node) !== "" && URL_OBJECTS.has(memberName(node)) && node.object.type === "Identifier" && GLOBAL_OBJECTS.has(node.object.name);
1957
+ }
1958
+ function isCreateObjectUrlCall(node) {
1959
+ if (node === void 0 || node === null || node.type !== "CallExpression") return false;
1960
+ return memberName(node.callee) === "createObjectURL" && isUrlObject(node.callee.object);
1961
+ }
1962
+ function constInitOf(identifier, scope) {
1963
+ let current = scope;
1964
+ while (current !== null) {
1965
+ const variable = current.set.get(identifier.name);
1966
+ if (variable !== void 0) {
1967
+ const def = variable.defs.length === 1 ? variable.defs[0] : void 0;
1968
+ const isConst = def !== void 0 && def.type === "Variable" && def.parent.kind === "const";
1969
+ return isConst ? unwrapExpression(def.node.init) : void 0;
1970
+ }
1971
+ current = current.upper;
1972
+ }
1973
+ return void 0;
1974
+ }
1975
+ function isNewFileReader(node) {
1976
+ return node !== void 0 && node !== null && node.type === "NewExpression" && node.callee.type === "Identifier" && node.callee.name === "FileReader";
1977
+ }
1978
+ function isFileReaderResult(node, scope) {
1979
+ if (node === void 0 || node === null || memberName(node) !== "result") return false;
1980
+ const object = unwrapExpression(node.object);
1981
+ if (isNewFileReader(object)) return true;
1982
+ return object.type === "Identifier" && isNewFileReader(constInitOf(object, scope));
1983
+ }
1984
+ function isLocalObjectUrl(node, scope) {
1985
+ const target = unwrapExpression(node);
1986
+ if (target === void 0 || target === null) return false;
1987
+ if (isCreateObjectUrlCall(target) || isFileReaderResult(target, scope)) return true;
1988
+ if (target.type !== "Identifier") return false;
1989
+ const init = constInitOf(target, scope);
1990
+ return isCreateObjectUrlCall(init) || isFileReaderResult(init, scope);
1991
+ }
1992
+ function scopeOf(context, node) {
1993
+ const sourceCode = context.sourceCode ?? context.getSourceCode();
1994
+ return typeof sourceCode.getScope === "function" ? sourceCode.getScope(node) : context.getScope();
1995
+ }
1996
+ function isExemptFetchTarget(context, node) {
1997
+ const target = node.arguments[0];
1998
+ if (isDataOrBlobUrl(target) || isThirdPartyLiteralUrl(target)) return true;
1999
+ return isLocalObjectUrl(target, scopeOf(context, node));
2000
+ }
2001
+ function isAxiosSource(node) {
2002
+ return node !== void 0 && node.type === "Literal" && node.value === AXIOS_MODULE;
2003
+ }
2004
+ function isTypeOnlyImport2(node) {
2005
+ if (node.importKind === "type") return true;
2006
+ const hasSpecifiers = node.specifiers.length > 0;
2007
+ return hasSpecifiers && node.specifiers.every((specifier) => specifier.importKind === "type");
2008
+ }
2009
+ var noRawBackendFetchRule = {
2010
+ meta: {
2011
+ type: "problem",
2012
+ docs: {
2013
+ description: "PAT-API-1: web apps call the backend only through the shared client or generated hooks, never raw fetch() or axios in app code.",
2014
+ category: "Best Practices",
2015
+ recommended: true
2016
+ },
2017
+ schema: [
2018
+ {
2019
+ type: "object",
2020
+ properties: {
2021
+ allow: { type: "array", items: { type: "string" } }
2022
+ },
2023
+ additionalProperties: false
2024
+ }
2025
+ ],
2026
+ messages: {
2027
+ rawFetch: "Raw fetch() in app code (PAT-API-1). Call the backend through the shared client or a generated API hook; only src/server/, src/http/, *-client packages, generated code and the service worker may call fetch.",
2028
+ rawAxios: "axios imported in app code (PAT-API-1). Use the shared client or a generated API hook; only src/server/, src/http/, *-client packages and generated code may import axios."
2029
+ }
2030
+ },
2031
+ create(context) {
2032
+ const options = context.options[0] ?? {};
2033
+ const allow = Array.isArray(options.allow) ? options.allow : [];
2034
+ const filename = context.filename ?? context.getFilename();
2035
+ if (isExemptFile(filename, allow)) return {};
2036
+ const sourceCode = context.sourceCode ?? context.getSourceCode();
2037
+ if (sourceCode.ast === void 0 || sourceCode.ast.type !== "Program") return {};
2038
+ return {
2039
+ CallExpression(node) {
2040
+ if (isFetchCallee(node.callee)) {
2041
+ if (!isExemptFetchTarget(context, node)) context.report({ node, messageId: "rawFetch" });
2042
+ return;
2043
+ }
2044
+ const isRequire = node.callee.type === "Identifier" && node.callee.name === "require";
2045
+ if (isRequire && isAxiosSource(node.arguments[0])) context.report({ node, messageId: "rawAxios" });
2046
+ },
2047
+ ImportExpression(node) {
2048
+ if (isAxiosSource(node.source)) context.report({ node, messageId: "rawAxios" });
2049
+ },
2050
+ ImportDeclaration(node) {
2051
+ if (isAxiosSource(node.source) && !isTypeOnlyImport2(node)) context.report({ node, messageId: "rawAxios" });
2052
+ }
2053
+ };
2054
+ }
2055
+ };
2056
+ var no_raw_backend_fetch_default = {
2057
+ rules: {
2058
+ "no-raw-backend-fetch": noRawBackendFetchRule
2059
+ }
2060
+ };
2061
+
1893
2062
  // src/rules/no-raw-color-literal.js
1894
2063
  var FULL_HEX_COLOR = /^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
1895
2064
  var FUNCTIONAL_COLOR = /\b(?:rgba?|hsla?)\s*\(/;
@@ -1997,8 +2166,8 @@ var preferConstEnumRule = {
1997
2166
  const exportPrefix = isExported ? "export " : "";
1998
2167
  const enumMembers = types.map((member) => {
1999
2168
  const value = member.literal.value;
2000
- const memberName = toPascalCase(value);
2001
- return ` ${memberName} = '${value}',`;
2169
+ const memberName2 = toPascalCase(value);
2170
+ return ` ${memberName2} = '${value}',`;
2002
2171
  }).join("\n");
2003
2172
  const replacement = `${exportPrefix}const enum ${typeName} {
2004
2173
  ${enumMembers}
@@ -2708,6 +2877,7 @@ var ruleModules = [
2708
2877
  no_null_check_default,
2709
2878
  no_optional_undefined_default,
2710
2879
  no_product_imports_in_shared_default,
2880
+ no_raw_backend_fetch_default,
2711
2881
  no_raw_color_literal_default,
2712
2882
  prefer_const_enum_default,
2713
2883
  require_stable_hook_args_default,
@@ -2778,11 +2948,18 @@ var gameConfig = {
2778
2948
  "@dloizides/game-menu-focusable": "error"
2779
2949
  }
2780
2950
  };
2951
+ var API_RULE_NAMES = ["no-raw-backend-fetch"];
2952
+ var apiConfig = {
2953
+ plugins: commentsConfig.plugins,
2954
+ rules: {
2955
+ "@dloizides/no-raw-backend-fetch": "error"
2956
+ }
2957
+ };
2781
2958
  var recommendedConfig = {
2782
2959
  plugins: commentsConfig.plugins,
2783
- rules: { ...commentsConfig.rules, ...rowActionsConfig.rules }
2960
+ rules: { ...commentsConfig.rules, ...rowActionsConfig.rules, ...apiConfig.rules }
2784
2961
  };
2785
2962
 
2786
- export { COMMENT_RULE_NAMES, GAME_RULE_NAMES, ROW_ACTION_RULE_NAMES, commentsConfig, gameConfig, legacyPlugins, plugin, recommendedConfig, rowActionsConfig, ruleNames, rules };
2963
+ export { API_RULE_NAMES, COMMENT_RULE_NAMES, GAME_RULE_NAMES, ROW_ACTION_RULE_NAMES, apiConfig, commentsConfig, gameConfig, legacyPlugins, plugin, recommendedConfig, rowActionsConfig, ruleNames, rules };
2787
2964
  //# sourceMappingURL=index.mjs.map
2788
2965
  //# sourceMappingURL=index.mjs.map