@colixsystems/widget-sdk 0.343.2 → 0.344.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/README.md +5 -1
- package/dist/contract.cjs +5 -1
- package/dist/contract.js +5 -1
- package/dist/linter.cjs +154 -0
- package/dist/linter.js +154 -0
- package/package.json +1 -1
- package/src/contract.js +5 -1
package/README.md
CHANGED
|
@@ -91,7 +91,11 @@ See the design reference for the full architecture: [`docs/architecture/widget-m
|
|
|
91
91
|
|
|
92
92
|
## Status
|
|
93
93
|
|
|
94
|
-
`v0.
|
|
94
|
+
`v0.344.0` — pre-publish. The package surface (types, function names, export paths) is the v1 contract; runtime behaviour for some hooks is stubbed (each hook documents what's wired and what isn't). It is **not yet published to npm**.
|
|
95
|
+
|
|
96
|
+
### What's new in 0.344.0 (contract 1.281.0)
|
|
97
|
+
|
|
98
|
+
**The linter flags a date shown with a fixed pattern or locale.** A date written with date-fns `format(d, "dd/MM/yyyy")`, `toLocaleDateString("en-GB")`, `new Intl.DateTimeFormat("de-DE")` or a string built from `getDate()` and `getMonth()` reads the same in every app language, and `03/01/2027` is a different day in another country. The new `date-shown-fixed-format` finding names `useI18n().formatDate(value)` as the fix, and `Intl.DateTimeFormat(useI18n().locale, …)` for part of a date. For date-fns it reads `format`, `lightFormat` and date-fns-tz `formatInTimeZone`, imported by name, by default (`date-fns/format`) or as a namespace (`import * as dateFns`). A pattern is flagged when it prints words (`EEE`, `MMM`, `MMMM`, `do`, the localized `P` tokens), which date-fns writes in English unless the call passes a `locale` other than one fixed `date-fns/locale` import, or when it writes a numeric day and month in any order but year-month-day. No finding for: a numeric year-month-day pattern (`yyyy-MM-dd`, `yyyyMMdd`), a date-fns pattern with no day and month and no words (`HH:mm`, `yyyy`), date math (`addDays`, `differenceInDays`), a locale taken from `useI18n().locale`, and a fixed year-first locale (`sv-SE`, `en-CA`, …) with no options or only numeric and `timeZone` options, the usual way to build a local `YYYY-MM-DD` key. Not read: `require("date-fns")` and the curried `date-fns/fp` formatters. It is a **warning**: `appstudio-widget lint` prints it and an uploaded widget still publishes, because the linter reads text and cannot always tell a shown date from a stored one. The in-Studio widget agent's publish gate refuses every lint finding, warnings included, so a widget it writes or edits is repaired before it ships: an edit that touches a widget with its own fixed-pattern date helper rewrites the helper to use `formatDate`. That repair holds even where a widget hard-coded Swedish dates in an app whose language is still English: the date then follows the app's language.
|
|
95
99
|
|
|
96
100
|
### What's new in 0.343.2 (contract 1.280.0)
|
|
97
101
|
|
package/dist/contract.cjs
CHANGED
|
@@ -7149,7 +7149,11 @@ const CONTRACT = deepFreeze({
|
|
|
7149
7149
|
// 1.280.0: additive (sc-18010) — connections.add and connections.signIn
|
|
7150
7150
|
// may reject SIGN_IN_NEEDS_SETUP (422): the server's sign-in needs the
|
|
7151
7151
|
// app's administrator to register it first. No token fixes it.
|
|
7152
|
-
|
|
7152
|
+
// 1.281.0: stricter lint (sc-18484) — a date shown with a fixed pattern or
|
|
7153
|
+
// locale (date-fns format(d, "dd/MM/yyyy"), toLocaleDateString("en-GB"),
|
|
7154
|
+
// a hand-built getDate()/getMonth() string) is the `date-shown-fixed-format`
|
|
7155
|
+
// warning; the widget agent's publish gate blocks on it.
|
|
7156
|
+
version: "1.281.0",
|
|
7153
7157
|
sharedTranslationKeys: SHARED_TRANSLATION_KEYS,
|
|
7154
7158
|
hooks: HOOKS,
|
|
7155
7159
|
primitives: PRIMITIVES,
|
package/dist/contract.js
CHANGED
|
@@ -7144,7 +7144,11 @@ const CONTRACT = deepFreeze({
|
|
|
7144
7144
|
// 1.280.0: additive (sc-18010) — connections.add and connections.signIn
|
|
7145
7145
|
// may reject SIGN_IN_NEEDS_SETUP (422): the server's sign-in needs the
|
|
7146
7146
|
// app's administrator to register it first. No token fixes it.
|
|
7147
|
-
|
|
7147
|
+
// 1.281.0: stricter lint (sc-18484) — a date shown with a fixed pattern or
|
|
7148
|
+
// locale (date-fns format(d, "dd/MM/yyyy"), toLocaleDateString("en-GB"),
|
|
7149
|
+
// a hand-built getDate()/getMonth() string) is the `date-shown-fixed-format`
|
|
7150
|
+
// warning; the widget agent's publish gate blocks on it.
|
|
7151
|
+
version: "1.281.0",
|
|
7148
7152
|
sharedTranslationKeys: SHARED_TRANSLATION_KEYS,
|
|
7149
7153
|
hooks: HOOKS,
|
|
7150
7154
|
primitives: PRIMITIVES,
|
package/dist/linter.cjs
CHANGED
|
@@ -1851,6 +1851,159 @@ function _hardcodedCurrencyLabelRules(source) {
|
|
|
1851
1851
|
}
|
|
1852
1852
|
|
|
1853
1853
|
|
|
1854
|
+
// Story 18484 — a date shown with a fixed pattern or locale reads the same in
|
|
1855
|
+
// every app language, and 03/01/2027 is another day abroad. A `warning`: the
|
|
1856
|
+
// agent's publish gate blocks on every lint finding, so the agent repairs it
|
|
1857
|
+
// there, while an uploaded widget ships with it (text alone cannot tell a
|
|
1858
|
+
// shown date from a stored one). A numeric year-month-day key is data, never flagged.
|
|
1859
|
+
const DATE_FNS_IMPORT_RE =
|
|
1860
|
+
/\bimport\s+(?:\*\s*as\s+([\w$]+)|([\w$]+)|\{([^{}]*)\})\s*from\s*["'](date-fns(?:-tz)?)(\/[\w/-]+)?["']/g;
|
|
1861
|
+
// The pattern's argument position, by formatter name.
|
|
1862
|
+
const DATE_FNS_FORMATTERS = new Map([["format", 1], ["lightFormat", 1], ["formatInTimeZone", 2]]);
|
|
1863
|
+
// Tokens that print words (weekday, month name, localized date, ordinal), English unless given a locale.
|
|
1864
|
+
const DATE_FNS_TEXT_TOKEN_RE = /E|M{3}|L{3}|P|do/;
|
|
1865
|
+
const FIXED_LOCALE_RE = /(\.toLocaleDateString|\bDateTimeFormat)\s*\(\s*(["'])([A-Za-z]{2,3}(?:-[A-Za-z0-9]{2,8})*)\2/g;
|
|
1866
|
+
// Locales whose numeric date is YYYY-MM-DD: the usual idiom for a local date key.
|
|
1867
|
+
const YEAR_FIRST_LOCALES = new Set(["sv", "sv-SE", "en-CA", "fr-CA", "lt", "lt-LT", "en-SE"]);
|
|
1868
|
+
const LOCALE_OPTIONS_RE = /^\s*,\s*\{([^{}]{0,300})\}/;
|
|
1869
|
+
const TEXT_DATE_OPTION_RE = /\bweekday\b|\bera\b|\bmonth\s*:\s*["'](?:long|short|narrow)|\bdateStyle\s*:\s*["'](?:full|long|medium)/;
|
|
1870
|
+
const LOCALE_OPTION_RE = /\blocale\b\s*(?::\s*([\w$]+)(\s*\.)?)?/;
|
|
1871
|
+
const DAY_GETTER_RE = /\.get(?:UTC)?Date\s*\(\s*\)/;
|
|
1872
|
+
const MONTH_GETTER_RE = /\.get(?:UTC)?Month\s*\(\s*\)/;
|
|
1873
|
+
const YEAR_GETTER_RE = /\.get(?:UTC)?FullYear\s*\(\s*\)/;
|
|
1874
|
+
const STRING_BUILD_RE = /\$\{|["'`]\s*\+|\+\s*["'`]|\]\s*\.join\s*\(/;
|
|
1875
|
+
// How far a formatter call's arguments may run.
|
|
1876
|
+
const DATE_ARG_SPAN = 400;
|
|
1877
|
+
|
|
1878
|
+
const CALL_RE = /(?<![\w$.])([\w$]+)\s*\(/g;
|
|
1879
|
+
const MEMBER_CALL_RE = /(?<![\w$.])([\w$]+)\s*\.\s*([\w$]+)\s*\(/g;
|
|
1880
|
+
|
|
1881
|
+
/** date-fns formatter names and namespaces, and the locale objects it was given. */
|
|
1882
|
+
function _dateFnsImports(strings) {
|
|
1883
|
+
const imports = { formatters: new Map(), namespaces: new Set(), locales: new Set(), localeNamespaces: new Set() };
|
|
1884
|
+
for (const [, ns, deflt, named, , subpath = ""] of strings.matchAll(DATE_FNS_IMPORT_RE)) {
|
|
1885
|
+
if (subpath.startsWith("/locale")) {
|
|
1886
|
+
if (ns) imports.localeNamespaces.add(ns);
|
|
1887
|
+
if (deflt) imports.locales.add(deflt);
|
|
1888
|
+
for (const spec of (named || "").split(",")) {
|
|
1889
|
+
const [imported, local] = spec.trim().split(/\s+as\s+/);
|
|
1890
|
+
if (imported) imports.locales.add(local || imported);
|
|
1891
|
+
}
|
|
1892
|
+
continue;
|
|
1893
|
+
}
|
|
1894
|
+
if (ns && !subpath) imports.namespaces.add(ns);
|
|
1895
|
+
if (deflt && DATE_FNS_FORMATTERS.has(subpath.slice(1))) imports.formatters.set(deflt, DATE_FNS_FORMATTERS.get(subpath.slice(1)));
|
|
1896
|
+
for (const spec of (named || "").split(",")) {
|
|
1897
|
+
const [imported, local] = spec.trim().split(/\s+as\s+/);
|
|
1898
|
+
if (DATE_FNS_FORMATTERS.has(imported)) imports.formatters.set(local || imported, DATE_FNS_FORMATTERS.get(imported));
|
|
1899
|
+
}
|
|
1900
|
+
}
|
|
1901
|
+
return imports;
|
|
1902
|
+
}
|
|
1903
|
+
|
|
1904
|
+
/** Where each top-level argument of a call starts, and where the call ends. */
|
|
1905
|
+
function _callArgs(code, openParen) {
|
|
1906
|
+
const limit = Math.min(code.length, openParen + DATE_ARG_SPAN);
|
|
1907
|
+
const starts = [openParen + 1];
|
|
1908
|
+
let depth = 0;
|
|
1909
|
+
for (let i = openParen + 1; i < limit; i += 1) {
|
|
1910
|
+
const ch = code[i];
|
|
1911
|
+
if (ch === "(" || ch === "[" || ch === "{") depth += 1;
|
|
1912
|
+
else if (ch === ")" || ch === "]" || ch === "}") {
|
|
1913
|
+
if (depth === 0) return { starts, end: i };
|
|
1914
|
+
depth -= 1;
|
|
1915
|
+
} else if (ch === "," && depth === 0) starts.push(i + 1);
|
|
1916
|
+
}
|
|
1917
|
+
return { starts, end: limit };
|
|
1918
|
+
}
|
|
1919
|
+
|
|
1920
|
+
const _stringLiteral = (text) => /^\s*(["'`])((?:(?!\1)[^$\\]){1,80})\1/.exec(text)?.[2] ?? null;
|
|
1921
|
+
|
|
1922
|
+
/** True when the call's options pass a locale that is not one fixed date-fns locale. */
|
|
1923
|
+
function _appLocaleOption(options, imports) {
|
|
1924
|
+
const m = LOCALE_OPTION_RE.exec(options);
|
|
1925
|
+
if (!m) return false;
|
|
1926
|
+
if (!m[1]) return true;
|
|
1927
|
+
return !(imports.locales.has(m[1]) || (imports.localeNamespaces.has(m[1]) && m[2]));
|
|
1928
|
+
}
|
|
1929
|
+
|
|
1930
|
+
/** A date-fns pattern that prints words without the app's locale, or a day and month not year-first. */
|
|
1931
|
+
function _fixedDatePattern(pattern, appLocale) {
|
|
1932
|
+
const tokens = pattern.replace(/'[^']*'/g, "");
|
|
1933
|
+
if (DATE_FNS_TEXT_TOKEN_RE.test(tokens)) return !appLocale;
|
|
1934
|
+
const day = tokens.indexOf("d");
|
|
1935
|
+
const month = tokens.search(/[ML]/);
|
|
1936
|
+
if (day < 0 || month < 0) return false;
|
|
1937
|
+
const year = tokens.indexOf("y");
|
|
1938
|
+
return !(year >= 0 && year < month && month < day);
|
|
1939
|
+
}
|
|
1940
|
+
|
|
1941
|
+
/** A literal locale, unless it writes a numeric year-first date (a key) with no words. */
|
|
1942
|
+
function _fixedLocaleCall(strings, locale, after) {
|
|
1943
|
+
if (!YEAR_FIRST_LOCALES.has(locale)) return true;
|
|
1944
|
+
const options = LOCALE_OPTIONS_RE.exec(strings.slice(after, after + DATE_ARG_SPAN));
|
|
1945
|
+
return !!options && TEXT_DATE_OPTION_RE.test(options[1]);
|
|
1946
|
+
}
|
|
1947
|
+
|
|
1948
|
+
function _handBuiltDateLine(codeLine, stringsLine) {
|
|
1949
|
+
const day = codeLine.search(DAY_GETTER_RE);
|
|
1950
|
+
const month = codeLine.search(MONTH_GETTER_RE);
|
|
1951
|
+
if (day < 0 || month < 0 || !STRING_BUILD_RE.test(stringsLine)) return false;
|
|
1952
|
+
const year = codeLine.search(YEAR_GETTER_RE);
|
|
1953
|
+
return day < month || year > day;
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
function _fixedDateFormatRules(source) {
|
|
1957
|
+
const code = _stripNonCode(source);
|
|
1958
|
+
const strings = _stripNonCode(source, { keepStrings: true });
|
|
1959
|
+
const lineAt = _lineCounter(code);
|
|
1960
|
+
const shown = new Map();
|
|
1961
|
+
const report = (index, what) => {
|
|
1962
|
+
const line = lineAt(index);
|
|
1963
|
+
if (!shown.has(line)) shown.set(line, what);
|
|
1964
|
+
};
|
|
1965
|
+
const imports = _dateFnsImports(strings);
|
|
1966
|
+
const checkCall = (m, patternArg) => {
|
|
1967
|
+
const { starts, end } = _callArgs(code, m.index + m[0].length - 1);
|
|
1968
|
+
if (starts.length <= patternArg) return;
|
|
1969
|
+
const pattern = _stringLiteral(strings.slice(starts[patternArg], end));
|
|
1970
|
+
const options = starts.length > patternArg + 1 ? code.slice(starts[patternArg + 1], end) : "";
|
|
1971
|
+
if (pattern && _fixedDatePattern(pattern, _appLocaleOption(options, imports))) report(m.index, `pattern "${pattern}"`);
|
|
1972
|
+
};
|
|
1973
|
+
if (imports.formatters.size > 0) {
|
|
1974
|
+
for (const m of code.matchAll(CALL_RE)) {
|
|
1975
|
+
if (imports.formatters.has(m[1])) checkCall(m, imports.formatters.get(m[1]));
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
if (imports.namespaces.size > 0) {
|
|
1979
|
+
for (const m of code.matchAll(MEMBER_CALL_RE)) {
|
|
1980
|
+
if (imports.namespaces.has(m[1]) && DATE_FNS_FORMATTERS.has(m[2])) checkCall(m, DATE_FNS_FORMATTERS.get(m[2]));
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
for (const m of strings.matchAll(FIXED_LOCALE_RE)) {
|
|
1984
|
+
if (code.startsWith(m[1], m.index) && _fixedLocaleCall(strings, m[3], m.index + m[0].length)) {
|
|
1985
|
+
report(m.index, `locale "${m[3]}"`);
|
|
1986
|
+
}
|
|
1987
|
+
}
|
|
1988
|
+
const codeLines = code.split("\n");
|
|
1989
|
+
const stringLines = strings.split("\n");
|
|
1990
|
+
let offset = 0;
|
|
1991
|
+
codeLines.forEach((codeLine, i) => {
|
|
1992
|
+
if (_handBuiltDateLine(codeLine, stringLines[i])) report(offset, "day/month order built by hand");
|
|
1993
|
+
offset += codeLine.length + 1;
|
|
1994
|
+
});
|
|
1995
|
+
const lines = source.split(/\r?\n/);
|
|
1996
|
+
return [...shown].sort((a, b) => a[0] - b[0]).map(([line, what]) => ({
|
|
1997
|
+
rule: "date-shown-fixed-format",
|
|
1998
|
+
severity: "warning",
|
|
1999
|
+
label:
|
|
2000
|
+
`shows a date with a fixed ${what}, the same in every app language. Use useI18n().formatDate(value) ` +
|
|
2001
|
+
`({ time: true } adds the time); for part of a date, Intl.DateTimeFormat(useI18n().locale, …). date-fns is for date math.`,
|
|
2002
|
+
line,
|
|
2003
|
+
snippet: (lines[line - 1] || "").trim().slice(0, 200),
|
|
2004
|
+
}));
|
|
2005
|
+
}
|
|
2006
|
+
|
|
1854
2007
|
// sc-4650 — soft warning: a widget that charges must tell a failure worth
|
|
1855
2008
|
// retrying from a refusal only the workspace owner can lift. Collapsing every
|
|
1856
2009
|
// rejection into one "please try again" is what sent payers round an
|
|
@@ -4627,6 +4780,7 @@ function lintSource(source, options) {
|
|
|
4627
4780
|
findings.push(..._paymentKindRules(source));
|
|
4628
4781
|
findings.push(..._paymentStatusWrittenRules(source));
|
|
4629
4782
|
findings.push(..._hardcodedCurrencyLabelRules(source));
|
|
4783
|
+
findings.push(..._fixedDateFormatRules(source));
|
|
4630
4784
|
findings.push(..._paymentErrorHandlingRules(source));
|
|
4631
4785
|
findings.push(..._datastoreErrorHandlingRules(source));
|
|
4632
4786
|
findings.push(..._readErrorHandlingRules(source));
|
package/dist/linter.js
CHANGED
|
@@ -1846,6 +1846,159 @@ function _hardcodedCurrencyLabelRules(source) {
|
|
|
1846
1846
|
}
|
|
1847
1847
|
|
|
1848
1848
|
|
|
1849
|
+
// Story 18484 — a date shown with a fixed pattern or locale reads the same in
|
|
1850
|
+
// every app language, and 03/01/2027 is another day abroad. A `warning`: the
|
|
1851
|
+
// agent's publish gate blocks on every lint finding, so the agent repairs it
|
|
1852
|
+
// there, while an uploaded widget ships with it (text alone cannot tell a
|
|
1853
|
+
// shown date from a stored one). A numeric year-month-day key is data, never flagged.
|
|
1854
|
+
const DATE_FNS_IMPORT_RE =
|
|
1855
|
+
/\bimport\s+(?:\*\s*as\s+([\w$]+)|([\w$]+)|\{([^{}]*)\})\s*from\s*["'](date-fns(?:-tz)?)(\/[\w/-]+)?["']/g;
|
|
1856
|
+
// The pattern's argument position, by formatter name.
|
|
1857
|
+
const DATE_FNS_FORMATTERS = new Map([["format", 1], ["lightFormat", 1], ["formatInTimeZone", 2]]);
|
|
1858
|
+
// Tokens that print words (weekday, month name, localized date, ordinal), English unless given a locale.
|
|
1859
|
+
const DATE_FNS_TEXT_TOKEN_RE = /E|M{3}|L{3}|P|do/;
|
|
1860
|
+
const FIXED_LOCALE_RE = /(\.toLocaleDateString|\bDateTimeFormat)\s*\(\s*(["'])([A-Za-z]{2,3}(?:-[A-Za-z0-9]{2,8})*)\2/g;
|
|
1861
|
+
// Locales whose numeric date is YYYY-MM-DD: the usual idiom for a local date key.
|
|
1862
|
+
const YEAR_FIRST_LOCALES = new Set(["sv", "sv-SE", "en-CA", "fr-CA", "lt", "lt-LT", "en-SE"]);
|
|
1863
|
+
const LOCALE_OPTIONS_RE = /^\s*,\s*\{([^{}]{0,300})\}/;
|
|
1864
|
+
const TEXT_DATE_OPTION_RE = /\bweekday\b|\bera\b|\bmonth\s*:\s*["'](?:long|short|narrow)|\bdateStyle\s*:\s*["'](?:full|long|medium)/;
|
|
1865
|
+
const LOCALE_OPTION_RE = /\blocale\b\s*(?::\s*([\w$]+)(\s*\.)?)?/;
|
|
1866
|
+
const DAY_GETTER_RE = /\.get(?:UTC)?Date\s*\(\s*\)/;
|
|
1867
|
+
const MONTH_GETTER_RE = /\.get(?:UTC)?Month\s*\(\s*\)/;
|
|
1868
|
+
const YEAR_GETTER_RE = /\.get(?:UTC)?FullYear\s*\(\s*\)/;
|
|
1869
|
+
const STRING_BUILD_RE = /\$\{|["'`]\s*\+|\+\s*["'`]|\]\s*\.join\s*\(/;
|
|
1870
|
+
// How far a formatter call's arguments may run.
|
|
1871
|
+
const DATE_ARG_SPAN = 400;
|
|
1872
|
+
|
|
1873
|
+
const CALL_RE = /(?<![\w$.])([\w$]+)\s*\(/g;
|
|
1874
|
+
const MEMBER_CALL_RE = /(?<![\w$.])([\w$]+)\s*\.\s*([\w$]+)\s*\(/g;
|
|
1875
|
+
|
|
1876
|
+
/** date-fns formatter names and namespaces, and the locale objects it was given. */
|
|
1877
|
+
function _dateFnsImports(strings) {
|
|
1878
|
+
const imports = { formatters: new Map(), namespaces: new Set(), locales: new Set(), localeNamespaces: new Set() };
|
|
1879
|
+
for (const [, ns, deflt, named, , subpath = ""] of strings.matchAll(DATE_FNS_IMPORT_RE)) {
|
|
1880
|
+
if (subpath.startsWith("/locale")) {
|
|
1881
|
+
if (ns) imports.localeNamespaces.add(ns);
|
|
1882
|
+
if (deflt) imports.locales.add(deflt);
|
|
1883
|
+
for (const spec of (named || "").split(",")) {
|
|
1884
|
+
const [imported, local] = spec.trim().split(/\s+as\s+/);
|
|
1885
|
+
if (imported) imports.locales.add(local || imported);
|
|
1886
|
+
}
|
|
1887
|
+
continue;
|
|
1888
|
+
}
|
|
1889
|
+
if (ns && !subpath) imports.namespaces.add(ns);
|
|
1890
|
+
if (deflt && DATE_FNS_FORMATTERS.has(subpath.slice(1))) imports.formatters.set(deflt, DATE_FNS_FORMATTERS.get(subpath.slice(1)));
|
|
1891
|
+
for (const spec of (named || "").split(",")) {
|
|
1892
|
+
const [imported, local] = spec.trim().split(/\s+as\s+/);
|
|
1893
|
+
if (DATE_FNS_FORMATTERS.has(imported)) imports.formatters.set(local || imported, DATE_FNS_FORMATTERS.get(imported));
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
return imports;
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1899
|
+
/** Where each top-level argument of a call starts, and where the call ends. */
|
|
1900
|
+
function _callArgs(code, openParen) {
|
|
1901
|
+
const limit = Math.min(code.length, openParen + DATE_ARG_SPAN);
|
|
1902
|
+
const starts = [openParen + 1];
|
|
1903
|
+
let depth = 0;
|
|
1904
|
+
for (let i = openParen + 1; i < limit; i += 1) {
|
|
1905
|
+
const ch = code[i];
|
|
1906
|
+
if (ch === "(" || ch === "[" || ch === "{") depth += 1;
|
|
1907
|
+
else if (ch === ")" || ch === "]" || ch === "}") {
|
|
1908
|
+
if (depth === 0) return { starts, end: i };
|
|
1909
|
+
depth -= 1;
|
|
1910
|
+
} else if (ch === "," && depth === 0) starts.push(i + 1);
|
|
1911
|
+
}
|
|
1912
|
+
return { starts, end: limit };
|
|
1913
|
+
}
|
|
1914
|
+
|
|
1915
|
+
const _stringLiteral = (text) => /^\s*(["'`])((?:(?!\1)[^$\\]){1,80})\1/.exec(text)?.[2] ?? null;
|
|
1916
|
+
|
|
1917
|
+
/** True when the call's options pass a locale that is not one fixed date-fns locale. */
|
|
1918
|
+
function _appLocaleOption(options, imports) {
|
|
1919
|
+
const m = LOCALE_OPTION_RE.exec(options);
|
|
1920
|
+
if (!m) return false;
|
|
1921
|
+
if (!m[1]) return true;
|
|
1922
|
+
return !(imports.locales.has(m[1]) || (imports.localeNamespaces.has(m[1]) && m[2]));
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1925
|
+
/** A date-fns pattern that prints words without the app's locale, or a day and month not year-first. */
|
|
1926
|
+
function _fixedDatePattern(pattern, appLocale) {
|
|
1927
|
+
const tokens = pattern.replace(/'[^']*'/g, "");
|
|
1928
|
+
if (DATE_FNS_TEXT_TOKEN_RE.test(tokens)) return !appLocale;
|
|
1929
|
+
const day = tokens.indexOf("d");
|
|
1930
|
+
const month = tokens.search(/[ML]/);
|
|
1931
|
+
if (day < 0 || month < 0) return false;
|
|
1932
|
+
const year = tokens.indexOf("y");
|
|
1933
|
+
return !(year >= 0 && year < month && month < day);
|
|
1934
|
+
}
|
|
1935
|
+
|
|
1936
|
+
/** A literal locale, unless it writes a numeric year-first date (a key) with no words. */
|
|
1937
|
+
function _fixedLocaleCall(strings, locale, after) {
|
|
1938
|
+
if (!YEAR_FIRST_LOCALES.has(locale)) return true;
|
|
1939
|
+
const options = LOCALE_OPTIONS_RE.exec(strings.slice(after, after + DATE_ARG_SPAN));
|
|
1940
|
+
return !!options && TEXT_DATE_OPTION_RE.test(options[1]);
|
|
1941
|
+
}
|
|
1942
|
+
|
|
1943
|
+
function _handBuiltDateLine(codeLine, stringsLine) {
|
|
1944
|
+
const day = codeLine.search(DAY_GETTER_RE);
|
|
1945
|
+
const month = codeLine.search(MONTH_GETTER_RE);
|
|
1946
|
+
if (day < 0 || month < 0 || !STRING_BUILD_RE.test(stringsLine)) return false;
|
|
1947
|
+
const year = codeLine.search(YEAR_GETTER_RE);
|
|
1948
|
+
return day < month || year > day;
|
|
1949
|
+
}
|
|
1950
|
+
|
|
1951
|
+
function _fixedDateFormatRules(source) {
|
|
1952
|
+
const code = _stripNonCode(source);
|
|
1953
|
+
const strings = _stripNonCode(source, { keepStrings: true });
|
|
1954
|
+
const lineAt = _lineCounter(code);
|
|
1955
|
+
const shown = new Map();
|
|
1956
|
+
const report = (index, what) => {
|
|
1957
|
+
const line = lineAt(index);
|
|
1958
|
+
if (!shown.has(line)) shown.set(line, what);
|
|
1959
|
+
};
|
|
1960
|
+
const imports = _dateFnsImports(strings);
|
|
1961
|
+
const checkCall = (m, patternArg) => {
|
|
1962
|
+
const { starts, end } = _callArgs(code, m.index + m[0].length - 1);
|
|
1963
|
+
if (starts.length <= patternArg) return;
|
|
1964
|
+
const pattern = _stringLiteral(strings.slice(starts[patternArg], end));
|
|
1965
|
+
const options = starts.length > patternArg + 1 ? code.slice(starts[patternArg + 1], end) : "";
|
|
1966
|
+
if (pattern && _fixedDatePattern(pattern, _appLocaleOption(options, imports))) report(m.index, `pattern "${pattern}"`);
|
|
1967
|
+
};
|
|
1968
|
+
if (imports.formatters.size > 0) {
|
|
1969
|
+
for (const m of code.matchAll(CALL_RE)) {
|
|
1970
|
+
if (imports.formatters.has(m[1])) checkCall(m, imports.formatters.get(m[1]));
|
|
1971
|
+
}
|
|
1972
|
+
}
|
|
1973
|
+
if (imports.namespaces.size > 0) {
|
|
1974
|
+
for (const m of code.matchAll(MEMBER_CALL_RE)) {
|
|
1975
|
+
if (imports.namespaces.has(m[1]) && DATE_FNS_FORMATTERS.has(m[2])) checkCall(m, DATE_FNS_FORMATTERS.get(m[2]));
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
for (const m of strings.matchAll(FIXED_LOCALE_RE)) {
|
|
1979
|
+
if (code.startsWith(m[1], m.index) && _fixedLocaleCall(strings, m[3], m.index + m[0].length)) {
|
|
1980
|
+
report(m.index, `locale "${m[3]}"`);
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
const codeLines = code.split("\n");
|
|
1984
|
+
const stringLines = strings.split("\n");
|
|
1985
|
+
let offset = 0;
|
|
1986
|
+
codeLines.forEach((codeLine, i) => {
|
|
1987
|
+
if (_handBuiltDateLine(codeLine, stringLines[i])) report(offset, "day/month order built by hand");
|
|
1988
|
+
offset += codeLine.length + 1;
|
|
1989
|
+
});
|
|
1990
|
+
const lines = source.split(/\r?\n/);
|
|
1991
|
+
return [...shown].sort((a, b) => a[0] - b[0]).map(([line, what]) => ({
|
|
1992
|
+
rule: "date-shown-fixed-format",
|
|
1993
|
+
severity: "warning",
|
|
1994
|
+
label:
|
|
1995
|
+
`shows a date with a fixed ${what}, the same in every app language. Use useI18n().formatDate(value) ` +
|
|
1996
|
+
`({ time: true } adds the time); for part of a date, Intl.DateTimeFormat(useI18n().locale, …). date-fns is for date math.`,
|
|
1997
|
+
line,
|
|
1998
|
+
snippet: (lines[line - 1] || "").trim().slice(0, 200),
|
|
1999
|
+
}));
|
|
2000
|
+
}
|
|
2001
|
+
|
|
1849
2002
|
// sc-4650 — soft warning: a widget that charges must tell a failure worth
|
|
1850
2003
|
// retrying from a refusal only the workspace owner can lift. Collapsing every
|
|
1851
2004
|
// rejection into one "please try again" is what sent payers round an
|
|
@@ -4622,6 +4775,7 @@ export function lintSource(source, options) {
|
|
|
4622
4775
|
findings.push(..._paymentKindRules(source));
|
|
4623
4776
|
findings.push(..._paymentStatusWrittenRules(source));
|
|
4624
4777
|
findings.push(..._hardcodedCurrencyLabelRules(source));
|
|
4778
|
+
findings.push(..._fixedDateFormatRules(source));
|
|
4625
4779
|
findings.push(..._paymentErrorHandlingRules(source));
|
|
4626
4780
|
findings.push(..._datastoreErrorHandlingRules(source));
|
|
4627
4781
|
findings.push(..._readErrorHandlingRules(source));
|
package/package.json
CHANGED
package/src/contract.js
CHANGED
|
@@ -7144,7 +7144,11 @@ const CONTRACT = deepFreeze({
|
|
|
7144
7144
|
// 1.280.0: additive (sc-18010) — connections.add and connections.signIn
|
|
7145
7145
|
// may reject SIGN_IN_NEEDS_SETUP (422): the server's sign-in needs the
|
|
7146
7146
|
// app's administrator to register it first. No token fixes it.
|
|
7147
|
-
|
|
7147
|
+
// 1.281.0: stricter lint (sc-18484) — a date shown with a fixed pattern or
|
|
7148
|
+
// locale (date-fns format(d, "dd/MM/yyyy"), toLocaleDateString("en-GB"),
|
|
7149
|
+
// a hand-built getDate()/getMonth() string) is the `date-shown-fixed-format`
|
|
7150
|
+
// warning; the widget agent's publish gate blocks on it.
|
|
7151
|
+
version: "1.281.0",
|
|
7148
7152
|
sharedTranslationKeys: SHARED_TRANSLATION_KEYS,
|
|
7149
7153
|
hooks: HOOKS,
|
|
7150
7154
|
primitives: PRIMITIVES,
|