Skip to content

Commit

Permalink
Fix linting and add new lint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
dolanmiu committed Sep 19, 2022
1 parent e90d97b commit 5950055
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 46 deletions.
46 changes: 29 additions & 17 deletions .eslintrc.js
Expand Up @@ -12,6 +12,7 @@ https://github.com/typescript-eslint/tslint-to-eslint-config/blob/master/docs/FA
Happy linting! 💖
*/
module.exports = {
extends: "eslint:recommended",
env: {
browser: true,
es6: true,
Expand All @@ -33,6 +34,31 @@ module.exports = {
],
root: true,
rules: {
"no-undef": "off",
"no-extra-boolean-cast": "off",
"no-alert": "error",
"no-self-compare": "error",
"no-unreachable-loop": "error",
"no-template-curly-in-string": "error",
"no-unused-private-class-members": "error",
"no-extend-native": "error",
"no-floating-decimal": "error",
"no-implied-eval": "error",
"no-iterator": "error",
"no-lone-blocks": "error",
"no-loop-func": "error",
"no-new-object": "error",
"no-proto": "error",
"no-useless-catch": "error",
"one-var-declaration-per-line": "error",
"prefer-arrow-callback": "error",
"prefer-destructuring": "error",
"prefer-exponentiation-operator": "error",
"prefer-promise-reject-errors": "error",
"prefer-regex-literals": "error",
"prefer-spread": "error",
"prefer-template": "error",
"require-await": "error",
"@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/array-type": [
"error",
Expand Down Expand Up @@ -95,22 +121,6 @@ module.exports = {
allowTypedFunctionExpressions: false,
},
],
"@typescript-eslint/indent": [
"error",
4,
{
ObjectExpression: "first",
FunctionDeclaration: {
parameters: "first",
},
FunctionExpression: {
parameters: "first",
},
SwitchCase: 1,
flatTernaryExpressions: false,
ignoredNodes: [],
},
],
"@typescript-eslint/naming-convention": [
"error",
{
Expand Down Expand Up @@ -176,7 +186,7 @@ module.exports = {
"import/order": "error",
indent: "off",
"jsdoc/check-alignment": "error",
"jsdoc/check-indentation": "error",
"jsdoc/check-indentation": "off",
"jsdoc/newline-after-description": "error",
"max-classes-per-file": "off",
"max-len": "off",
Expand Down Expand Up @@ -245,13 +255,15 @@ module.exports = {
"functional/no-method-signature": "error",
"functional/no-mixed-type": "error",
"functional/prefer-readonly-type": "error",
"no-unused-vars": ["error", { argsIgnorePattern: "^[_]+$" }],
},
overrides: [
{
files: ["*.spec.ts"],
rules: {
"@typescript-eslint/no-unused-expressions": "off",
"@typescript-eslint/dot-notation": "off",
"prefer-destructuring": "off",
},
},
],
Expand Down
7 changes: 3 additions & 4 deletions demo/74-nodejs-stream.ts
@@ -1,4 +1,4 @@
// Simple example to add text to a document
// Exporting the document as a stream
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun } from "../build";
Expand Down Expand Up @@ -26,6 +26,5 @@ const doc = new Document({
],
});

Packer.toStream(doc).then((stream) => {
stream.pipe(fs.createWriteStream("My Document.docx"));
});
const stream = Packer.toStream(doc);
stream.pipe(fs.createWriteStream("My Document.docx"));
4 changes: 2 additions & 2 deletions src/export/packer/next-compiler.spec.ts
Expand Up @@ -25,7 +25,7 @@ describe("Compiler", () => {
});

describe("#compile()", () => {
it("should pack all the content", async function () {
it("should pack all the content", function () {
this.timeout(99999999);
const file = new File({
sections: [],
Expand Down Expand Up @@ -53,7 +53,7 @@ describe("Compiler", () => {
expect(fileNames).to.include("_rels/.rels");
});

it("should pack all additional headers and footers", async function () {
it("should pack all additional headers and footers", function () {
const file = new File({
sections: [
{
Expand Down
12 changes: 7 additions & 5 deletions src/export/packer/packer.spec.ts
Expand Up @@ -54,7 +54,7 @@ describe("Packer", () => {
assert.isTrue(buffer.byteLength > 0);
});

it("should handle exception if it throws any", async () => {
it("should handle exception if it throws any", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const compiler = stub((Packer as any).compiler, "compile");

Expand Down Expand Up @@ -139,7 +139,7 @@ describe("Packer", () => {
const stream = await Packer.toStream(file);
return new Promise((resolve, reject) => {
stream.on("error", () => {
reject();
reject(new Error());
});

stream.on("end", () => {
Expand All @@ -148,7 +148,7 @@ describe("Packer", () => {
});
});

it("should handle exception if it throws any", async () => {
it("should handle exception if it throws any", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const compiler = stub((Packer as any).compiler, "compile").callsFake(() => ({
// tslint:disable-next-line: no-empty
Expand All @@ -160,9 +160,11 @@ describe("Packer", () => {
}));

compiler.throwsException();
return Packer.toStream(file).catch((error) => {
try {
Packer.toStream(file);
} catch (error) {
assert.isDefined(error);
});
}
});

afterEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/export/packer/packer.ts
Expand Up @@ -58,7 +58,7 @@ export class Packer {
return zipData;
}

public static async toStream(file: File, prettify?: boolean | PrettifyType): Promise<Stream> {
public static toStream(file: File, prettify?: boolean | PrettifyType): Stream {
const zip = this.compiler.compile(file, prettify);
const zipData = zip.generateNodeStream({
type: "nodebuffer",
Expand Down
1 change: 1 addition & 0 deletions src/file/custom-properties/custom-properties.ts
Expand Up @@ -33,6 +33,7 @@ export class CustomProperties extends XmlComponent {
}

public addCustomProperty(property: ICustomPropertyOptions): void {
// eslint-disable-next-line functional/immutable-data
this.properties.push(new CustomProperty(this.nextId++, property));
}
}
17 changes: 13 additions & 4 deletions src/file/file.ts
Expand Up @@ -37,13 +37,13 @@ export interface ISectionOptions {
}

export class File {
// eslint-disable-next-line functional/immutable-data
// eslint-disable-next-line functional/prefer-readonly-type
private currentRelationshipId: number = 1;

private readonly documentWrapper: DocumentWrapper;
// eslint-disable-next-line functional/immutable-data
// eslint-disable-next-line functional/prefer-readonly-type
private readonly headers: IDocumentHeader[] = [];
// eslint-disable-next-line functional/immutable-data
// eslint-disable-next-line functional/prefer-readonly-type
private readonly footers: IDocumentFooter[] = [];
private readonly coreProperties: CoreProperties;
private readonly numbering: Numbering;
Expand Down Expand Up @@ -128,7 +128,7 @@ export class File {
}

if (options.footnotes) {
// tslint:disable-next-line: forin
// eslint-disable-next-line guard-for-in
for (const key in options.footnotes) {
this.footnotesWrapper.View.createFootNote(parseFloat(key), options.footnotes[key].children);
}
Expand Down Expand Up @@ -156,6 +156,7 @@ export class File {
}

private createHeader(header: Header): HeaderWrapper {
// eslint-disable-next-line functional/immutable-data
const wrapper = new HeaderWrapper(this.media, this.currentRelationshipId++);

for (const child of header.options.children) {
Expand All @@ -167,6 +168,7 @@ export class File {
}

private createFooter(footer: Footer): FooterWrapper {
// eslint-disable-next-line functional/immutable-data
const wrapper = new FooterWrapper(this.media, this.currentRelationshipId++);

for (const child of footer.options.children) {
Expand All @@ -178,6 +180,7 @@ export class File {
}

private addHeaderToDocument(header: HeaderWrapper, type: HeaderFooterReferenceType = HeaderFooterReferenceType.DEFAULT): void {
// eslint-disable-next-line functional/immutable-data
this.headers.push({ header, type });
this.documentWrapper.Relationships.createRelationship(
header.View.ReferenceId,
Expand All @@ -188,6 +191,7 @@ export class File {
}

private addFooterToDocument(footer: FooterWrapper, type: HeaderFooterReferenceType = HeaderFooterReferenceType.DEFAULT): void {
// eslint-disable-next-line functional/immutable-data
this.footers.push({ footer, type });
this.documentWrapper.Relationships.createRelationship(
footer.View.ReferenceId,
Expand Down Expand Up @@ -220,26 +224,31 @@ export class File {
);

this.documentWrapper.Relationships.createRelationship(
// eslint-disable-next-line functional/immutable-data
this.currentRelationshipId++,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
"styles.xml",
);
this.documentWrapper.Relationships.createRelationship(
// eslint-disable-next-line functional/immutable-data
this.currentRelationshipId++,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",
"numbering.xml",
);
this.documentWrapper.Relationships.createRelationship(
// eslint-disable-next-line functional/immutable-data
this.currentRelationshipId++,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes",
"footnotes.xml",
);
this.documentWrapper.Relationships.createRelationship(
// eslint-disable-next-line functional/immutable-data
this.currentRelationshipId++,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings",
"settings.xml",
);
this.documentWrapper.Relationships.createRelationship(
// eslint-disable-next-line functional/immutable-data
this.currentRelationshipId++,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
"comments.xml",
Expand Down
12 changes: 8 additions & 4 deletions src/file/header.ts
Expand Up @@ -6,13 +6,17 @@ export interface IHeaderOptions {
}

export class Header {
public constructor(public readonly options: IHeaderOptions = { children: [] }) {
// noop
public readonly options: IHeaderOptions;

public constructor(options: IHeaderOptions = { children: [] }) {
this.options = options;
}
}

export class Footer {
public constructor(public readonly options: IHeaderOptions = { children: [] }) {
// noop
public readonly options: IHeaderOptions;

public constructor(options: IHeaderOptions = { children: [] }) {
this.options = options;
}
}
6 changes: 4 additions & 2 deletions src/file/media/media.spec.ts
Expand Up @@ -37,6 +37,7 @@ describe("Media", () => {
});

it("should return UInt8Array if atob is present", () => {
// eslint-disable-next-line functional/immutable-data
global.atob = () => "atob result";

const image = new Media().addMedia("", {
Expand All @@ -45,11 +46,12 @@ describe("Media", () => {
});
expect(image.stream).to.be.an.instanceof(Uint8Array);

// tslint:disable-next-line: no-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any, functional/immutable-data
(global as any).atob = undefined;
});

it("should use data as is if its not a string", () => {
// eslint-disable-next-line functional/immutable-data
global.atob = () => "atob result";

const image = new Media().addMedia(Buffer.from(""), {
Expand All @@ -58,7 +60,7 @@ describe("Media", () => {
});
expect(image.stream).to.be.an.instanceof(Uint8Array);

// tslint:disable-next-line: no-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any, functional/immutable-data
(global as any).atob = undefined;
});
});
Expand Down
3 changes: 2 additions & 1 deletion src/file/numbering/numbering.spec.ts
Expand Up @@ -34,13 +34,14 @@ describe("Numbering", () => {
.filter((el) => el["w:lvl"])
.forEach((el, ix) => {
expect(Object.keys(el)).to.have.lengthOf(1);
expect(Object.keys(el["w:lvl"]).sort()).to.deep.equal(["_attr", "w:start", "w:lvlJc", "w:numFmt", "w:pPr", "w:rPr"]);
expect(Object.keys(el["w:lvl"])).to.deep.equal(["_attr", "w:start", "w:lvlJc", "w:numFmt", "w:pPr", "w:rPr"]);
expect(el["w:lvl"]).to.have.deep.members([
{ _attr: { "w:ilvl": ix, "w15:tentative": 1 } },
{ "w:start": [{ _attr: { "w:val": 1 } }] },
{ "w:lvlJc": [{ _attr: { "w:val": "left" } }] },
{ "w:numFmt": [{ _attr: { "w:val": "bullet" } }] },
]);
// TODO
// Once chai 4.0.0 lands and #644 is resolved, we can add the following to the test:
// {"w:lvlText": {"_attr": {"w:val": "•"}}},
// {"w:rPr": [{"w:rFonts": {"_attr": {"w:ascii": "Symbol", "w:cs": "Symbol", "w:eastAsia": "Symbol", "w:hAnsi": "Symbol", "w:hint": "default"}}}]},
Expand Down
4 changes: 2 additions & 2 deletions src/file/paragraph/run/image-run.spec.ts
Expand Up @@ -771,7 +771,7 @@ describe("ImageRun", () => {
],
});

// tslint:disable-next-line: no-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any, functional/immutable-data
(global as any).atob = undefined;
});

Expand Down Expand Up @@ -1028,7 +1028,7 @@ describe("ImageRun", () => {
],
});

// tslint:disable-next-line: no-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any, functional/immutable-data
(global as any).atob = undefined;
});
});
Expand Down
8 changes: 5 additions & 3 deletions src/file/xml-components/imported-xml-component.ts
@@ -1,4 +1,3 @@
// eslint-disable @typescript-eslint/no-explicit-any
import { Element as XmlElement, xml2js } from "xml-js";

import { IXmlableObject, XmlAttributeComponent, XmlComponent } from "@file/xml-components";
Expand All @@ -15,7 +14,9 @@ export const convertToXmlComponent = (element: XmlElement): ImportedXmlComponent
switch (element.type) {
case undefined:
case "element":
// eslint-disable-next-line no-case-declarations
const xmlComponent = new ImportedXmlComponent(element.name as string, element.attributes);
// eslint-disable-next-line no-case-declarations
const childElements = element.elements || [];
for (const childElm of childElements) {
const child = convertToXmlComponent(childElm);
Expand All @@ -31,6 +32,7 @@ export const convertToXmlComponent = (element: XmlElement): ImportedXmlComponent
}
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
class ImportedXmlComponentAttributes extends XmlAttributeComponent<any> {
// noop
}
Expand All @@ -54,7 +56,7 @@ export class ImportedXmlComponent extends XmlComponent {
* @param importedContent xml content of the imported component
*/

// tslint:disable-next-line:variable-name
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public constructor(rootKey: string, _attr?: any) {
super(rootKey);
if (_attr) {
Expand All @@ -71,7 +73,7 @@ export class ImportedXmlComponent extends XmlComponent {
* Used for the attributes of root element that is being imported.
*/
export class ImportedRootElementAttributes extends XmlComponent {
// tslint:disable-next-line:variable-name
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public constructor(private readonly _attr: any) {
super("");
}
Expand Down

0 comments on commit 5950055

Please sign in to comment.