Skip to content

Commit 7a46444

Browse files
authored
[JS] Modernize and automate code quality/formatting (#6815)
* [JS] `prettier` config and updated `eslint` Fixes #6757 * lint-staged config run "npx lint-staged" from nodejs root for service * set up husky * `eslint --fix` on `adaptivecards` does not include fixing of fixes :) * eslint manual cleanup * Fix formatting of properties with quotes * `prettier` format `adaptivecards` * tsx/jsx support and remove from .editorconfig * Pre-filter commit hook to only check if files opened in nodejs
1 parent de2242f commit 7a46444

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+4579
-2646
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
root = true
22

3-
[**.{cpp,h,java,mm,cs,ts,js,tsx}]
3+
[**.{cpp,h,java,mm,cs}]
44
indent_style = spaces
55
indent_size = 4
66
charset = utf-8

source/nodejs/.eslintignore

-2
This file was deleted.

source/nodejs/.eslintrc.js

+117-261
Large diffs are not rendered by default.

source/nodejs/.husky/pre-commit

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
cd source/nodejs
5+
NODEJS_FILES_CHANGED=`git status . --short | wc -l`
6+
if [ $NODEJS_FILES_CHANGED != "0" ]; then
7+
npx lint-staged
8+
fi
9+

source/nodejs/.lintstagedrc.mjs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { ESLint } from "eslint";
2+
import micromatch from "micromatch";
3+
4+
// As suggested here: https://github.com/okonet/lint-staged#eslint--7-1
5+
const removeIgnoredFiles = async (files) => {
6+
const eslint = new ESLint();
7+
const isIgnored = await Promise.all(
8+
files.map((file) => {
9+
return eslint.isPathIgnored(file);
10+
})
11+
);
12+
const filteredFiles = files.filter((_, i) => !isIgnored[i]);
13+
return filteredFiles.join(" ");
14+
};
15+
16+
export default {
17+
"**/!(package-lock)*": async (files) => {
18+
const filesToLint = await removeIgnoredFiles(micromatch(files, ["**/!(__)*.{ts,tsx,js,jsx}"]));
19+
return [
20+
`eslint --fix --max-warnings=0 ${filesToLint}`,
21+
`prettier --write --ignore-unknown ${files.join(" ")}`
22+
];
23+
}
24+
};

source/nodejs/.prettierrc.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"printWidth": 100,
3+
"trailingComma": "none",
4+
"quoteProps": "preserve"
5+
}

source/nodejs/adaptivecards/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"watch": "webpack --watch",
3030
"start": "webpack-dev-server --open",
3131
"dts": "dts-generator --prefix adaptivecards --project . --out dist/adaptivecards.d.ts",
32-
"lint": "eslint src/*.ts",
32+
"lint": "eslint .",
3333
"prerelease": "npm run clean && npm run build-source",
3434
"release": "webpack --mode=production && npm run dts",
3535
"docs": "npx typedoc"

source/nodejs/adaptivecards/src/__tests__/components/action_set.spec.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
// Licensed under the MIT License.
33
import { ActionSet, SubmitAction } from "../../card-elements";
44

5-
test('ActionSet should be instantiated', () => {
5+
test("ActionSet should be instantiated", () => {
66
const actionSet = new ActionSet();
77
expect(actionSet).toEqual(expect.anything());
8-
})
8+
});
99

10-
test('getActionById returns action', () =>{
11-
const actionSet = new ActionSet()
12-
const action = new SubmitAction()
13-
const id = 'Card.LastActionHero'
14-
action.id = id
10+
test("getActionById returns action", () => {
11+
const actionSet = new ActionSet();
12+
const action = new SubmitAction();
13+
const id = "Card.LastActionHero";
14+
action.id = id;
1515

16-
actionSet.addAction(action)
16+
actionSet.addAction(action);
1717

18-
const result = actionSet.getActionById(id)
18+
const result = actionSet.getActionById(id);
1919

20-
expect(result).not.toBeUndefined()
21-
})
20+
expect(result).not.toBeUndefined();
21+
});

source/nodejs/adaptivecards/src/__tests__/components/adaptive_card.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33
import { AdaptiveCard } from "../../card-elements";
44

5-
test('AdaptiveCard should be instantiated', () => {
5+
test("AdaptiveCard should be instantiated", () => {
66
const adaptiveCard = new AdaptiveCard();
77
expect(adaptiveCard).toEqual(expect.anything());
8-
})
8+
});

source/nodejs/adaptivecards/src/__tests__/components/back_ground_image.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33
import { BackgroundImage } from "../../card-elements";
44

5-
test('BackgroundImage should be instantiated', () => {
5+
test("BackgroundImage should be instantiated", () => {
66
const backgroundImage = new BackgroundImage();
77
expect(backgroundImage).toEqual(expect.anything());
8-
})
8+
});

source/nodejs/adaptivecards/src/__tests__/components/carousel.spec.ts

+31-19
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
3-
import { AdaptiveCard, SerializationContext, TextBlock } from '../../card-elements';
4-
import { Carousel, CarouselPage } from '../../carousel';
5-
import { ValidationEvent } from '../../enums';
6-
import { CarouselConfig } from '../../host-config';
3+
import { AdaptiveCard, SerializationContext, TextBlock } from "../../card-elements";
4+
import { Carousel, CarouselPage } from "../../carousel";
5+
import { ValidationEvent } from "../../enums";
6+
import { CarouselConfig } from "../../host-config";
77

8-
describe('carousels', () => {
9-
describe('when default constructed', () => {
8+
describe("carousels", () => {
9+
describe("when default constructed", () => {
1010
const defaultCarousel = new Carousel();
1111

12-
it('should have reasonable default values', () => {
12+
it("should have reasonable default values", () => {
1313
expect(defaultCarousel.getJsonTypeName()).toBe("Carousel");
1414
expect(defaultCarousel.getItemCount()).toEqual(0);
1515
expect(defaultCarousel.getAllInputs()).toStrictEqual([]);
@@ -18,7 +18,7 @@ describe('carousels', () => {
1818
});
1919
});
2020

21-
describe('when parsed from a valid object', () => {
21+
describe("when parsed from a valid object", () => {
2222
const tinyTimer = 42;
2323

2424
const carouselTheJson = {
@@ -53,11 +53,17 @@ describe('carousels', () => {
5353

5454
let carouselTheObject: Carousel;
5555

56-
it('shouldn\'t throw exceptions', () => {
57-
carouselTheObject = context.parseElement(undefined, carouselTheJson, [], false, true) as Carousel;
56+
it("shouldn't throw exceptions", () => {
57+
carouselTheObject = context.parseElement(
58+
undefined,
59+
carouselTheJson,
60+
[],
61+
false,
62+
true
63+
) as Carousel;
5864
});
5965

60-
it('should have all of its expected properties and child elements', () => {
66+
it("should have all of its expected properties and child elements", () => {
6167
expect(carouselTheObject.timer).toEqual(carouselConfig.minAutoplayDelay);
6268
expect(carouselTheObject.getItemCount()).toBe(2);
6369
for (let i = 0; i < carouselTheObject.getItemCount(); i++) {
@@ -70,7 +76,7 @@ describe('carousels', () => {
7076
}
7177
});
7278

73-
it('shouldn\'t allow anything in pages but CarouselPages', () => {
79+
it("shouldn't allow anything in pages but CarouselPages", () => {
7480
const incorrectTypeCarousel = {
7581
"type": "Carousel",
7682
"pages": [
@@ -80,15 +86,21 @@ describe('carousels', () => {
8086
]
8187
};
8288

83-
let incorrectTypeParsed = context.parseElement(undefined, incorrectTypeCarousel, [], false, true) as Carousel;
89+
let incorrectTypeParsed = context.parseElement(
90+
undefined,
91+
incorrectTypeCarousel,
92+
[],
93+
false,
94+
true
95+
) as Carousel;
8496
expect(incorrectTypeParsed.getItemCount()).toBe(0);
8597
expect(context.eventCount).toBe(1);
8698
expect(context.getEventAt(0).event).toBe(ValidationEvent.ElementTypeNotAllowed);
8799
});
88100
});
89101

90-
describe('certain elements', () => {
91-
it('shouldn\'t be allowed within a Carousel', () => {
102+
describe("certain elements", () => {
103+
it("shouldn't be allowed within a Carousel", () => {
92104
const carouselCard = {
93105
"type": "AdaptiveCard",
94106
"body": {
@@ -128,12 +140,12 @@ describe('carousels', () => {
128140
expect(theCard.getActionCount()).toBe(0);
129141

130142
expect(context.eventCount).toBe(2);
131-
})
143+
});
132144
});
133145
});
134146

135-
describe('carousel pages', () => {
136-
it('shouldn\'t be parseable outside of a Carousel', () => {
147+
describe("carousel pages", () => {
148+
it("shouldn't be parseable outside of a Carousel", () => {
137149
const context = new SerializationContext();
138150
const pageObject = {
139151
"type": "CarouselPage",
@@ -143,7 +155,7 @@ describe('carousel pages', () => {
143155
"text": "unthinkable!"
144156
}
145157
]
146-
}
158+
};
147159
expect(context.parseElement(undefined, pageObject, [], false)).toBeUndefined();
148160
expect(context.eventCount).toBe(1);
149161
expect(context.getEventAt(0).event).toBe(ValidationEvent.UnknownElementType);

source/nodejs/adaptivecards/src/__tests__/components/column.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33
import { Column } from "../../card-elements";
44

5-
test('Column should be instantiated', () => {
5+
test("Column should be instantiated", () => {
66
const column = new Column();
77
expect(column).toEqual(expect.anything());
8-
})
8+
});

source/nodejs/adaptivecards/src/__tests__/components/column_set.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33
import { ColumnSet } from "../../card-elements";
44

5-
test('Container should be instantiated', () => {
5+
test("Container should be instantiated", () => {
66
const columnSet = new ColumnSet();
77
expect(columnSet).toEqual(expect.anything());
8-
})
8+
});

source/nodejs/adaptivecards/src/__tests__/components/container.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33
import { Container } from "../../card-elements";
44

5-
test('Container should be instantiated', () => {
5+
test("Container should be instantiated", () => {
66
const container = new Container();
77
expect(container).toEqual(expect.anything());
8-
})
8+
});
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
3-
import {Fact} from "../../card-elements";
3+
import { Fact } from "../../card-elements";
44

5-
test('Fact should be instantiated', ()=>{
5+
test("Fact should be instantiated", () => {
66
const fact = new Fact();
77
expect(fact).toEqual(expect.anything());
8-
})
8+
});
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
3-
import {FactSet} from "../../card-elements";
3+
import { FactSet } from "../../card-elements";
44

5-
test('FactSet should be instantiated', ()=>{
5+
test("FactSet should be instantiated", () => {
66
const factSet = new FactSet();
77
expect(factSet).toEqual(expect.anything());
8-
})
8+
});

source/nodejs/adaptivecards/src/__tests__/components/http_action.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33
import { HttpAction } from "../../card-elements";
44

5-
test('HttpAction should be instantiate', () => {
5+
test("HttpAction should be instantiate", () => {
66
const httpAction = new HttpAction();
77
expect(httpAction).toEqual(expect.anything());
8-
})
8+
});
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
3-
import {Image} from "../../card-elements";
3+
import { Image } from "../../card-elements";
44

5-
test('Image should be instantiated', ()=>{
5+
test("Image should be instantiated", () => {
66
const image = new Image();
77
expect(image).toEqual(expect.anything());
88
expect(image.altText).toBeUndefined();
9-
})
9+
});
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
3-
import {ImageSet} from "../../card-elements";
3+
import { ImageSet } from "../../card-elements";
44

5-
test('ImageSet should be instantiated', ()=>{
5+
test("ImageSet should be instantiated", () => {
66
const imageSet = new ImageSet();
77
expect(imageSet).toEqual(expect.anything());
8-
})
8+
});
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
3-
import {OpenUrlAction} from "../../card-elements";
3+
import { OpenUrlAction } from "../../card-elements";
44

5-
test('OpenUrlAction should be instantiated', ()=>{
5+
test("OpenUrlAction should be instantiated", () => {
66
const openUrlAction = new OpenUrlAction();
77
expect(openUrlAction).toEqual(expect.anything());
8-
})
8+
});

source/nodejs/adaptivecards/src/__tests__/components/show_card_action.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33
import { ShowCardAction } from "../../card-elements";
44

5-
test('ShowCardAction should be instantiated', () => {
5+
test("ShowCardAction should be instantiated", () => {
66
const showCardAction = new ShowCardAction();
77
expect(showCardAction).toEqual(expect.anything());
8-
})
8+
});
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
3-
import {SubmitAction} from "../../card-elements";
3+
import { SubmitAction } from "../../card-elements";
44

5-
test('SubmitAction should be instantiated', ()=>{
5+
test("SubmitAction should be instantiated", () => {
66
const submitAction = new SubmitAction();
77
expect(submitAction).toEqual(expect.anything());
8-
})
8+
});

source/nodejs/adaptivecards/src/__tests__/components/text_block.spec.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
3-
import {AdaptiveCard,TextBlock} from "../../card-elements";
3+
import { AdaptiveCard, TextBlock } from "../../card-elements";
44

5-
test('TextBlock should be instantiated', ()=>{
5+
test("TextBlock should be instantiated", () => {
66
const textBlock = new TextBlock();
77
expect(textBlock).toEqual(expect.anything());
8-
})
8+
});
99

1010
const emoji_message = "Mix 🗣 emoji inside 🙌 text";
1111
const simple_test_card = {
1212
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
1313
"type": "AdaptiveCard",
1414
"version": "1.0",
15-
"body": [{
16-
"type": "TextBlock",
17-
"text": emoji_message
18-
}]
15+
"body": [
16+
{
17+
"type": "TextBlock",
18+
"text": emoji_message
19+
}
20+
]
1921
};
2022

2123
// TODO: confirm this test working as expected

0 commit comments

Comments
 (0)