eslint-plugin-formatjs 8.0.1 → 8.0.3
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 +15 -11
- package/index.js.map +1 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -639,7 +639,7 @@ function obsoleteTypes(context, replaced, replacement, fixer) {
|
|
|
639
639
|
}
|
|
640
640
|
return edits;
|
|
641
641
|
}
|
|
642
|
-
function
|
|
642
|
+
function messageAnnotation(context, node, catalog) {
|
|
643
643
|
const parent = node.parent;
|
|
644
644
|
if (parent?.type !== "VariableDeclarator" || parent.init !== node) return;
|
|
645
645
|
const annotation = parent.id.typeAnnotation?.typeAnnotation;
|
|
@@ -663,7 +663,8 @@ function catalogAnnotation(context, node) {
|
|
|
663
663
|
function broad(type, seen = /* @__PURE__ */ new Set()) {
|
|
664
664
|
if (seen.has(type)) return false;
|
|
665
665
|
seen.add(type);
|
|
666
|
-
if (
|
|
666
|
+
if (!catalog && imported(type, "MessageDescriptor")) return true;
|
|
667
|
+
if (type.type === "TSTypeLiteral") return catalog && !!type.members?.every((member) => member.type === "TSPropertySignature" && !!member.typeAnnotation && descriptorType(member.typeAnnotation.typeAnnotation));
|
|
667
668
|
let scope = context.sourceCode.getScope(type);
|
|
668
669
|
while (scope) {
|
|
669
670
|
const binding = scope.set.get(type.typeName?.name ?? "");
|
|
@@ -675,14 +676,15 @@ function catalogAnnotation(context, node) {
|
|
|
675
676
|
}
|
|
676
677
|
const parameters = (type.typeArguments ?? type.typeParameters)?.params;
|
|
677
678
|
if (type.typeName?.name === "Readonly" && parameters?.length === 1) return broad(parameters[0], seen);
|
|
678
|
-
return type.typeName?.name === "Record" && parameters?.length === 2 && descriptorType(parameters[1]);
|
|
679
|
+
return catalog && type.typeName?.name === "Record" && parameters?.length === 2 && descriptorType(parameters[1]);
|
|
679
680
|
}
|
|
680
681
|
function typedMap(type) {
|
|
682
|
+
if (!catalog) return imported(type, "TypedMessageDescriptor");
|
|
681
683
|
return type.type === "TSTypeLiteral" && !!type.members?.every((member) => member.type === "TSPropertySignature" && !!member.typeAnnotation && imported(member.typeAnnotation.typeAnnotation, "TypedMessageDescriptor"));
|
|
682
684
|
}
|
|
683
685
|
let base = annotation;
|
|
684
686
|
let generated = typedMap(annotation) ? annotation : void 0;
|
|
685
|
-
if (annotation.type === "TSIntersectionType" && annotation.types?.length === 2) {
|
|
687
|
+
if (catalog && annotation.type === "TSIntersectionType" && annotation.types?.length === 2) {
|
|
686
688
|
const [left, right] = annotation.types;
|
|
687
689
|
if (right.type === "TSTypeLiteral" && right.members?.every((member) => member.type === "TSPropertySignature" && !!member.typeAnnotation && imported(member.typeAnnotation.typeAnnotation, "TypedMessageDescriptor"))) {
|
|
688
690
|
base = left;
|
|
@@ -816,7 +818,7 @@ const rule$20 = {
|
|
|
816
818
|
contract: "Generate or refresh the ICU argument contract.",
|
|
817
819
|
invalid: "Cannot generate message types: {{error}}.",
|
|
818
820
|
manual: "Unsupported number of generic arguments; update the call manually.",
|
|
819
|
-
annotation: "
|
|
821
|
+
annotation: "Message annotation may erase ICU contracts; use TypedMessageDescriptor types.",
|
|
820
822
|
dynamic: "Typed messages require static message strings and parser options for contract verification."
|
|
821
823
|
}
|
|
822
824
|
},
|
|
@@ -962,7 +964,7 @@ const rule$20 = {
|
|
|
962
964
|
});
|
|
963
965
|
return;
|
|
964
966
|
}
|
|
965
|
-
const annotation = imported.helper === "defineMessages" ?
|
|
967
|
+
const annotation = imported.helper === "defineMessages" || imported.helper === "defineMessage" ? messageAnnotation(context, node, catalog) : void 0;
|
|
966
968
|
if (annotation && !annotation.supported) {
|
|
967
969
|
context.report({
|
|
968
970
|
node: annotation.annotation,
|
|
@@ -973,7 +975,7 @@ const rule$20 = {
|
|
|
973
975
|
let contract;
|
|
974
976
|
let catalogType;
|
|
975
977
|
try {
|
|
976
|
-
if (annotation) catalogType = entries.length ? "{ " + entries.map((entry, index) => "readonly " + entry.key + ": import(" + JSON.stringify(imported.module) + ").TypedMessageDescriptor<" + types[index] + ">").join("; ") + " }" : "{}";
|
|
978
|
+
if (annotation) catalogType = !catalog ? "import(" + JSON.stringify(imported.module) + ").TypedMessageDescriptor" + (types[0] === "{}" ? "" : "<" + types[0] + ">") : entries.length ? "{ " + entries.map((entry, index) => "readonly " + entry.key + ": import(" + JSON.stringify(imported.module) + ").TypedMessageDescriptor" + (types[index] === "{}" ? "" : "<" + types[index] + ">")).join("; ") + " }" : "{}";
|
|
977
979
|
contract = !catalog ? types[0] : entries.length ? "{ " + entries.map((entry, index) => "readonly " + entry.key + ": " + types[index]).join("; ") + " }" : "{}";
|
|
978
980
|
} catch (error) {
|
|
979
981
|
context.report({
|
|
@@ -986,9 +988,11 @@ const rule$20 = {
|
|
|
986
988
|
const imports = hoistTypes(context, node, contract + (catalogType ? "\n" + catalogType : ""));
|
|
987
989
|
[contract, catalogType] = imports.text.split("\n");
|
|
988
990
|
const annotationMatches = !annotation || annotation.annotation === annotation.generated && renderType(annotation.generated) === catalogType;
|
|
989
|
-
const expected = `<${contract}>`;
|
|
990
991
|
const parameters = generic?.params;
|
|
991
|
-
|
|
992
|
+
const omitContract = (imported.helper === "defineMessage" || catalog) && types.every((type) => type === "{}") && (!parameters || parameters.length === 1);
|
|
993
|
+
const expected = omitContract ? "" : `<${contract}>`;
|
|
994
|
+
if (omitContract && !generic && annotationMatches) return;
|
|
995
|
+
if (!omitContract && parameters && parameters.length >= 1 && parameters.length <= 2 && renderType(parameters[0]) === contract && !generated && annotationMatches) return;
|
|
992
996
|
if (generic && (!parameters || parameters.length < 1 || parameters.length > 2)) {
|
|
993
997
|
context.report({
|
|
994
998
|
node: generic,
|
|
@@ -1003,7 +1007,7 @@ const rule$20 = {
|
|
|
1003
1007
|
const edits = [
|
|
1004
1008
|
...imports.fix(fixer),
|
|
1005
1009
|
...obsoleteTypes(context, [...parameters?.[0] ? [parameters[0]] : [], ...annotation && !annotationMatches ? [annotation.annotation] : []], contract + (catalogType ?? ""), fixer),
|
|
1006
|
-
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)
|
|
1010
|
+
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)
|
|
1007
1011
|
];
|
|
1008
1012
|
if (annotation && !annotationMatches) edits.push(fixer.replaceText(annotation.annotation, catalogType));
|
|
1009
1013
|
return edits;
|
|
@@ -5263,7 +5267,7 @@ var package_exports = /* @__PURE__ */ __exportAll({
|
|
|
5263
5267
|
version: () => version$1
|
|
5264
5268
|
});
|
|
5265
5269
|
var name$1 = "eslint-plugin-formatjs";
|
|
5266
|
-
var version$1 = "8.0.
|
|
5270
|
+
var version$1 = "8.0.3";
|
|
5267
5271
|
var description = "ESLint plugin for formatjs";
|
|
5268
5272
|
var keywords = [
|
|
5269
5273
|
"eslint",
|