Turn back on no-null-keyword. Use empty object instead of null to signal to the xml library that an empty element should be produced. Update the related tests.

Related to #306
This commit is contained in:
Bruce Duncan
2019-04-10 01:28:37 -04:00
parent 816cb54b14
commit bb1604cd8f
17 changed files with 72 additions and 71 deletions

View File

@ -216,7 +216,7 @@ describe("AbstractNumbering", () => {
const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.").keepLines();
const tree = new Formatter().format(level);
expect(tree["w:lvl"]).to.include({
"w:pPr": [{ "w:keepLines": null }],
"w:pPr": [{ "w:keepLines": {} }],
});
});
@ -225,7 +225,7 @@ describe("AbstractNumbering", () => {
const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.").keepNext();
const tree = new Formatter().format(level);
expect(tree["w:lvl"]).to.include({
"w:pPr": [{ "w:keepNext": null }],
"w:pPr": [{ "w:keepNext": {} }],
});
});
});

View File

@ -156,12 +156,12 @@ describe("Image", () => {
},
},
{
"a:srcRect": null,
"a:srcRect": {},
},
{
"a:stretch": [
{
"a:fillRect": null,
"a:fillRect": {},
},
],
},
@ -202,7 +202,7 @@ describe("Image", () => {
},
},
{
"a:avLst": null,
"a:avLst": {},
},
],
},

View File

@ -471,7 +471,7 @@ describe("Paragraph", () => {
{
"w:pPr": [
{
"w:pageBreakBefore": null,
"w:pageBreakBefore": {},
},
],
},
@ -625,7 +625,7 @@ describe("Paragraph", () => {
paragraph.keepLines();
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [{ "w:pPr": [{ "w:keepLines": null }] }],
"w:p": [{ "w:pPr": [{ "w:keepLines": {} }] }],
});
});
});
@ -635,7 +635,7 @@ describe("Paragraph", () => {
paragraph.keepNext();
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [{ "w:pPr": [{ "w:keepNext": null }] }],
"w:p": [{ "w:pPr": [{ "w:keepNext": {} }] }],
});
});
});
@ -645,7 +645,7 @@ describe("Paragraph", () => {
paragraph.bidirectional();
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [{ "w:pPr": [{ "w:bidi": null }] }],
"w:p": [{ "w:pPr": [{ "w:bidi": {} }] }],
});
});
});

View File

@ -8,7 +8,7 @@ describe("Compatibility", () => {
const compatibility = new Compatibility();
const tree = new Formatter().format(compatibility);
expect(tree).to.deep.equal({ "w:compat": null });
expect(tree).to.deep.equal({ "w:compat": {} });
});
});
@ -18,7 +18,7 @@ describe("Compatibility", () => {
compatibility.doNotExpandShiftReturn();
const tree = new Formatter().format(compatibility);
expect(tree).to.deep.equal({ "w:compat": [{ "w:doNotExpandShiftReturn": null }] });
expect(tree).to.deep.equal({ "w:compat": [{ "w:doNotExpandShiftReturn": {} }] });
});
});
});

View File

@ -20,7 +20,7 @@ describe("CharacterStyle", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
],
});
@ -41,7 +41,7 @@ describe("CharacterStyle", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
],
});
@ -63,7 +63,7 @@ describe("CharacterStyle", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
{ "w:basedOn": { _attr: { "w:val": "otherId" } } },
],
@ -89,7 +89,7 @@ describe("CharacterStyle", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
],
});
@ -113,7 +113,7 @@ describe("CharacterStyle", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
],
});
@ -136,7 +136,7 @@ describe("CharacterStyle", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
],
});
@ -159,7 +159,7 @@ describe("CharacterStyle", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
],
});
@ -191,7 +191,7 @@ describe("CharacterStyle", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
],
});
@ -214,7 +214,7 @@ describe("CharacterStyle", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
],
});
@ -237,7 +237,7 @@ describe("CharacterStyle", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
],
});
@ -260,7 +260,7 @@ describe("CharacterStyle", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
],
});
@ -280,7 +280,7 @@ describe("CharacterStyle", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
{ "w:link": { _attr: { "w:val": "MyLink" } } },
],
@ -300,8 +300,8 @@ describe("CharacterStyle", () => {
},
},
},
{ "w:unhideWhenUsed": null },
{ "w:semiHidden": null },
{ "w:unhideWhenUsed": {} },
{ "w:semiHidden": {} },
],
});
});

View File

@ -36,18 +36,18 @@ describe("Style components", () => {
it("UnhideWhenUsed#constructor", () => {
const style = new components.UnhideWhenUsed();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ "w:unhideWhenUsed": null });
expect(tree).to.deep.equal({ "w:unhideWhenUsed": {} });
});
it("QuickFormat#constructor", () => {
const style = new components.QuickFormat();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ "w:qFormat": null });
expect(tree).to.deep.equal({ "w:qFormat": {} });
});
it("SemiHidden#constructor", () => {
const style = new components.SemiHidden();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ "w:semiHidden": null });
expect(tree).to.deep.equal({ "w:semiHidden": {} });
});
});

View File

@ -12,7 +12,7 @@ describe("Default Styles", () => {
{ "w:name": { _attr: { "w:val": "Heading 1" } } },
{ "w:basedOn": { _attr: { "w:val": "Normal" } } },
{ "w:next": { _attr: { "w:val": "Normal" } } },
{ "w:qFormat": null },
{ "w:qFormat": {} },
],
});
});
@ -26,7 +26,7 @@ describe("Default Styles", () => {
{ "w:name": { _attr: { "w:val": "Title" } } },
{ "w:basedOn": { _attr: { "w:val": "Normal" } } },
{ "w:next": { _attr: { "w:val": "Normal" } } },
{ "w:qFormat": null },
{ "w:qFormat": {} },
],
});
});
@ -40,7 +40,7 @@ describe("Default Styles", () => {
{ "w:name": { _attr: { "w:val": "Heading 1" } } },
{ "w:basedOn": { _attr: { "w:val": "Normal" } } },
{ "w:next": { _attr: { "w:val": "Normal" } } },
{ "w:qFormat": null },
{ "w:qFormat": {} },
],
});
});
@ -54,7 +54,7 @@ describe("Default Styles", () => {
{ "w:name": { _attr: { "w:val": "Heading 2" } } },
{ "w:basedOn": { _attr: { "w:val": "Normal" } } },
{ "w:next": { _attr: { "w:val": "Normal" } } },
{ "w:qFormat": null },
{ "w:qFormat": {} },
],
});
});
@ -68,7 +68,7 @@ describe("Default Styles", () => {
{ "w:name": { _attr: { "w:val": "Heading 3" } } },
{ "w:basedOn": { _attr: { "w:val": "Normal" } } },
{ "w:next": { _attr: { "w:val": "Normal" } } },
{ "w:qFormat": null },
{ "w:qFormat": {} },
],
});
});
@ -82,7 +82,7 @@ describe("Default Styles", () => {
{ "w:name": { _attr: { "w:val": "Heading 4" } } },
{ "w:basedOn": { _attr: { "w:val": "Normal" } } },
{ "w:next": { _attr: { "w:val": "Normal" } } },
{ "w:qFormat": null },
{ "w:qFormat": {} },
],
});
});
@ -96,7 +96,7 @@ describe("Default Styles", () => {
{ "w:name": { _attr: { "w:val": "Heading 5" } } },
{ "w:basedOn": { _attr: { "w:val": "Normal" } } },
{ "w:next": { _attr: { "w:val": "Normal" } } },
{ "w:qFormat": null },
{ "w:qFormat": {} },
],
});
});
@ -110,7 +110,7 @@ describe("Default Styles", () => {
{ "w:name": { _attr: { "w:val": "Heading 6" } } },
{ "w:basedOn": { _attr: { "w:val": "Normal" } } },
{ "w:next": { _attr: { "w:val": "Normal" } } },
{ "w:qFormat": null },
{ "w:qFormat": {} },
],
});
});
@ -123,7 +123,7 @@ describe("Default Styles", () => {
{ _attr: { "w:type": "paragraph", "w:styleId": "ListParagraph" } },
{ "w:name": { _attr: { "w:val": "List Paragraph" } } },
{ "w:basedOn": { _attr: { "w:val": "Normal" } } },
{ "w:qFormat": null },
{ "w:qFormat": {} },
],
});
});
@ -176,10 +176,10 @@ describe("Default Styles", () => {
},
},
{
"w:semiHidden": null,
"w:semiHidden": {},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
],
});
@ -211,12 +211,12 @@ describe("Default Styles", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
{ "w:basedOn": { _attr: { "w:val": "DefaultParagraphFont" } } },
{
"w:semiHidden": null,
"w:semiHidden": {},
},
],
});
@ -255,12 +255,12 @@ describe("Default Styles", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
{ "w:basedOn": { _attr: { "w:val": "DefaultParagraphFont" } } },
{ "w:link": { _attr: { "w:val": "FootnoteText" } } },
{
"w:semiHidden": null,
"w:semiHidden": {},
},
],
});
@ -284,7 +284,7 @@ describe("Default Styles", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
{ "w:basedOn": { _attr: { "w:val": "DefaultParagraphFont" } } },
],

View File

@ -42,7 +42,7 @@ describe("ParagraphStyle", () => {
const style = new ParagraphStyle("myStyleId").quickFormat();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:qFormat": null }],
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:qFormat": {} }],
});
});
@ -216,7 +216,7 @@ describe("ParagraphStyle", () => {
const style = new ParagraphStyle("myStyleId").keepLines();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:pPr": [{ "w:keepLines": null }] }],
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:pPr": [{ "w:keepLines": {} }] }],
});
});
@ -224,7 +224,7 @@ describe("ParagraphStyle", () => {
const style = new ParagraphStyle("myStyleId").keepNext();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:pPr": [{ "w:keepNext": null }] }],
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:pPr": [{ "w:keepNext": {} }] }],
});
});
@ -439,7 +439,7 @@ describe("ParagraphStyle", () => {
const style = new ParagraphStyle("myStyleId").semiHidden();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:semiHidden": null }],
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:semiHidden": {} }],
});
});
@ -464,7 +464,7 @@ describe("ParagraphStyle", () => {
const style = new ParagraphStyle("myStyleId").unhideWhenUsed();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:unhideWhenUsed": null }],
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:unhideWhenUsed": {} }],
});
});
});

View File

@ -64,7 +64,7 @@ describe("Styles", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
],
},
@ -88,7 +88,7 @@ describe("Styles", () => {
},
},
{
"w:unhideWhenUsed": null,
"w:unhideWhenUsed": {},
},
],
},

View File

@ -17,7 +17,7 @@ describe("GridCol", () => {
it("does not set a width attribute if not given", () => {
const grid = new GridCol();
const tree = new Formatter().format(grid);
expect(tree).to.deep.equal({ "w:gridCol": null });
expect(tree).to.deep.equal({ "w:gridCol": {} });
});
});
});

View File

@ -37,15 +37,15 @@ describe("TableColumn", () => {
const tree = new Formatter().format(cells[0]);
expect(tree).to.deep.equal({
"w:tc": [{ "w:tcPr": [{ "w:vMerge": { _attr: { "w:val": "restart" } } }] }, { "w:p": null }],
"w:tc": [{ "w:tcPr": [{ "w:vMerge": { _attr: { "w:val": "restart" } } }] }, { "w:p": {} }],
});
const tree2 = new Formatter().format(cells[1]);
expect(tree2).to.deep.equal({ "w:tc": [{ "w:p": null }] });
expect(tree2).to.deep.equal({ "w:tc": [{ "w:p": {} }] });
const tree3 = new Formatter().format(cells[2]);
expect(tree3).to.deep.equal({
"w:tc": [{ "w:tcPr": [{ "w:vMerge": { _attr: { "w:val": "continue" } } }] }, { "w:p": null }],
"w:tc": [{ "w:tcPr": [{ "w:vMerge": { _attr: { "w:val": "continue" } } }] }, { "w:p": {} }],
});
});
});

View File

@ -11,7 +11,7 @@ describe("TableRow", () => {
const tableRow = new TableRow([]);
const tree = new Formatter().format(tableRow);
expect(tree).to.deep.equal({
"w:tr": null,
"w:tr": {},
});
});
@ -23,7 +23,7 @@ describe("TableRow", () => {
{
"w:tc": [
{
"w:p": null,
"w:p": {},
},
],
},

View File

@ -107,7 +107,7 @@ const WIDTHS = {
// ],
// },
// { "w:tblGrid": [{ "w:gridCol": { _attr: { "w:w": 100 } } }] },
// { "w:tr": [{ "w:tc": [{ "w:p": null }] }] },
// { "w:tr": [{ "w:tc": [{ "w:p": {} }] }] },
// ],
// };
@ -119,7 +119,7 @@ describe("Table", () => {
columns: 2,
});
const tree = new Formatter().format(table);
const cell = { "w:tc": [{ "w:p": null }] };
const cell = { "w:tc": [{ "w:p": {} }] };
expect(tree).to.deep.equal({
"w:tbl": [
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS] },
@ -284,7 +284,7 @@ describe("Table", () => {
.to.be.an("array")
.which.has.length.at.least(1);
expect(row["w:tr"].find((x) => x["w:tc"])).to.deep.equal({
"w:tc": [{ "w:p": null }],
"w:tc": [{ "w:p": {} }],
});
});
@ -311,7 +311,7 @@ describe("Table", () => {
const cell = row["w:tr"].find((x) => x["w:tc"]);
expect(cell).not.to.be.undefined;
expect(cell["w:tc"][cell["w:tc"].length - 1]).to.deep.equal({
"w:p": null,
"w:p": {},
});
});

View File

@ -73,7 +73,7 @@ describe("ImportedXmlComponent", () => {
},
},
{
"w:child": null,
"w:child": {},
},
],
});

View File

@ -1,4 +1,4 @@
import { assert } from "chai";
import { expect } from "chai";
import { Utility } from "tests/utility";
import { XmlComponent } from "./";
@ -15,7 +15,7 @@ describe("XmlComponent", () => {
describe("#constructor()", () => {
it("should create an Xml Component which has the correct rootKey", () => {
const newJson = Utility.jsonify(xmlComponent);
assert.equal(newJson.rootKey, "w:test");
expect(newJson.rootKey).to.equal("w:test");
});
});
@ -31,7 +31,7 @@ describe("XmlComponent", () => {
return;
}
assert.isNull(xml["w:test"]);
expect(xml["w:test"]).to.deep.equal({});
});
});
});

View File

@ -30,11 +30,11 @@ export abstract class XmlComponent extends BaseXmlComponent {
// represents our attributes, use the object itself as our children to
// avoid an unneeded XML close element. (Note: We have to use this
// function to get typescript to allow our check.)
// Additionally, if the array is empty, use null as our children to
// get an empty XML element generated.
// Additionally, if the array is empty, use an empty object as our
// children in order to get an empty XML element generated.
const onlyAttrs = (c) => typeof c === "object" && c._attr;
return {
[this.rootKey]: children.length ? (children.length === 1 && onlyAttrs(children[0]) ? children[0] : children) : null,
[this.rootKey]: children.length ? (children.length === 1 && onlyAttrs(children[0]) ? children[0] : children) : {},
};
}
@ -52,7 +52,9 @@ export abstract class XmlComponent extends BaseXmlComponent {
export abstract class IgnoreIfEmptyXmlComponent extends XmlComponent {
public prepForXml(): IXmlableObject | undefined {
const result = super.prepForXml();
if (result && result[this.rootKey]) {
// Ignore the object if its falsey or is an empty object (would produce
// an empty XML element if allowed to be included in the output).
if (result && (typeof result[this.rootKey] !== "object" || Object.keys(result[this.rootKey]).length)) {
return result;
}
}

View File

@ -15,8 +15,7 @@
"max-classes-per-file": [false],
"no-implicit-dependencies": false,
"no-submodule-imports": false,
// We need null in order to get empty elements from the xml library
"no-null-keyword": false,
"no-null-keyword": true,
"return-undefined": true,
"prefer-readonly": true,
"no-duplicate-imports": true,