Skip to content

Commit fc330f2

Browse files
committed
get first 3 tests to pass
1 parent e822a6b commit fc330f2

3 files changed

Lines changed: 48 additions & 38 deletions

File tree

test/fixtures/transformTargetPage.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11

22
{
3-
"does not contain {{Merge}}, {{Merge from}}, {{Being merged}}, {{Being merged from}}, {{Merge portions from}}, and does not contain {{Merge to}}, {{Being merged to}}, {{Articles for deletion/dated}}": {
3+
"contains 0 {{Merge from}}-ish templates, and contains 0 {{Merge to}}-ish templates": {
44
"oldWikicode": "Test",
55
"sourcePage": "Asian relations with Northeast India",
66
"targetPage": "Foreign relations of India",
77
"nominationName": "Asian relations with Northeast India (2nd nomination)",
88
"dateOfClosure": "April 2026",
99
"expectedWikicode": "{{Being merged from|Asian relations with Northeast India|afd=Asian relations with Northeast India (2nd nomination)|date=April 2026}}\nTest"
1010
},
11-
"does not contain {{Merge}}, {{Merge from}}, {{Being merged}}, {{Being merged from}}, {{Merge portions from}}, contains 1 {{Merge to}}, {{Being merged to}}, {{Articles for deletion/dated}}": {
11+
"contains 0 {{Merge from}}-ish templates, and contains 1 {{Merge to}}-ish templates": {
1212
"oldWikicode": "{{Merging to|Average|discuss=Talk:Average#Merge proposal|date=February 2026}}\nTest",
1313
"sourcePage": "Asian relations with Northeast India",
1414
"targetPage": "Foreign relations of India",
1515
"nominationName": "Asian relations with Northeast India (2nd nomination)",
1616
"dateOfClosure": "April 2026",
1717
"expectedWikicode": "{{Being merged from|Asian relations with Northeast India|afd=Asian relations with Northeast India (2nd nomination)|date=April 2026}}\nTest"
1818
},
19-
"does not contain {{Merge}}, {{Merge from}}, {{Being merged}}, {{Being merged from}}, {{Merge portions from}}, contains 2 {{Merge to}}, {{Being merged to}}, {{Articles for deletion/dated}}": {
19+
"contains 0 {{Merge from}}-ish templates, and contains 2 {{Merge to}}-ish templates": {
2020
"oldWikicode": "{{Merging to|Average|discuss=Talk:Average#Merge proposal|date=February 2026}}\n{{Merging to|Average|discuss=Talk:Average#Merge proposal|date=February 2026}}\nTest",
2121
"sourcePage": "Asian relations with Northeast India",
2222
"targetPage": "Foreign relations of India",

test/testTaskAddMergeTemplates.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -170,32 +170,6 @@ Lorem impsum`
170170
fixture.dateOfClosure
171171
);
172172
assert.strictEqual(output, fixture.expectedWikicode);
173-
174-
// TO ADD TO JSON FIXTURES FILE:
175-
176-
// test: does not contain any of {{Merge}}, {{Merge from}}, {{Being merged}}, {{Being merged from}}, {{Merge portions from}}
177-
// place {{Being merged from|Source article|afd=NominationName|date=Date of the closure}} at top
178-
// remove all of {{Merge to}}, {{Being merged to}}, {{Articles for deletion/dated}}, and {{Articles for deletion/dated}}'s hidden comments
179-
180-
// case sensitive version of the above
181-
// various parameter counts for the above (0, 1, 2)
182-
// test for some common redirects of the above templates
183-
// test for no "remove all of"'s being present
184-
// first nomination (blank), second nomination (not blank)
185-
186-
// test: contains 2 or more of {{Merge}}, {{Merge from}}, {{Being merged}}, {{Being merged from}}, {{Merge portions from}}
187-
// remove all but the bottom one
188-
// then perform the steps in the next test
189-
190-
// test: contains exactly 1 of {{Merge}}, {{Merge from}}, {{Being merged}}, {{Being merged from}}, {{Merge portions from}}, and has the correct |1= and |afd= parameters
191-
// remove template
192-
// replace with {{Being merged from|Source article|afd=NominationName|date=Date of the closure}} in the same spot
193-
194-
// test: contains exactly 1 of {{Merge}}, {{Merge from}}, {{Being merged}}, {{Being merged from}}, {{Merge portions from}}, and has a mismatch of any of the following parameters: |1= and |afd= parameters
195-
// remove template (including hidden comments around {{Articles for deletion/dated}})
196-
// place {{Being merged from|Source article|afd=NominationName|date=Date of the closure}} at the top of the article, but below hatnotes
197-
198-
// aliases of the parameter afd (discuss, discussion, talk)
199173
});
200174
});
201175
});

xfdcloser-src/Controllers/Tasks/AddMergeTemplates.js

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,28 +104,64 @@ export default class AddMergeTemplatesTask extends TaskItemController {
104104
transformTargetPage(oldWikicode, sourcePage, targetPage, nominationName, dateOfClosure) {
105105
let newWikicode = oldWikicode;
106106

107-
// TODO: add common aliases (and add test)
108-
// TODO: case insensitive (and add test)
109-
const mergeFromTemplates = ["Merge", "Merge from", "Being merged", "Being merged from", "Merge portions from"];
110-
const mergeFromTemplatesCount = oldWikicode.match(new RegExp(`{{\\s*(${mergeFromTemplates.join("|")})\\s*`, "gi"))?.length || 0;
107+
const mergeFromTemplates = [
108+
"Merge", /* aliases: */ "Mergedisputed", "Mergewith", "MergeDisputed", "MergeVfD", "Merge-disputed", "Merge disputed", "Merge-multiple", "Mergesplit", "MergeSplit", "Mergemulti", "Mergetomultiple-with", "Multimerge", "Proposed merge", "Merge with",
109+
110+
"Merge from", /* aliases: */ "Merge-from", "Include", "Mergefrom-multiple", "Multiplemergefrom", "Mergefrommulti", "Mergefrommultiple", "Multimergefrom", "Mergefrom-category", "MergeFrom", "Mergefrom", "Merge from draft", "Merge from AfD",
111+
112+
"Being merged", /* aliases: */ "Merging", "Mergingsectionto",
113+
114+
"Being merged from", /* aliases: */ "Merging from", "Mergingfrom", "Being Merge from", "Being merge from", "Merging-from",
115+
116+
"Merge portions from", /* aliases: */ "Move section portions from", "Move portions from", "Merge section portions from",
117+
];
118+
const mergeToTemplates = [
119+
"Merge to", /* aliases: */ "Merge-to", "Mergeinto", "MergePartial", "MergetoCat", "Mergelist", "Mergeto-disputed", "Mergeto-multiple", "Multiplemergeinto", "Multiplemergeto", "Multiple-merge-to", "Merge into", "MergeTo", "Mergeto", "Merge to article", "Merge2", "Merge-into",
120+
121+
"Being merged to", /* aliases: */ "Merging to", "Being Merge to", "Being merge to", "Merging into", "Merginginto", "Mergingto", "Merging-to",
122+
123+
"Article for deletion/dated", /* aliases: */ "AfDM", "Afd/dated", "AfD/dated", "Afdm",
124+
];
125+
const mergeFromTemplatesCount = oldWikicode.match(new RegExp(`{{\\s*(${mergeFromTemplates.join("|")})\\s*[|}]`, "gi"))?.length || 0;
111126
if ( !mergeFromTemplatesCount ) {
127+
// Add {{Being merged from}} template
112128
newWikicode = `{{Being merged from|${sourcePage}|afd=${nominationName}|date=${dateOfClosure}}}\n` + newWikicode;
113-
// if present, delete {{Merge to}}, {{being merged to}}, {{Article for deletion/dated}}, or one of their redirects. When removing {{Article for deletion/dated}} or its redirects, also remove the <! -- hidden comments -- > around it -- > (XFDcloser already does this, but only on the source article)
129+
// Delete "Merge to" templates
130+
newWikicode = this.deleteTemplatesIfPresent(mergeToTemplates, newWikicode);
131+
// Delete hidden HTML comments from {{Articles for deletion/dated}}
132+
// newWikicode = newWikicode.replace(/<!--[\s\S]*?-->/gi, "");
114133
} else {
115134
if ( mergeFromTemplatesCount > 1 ) {
116-
// remove all of them except the last one (when removing {{Article for deletion/dated}}, also remove the hidden comments as above).
135+
// remove all of them except the last one
117136
}
118-
const sourceArticleMatches = /* TODO */ false; // check whether the existing |1="Source article" matches the article that was nominated
119-
const nominationNameMatches = /* TODO */ false; // check whether the |afd= parameter (or one of its aliases: discuss, discussion, talk) contains the correct NominationName (e.g., Earth (8th nomination))
137+
const sourceArticleMatches = /* TODO */ false; // check whether the existing |1="Source article" matches the article that was nominated. missing parameter = mismatch.
138+
const nominationNameMatches = /* TODO */ false; // check whether the |afd= parameter (or one of its aliases: discuss, discussion, talk) contains the correct NominationName (e.g., Earth (8th nomination)). missing parameter = mismatch.
120139
if ( sourceArticleMatches && nominationNameMatches ) {
121140
// replace template with {{Being merged from|Source article|afd=NominationName|date=Date of the closure}} on the exact same line
122141
} else {
123-
// replace template with {{Being merged from|Source article|afd=NominationName|date=Date of the closure}} at the top of the article, but below hatnotes
142+
// remove template
143+
// add {{Being merged from|Source article|afd=NominationName|date=Date of the closure}} at the top of the article, but below hatnotes
124144
}
125145
}
126146
return newWikicode;
127147
}
128148

149+
/**
150+
* @param {string|array} templateNames
151+
*/
152+
deleteTemplatesIfPresent(templateNames, wikicode) {
153+
if ( typeof templateNames === "string" ) {
154+
templateNames = [templateNames];
155+
}
156+
for ( const templateName of templateNames ) {
157+
// if it's on its own line, also delete the line break at the end
158+
wikicode = wikicode.replace(new RegExp(`^\\s*{{\\s*${templateName}\\s*[^}]*}}\\s*\\n`, "gim"), "");
159+
// if it's not on its own line, don't delete any line breaks
160+
wikicode = wikicode.replace(new RegExp(`{{\\s*${templateName}\\s*[^}]*}}`, "gi"), "");
161+
}
162+
return wikicode;
163+
}
164+
129165
doTask() {
130166
const mergers = this.getMergers();
131167
if ( mergers.length === 0 ) {

0 commit comments

Comments
 (0)