eslint-plugin-formatjs 8.0.2 → 8.0.4
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/index.js +14 -10
- package/index.js.map +1 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -63,7 +63,7 @@ function messageTypes(ast, module, ignoreList = []) {
|
|
|
63
63
|
else type = `import(${JSON.stringify(module)}).MessageTag`;
|
|
64
64
|
return `readonly ${JSON.stringify(name)}${ignored.has(name) ? "?" : ""}: ${type}`;
|
|
65
65
|
});
|
|
66
|
-
return fields.length ? `{ ${fields.join("; ")} }` :
|
|
66
|
+
return fields.length ? `{ ${fields.join("; ")} }` : `import(${JSON.stringify(module)}).NoMessageValues`;
|
|
67
67
|
}
|
|
68
68
|
//#endregion
|
|
69
69
|
//#region packages/eslint-plugin-formatjs/util.ts
|
|
@@ -538,7 +538,7 @@ function hoistTypes(context, node, contract) {
|
|
|
538
538
|
const occupied = new Set(source.scopeManager?.scopes.flatMap((scope) => scope.variables.map((variable) => variable.name)));
|
|
539
539
|
const resolved = /* @__PURE__ */ new Map();
|
|
540
540
|
return {
|
|
541
|
-
text: contract.replace(/import\(("(?:[^"\\]|\\.)*")\)\.(MessageTag|MessageValuesOf|MessageValue|TypedMessageDescriptor)/g, (reference, quotedModule, imported) => {
|
|
541
|
+
text: contract.replace(/import\(("(?:[^"\\]|\\.)*")\)\.(MessageTag|MessageValuesOf|MessageValue|NoMessageValues|TypedMessageDescriptor)/g, (reference, quotedModule, imported) => {
|
|
542
542
|
if (resolved.has(reference)) return resolved.get(reference);
|
|
543
543
|
const module = JSON.parse(quotedModule);
|
|
544
544
|
for (const declaration of source.ast.body) {
|
|
@@ -762,10 +762,11 @@ function checkInlineMessage(context, node) {
|
|
|
762
762
|
});
|
|
763
763
|
return true;
|
|
764
764
|
}
|
|
765
|
+
const emptyContract = contract === `import(${JSON.stringify(module)}).NoMessageValues`;
|
|
765
766
|
const imports = hoistTypes(context, node, contract);
|
|
766
767
|
contract = imports.text;
|
|
767
768
|
const parameters = generic?.params;
|
|
768
|
-
if (
|
|
769
|
+
if (emptyContract && node.arguments.length === 1) {
|
|
769
770
|
if (!generic) return true;
|
|
770
771
|
if (parameters?.length === 1) {
|
|
771
772
|
context.report({
|
|
@@ -974,8 +975,9 @@ const rule$20 = {
|
|
|
974
975
|
}
|
|
975
976
|
let contract;
|
|
976
977
|
let catalogType;
|
|
978
|
+
const emptyContract = `import(${JSON.stringify(imported.module)}).NoMessageValues`;
|
|
977
979
|
try {
|
|
978
|
-
if (annotation) catalogType = !catalog ? "import(" + JSON.stringify(imported.module) + ").TypedMessageDescriptor<" + types[0] + ">" : entries.length ? "{ " + entries.map((entry, index) => "readonly " + entry.key + ": import(" + JSON.stringify(imported.module) + ").TypedMessageDescriptor<" + types[index] + ">").join("; ") + " }" : "{}";
|
|
980
|
+
if (annotation) catalogType = !catalog ? "import(" + JSON.stringify(imported.module) + ").TypedMessageDescriptor" + (types[0] === emptyContract ? "" : "<" + types[0] + ">") : entries.length ? "{ " + entries.map((entry, index) => "readonly " + entry.key + ": import(" + JSON.stringify(imported.module) + ").TypedMessageDescriptor" + (types[index] === emptyContract ? "" : "<" + types[index] + ">")).join("; ") + " }" : "{}";
|
|
979
981
|
contract = !catalog ? types[0] : entries.length ? "{ " + entries.map((entry, index) => "readonly " + entry.key + ": " + types[index]).join("; ") + " }" : "{}";
|
|
980
982
|
} catch (error) {
|
|
981
983
|
context.report({
|
|
@@ -985,12 +987,14 @@ const rule$20 = {
|
|
|
985
987
|
});
|
|
986
988
|
return;
|
|
987
989
|
}
|
|
988
|
-
const
|
|
990
|
+
const parameters = generic?.params;
|
|
991
|
+
const omitContract = (imported.helper === "defineMessage" || catalog) && types.every((type) => type === emptyContract) && (!parameters || parameters.length === 1);
|
|
992
|
+
const imports = hoistTypes(context, node, (omitContract ? "" : contract) + (catalogType ? "\n" + catalogType : ""));
|
|
989
993
|
[contract, catalogType] = imports.text.split("\n");
|
|
990
994
|
const annotationMatches = !annotation || annotation.annotation === annotation.generated && renderType(annotation.generated) === catalogType;
|
|
991
|
-
const expected = `<${contract}>`;
|
|
992
|
-
|
|
993
|
-
if (parameters && parameters.length >= 1 && parameters.length <= 2 && renderType(parameters[0]) === contract && !generated && annotationMatches) return;
|
|
995
|
+
const expected = omitContract ? "" : `<${contract}>`;
|
|
996
|
+
if (omitContract && !generic && annotationMatches) return;
|
|
997
|
+
if (!omitContract && parameters && parameters.length >= 1 && parameters.length <= 2 && renderType(parameters[0]) === contract && !generated && annotationMatches) return;
|
|
994
998
|
if (generic && (!parameters || parameters.length < 1 || parameters.length > 2)) {
|
|
995
999
|
context.report({
|
|
996
1000
|
node: generic,
|
|
@@ -1005,7 +1009,7 @@ const rule$20 = {
|
|
|
1005
1009
|
const edits = [
|
|
1006
1010
|
...imports.fix(fixer),
|
|
1007
1011
|
...obsoleteTypes(context, [...parameters?.[0] ? [parameters[0]] : [], ...annotation && !annotationMatches ? [annotation.annotation] : []], contract + (catalogType ?? ""), fixer),
|
|
1008
|
-
generic && parameters?.[0] ? fixer.replaceTextRange([generic.range[0] + 1, parameters[0].range[1]], contract) : fixer.insertTextAfter(node.optional ? source.getTokenAfter(node.callee) : node.callee, expected)
|
|
1012
|
+
omitContract && generic ? fixer.remove(generic) : generic && parameters?.[0] ? fixer.replaceTextRange([generic.range[0] + 1, parameters[0].range[1]], contract) : fixer.insertTextAfter(node.optional ? source.getTokenAfter(node.callee) : node.callee, expected)
|
|
1009
1013
|
];
|
|
1010
1014
|
if (annotation && !annotationMatches) edits.push(fixer.replaceText(annotation.annotation, catalogType));
|
|
1011
1015
|
return edits;
|
|
@@ -5265,7 +5269,7 @@ var package_exports = /* @__PURE__ */ __exportAll({
|
|
|
5265
5269
|
version: () => version$1
|
|
5266
5270
|
});
|
|
5267
5271
|
var name$1 = "eslint-plugin-formatjs";
|
|
5268
|
-
var version$1 = "8.0.
|
|
5272
|
+
var version$1 = "8.0.4";
|
|
5269
5273
|
var description = "ESLint plugin for formatjs";
|
|
5270
5274
|
var keywords = [
|
|
5271
5275
|
"eslint",
|