Skip to content

Commit

Permalink
fix: lint code in CI (omnivore-app#161)
Browse files Browse the repository at this point in the history
* fix: lint code in CI

* fix github ci

* add eslint dependancy
  • Loading branch information
sywhb committed Nov 14, 2023
1 parent c72268e commit 460c489
Show file tree
Hide file tree
Showing 17 changed files with 1,162 additions and 811 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ indent_style = tab
indent_size = 4
tab_width = 4

[*.yml]
indent_style = space
indent_size = 2

# 2 space indentation
[*.{ts,js}]
indent_style = space
Expand Down
4 changes: 2 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
npm node_modules
build
node_modules
build
44 changes: 21 additions & 23 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"env": { "node": true },
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { "args": "none" }],
"@typescript-eslint/ban-ts-comment": "off",
"no-prototype-builtins": "off",
"@typescript-eslint/no-empty-function": "off",
"semi": ["error", "always"]
}
}
"root": true,
"parser": "@typescript-eslint/parser",
"env": { "node": true },
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { "args": "none" }],
"@typescript-eslint/ban-ts-comment": "off",
"no-prototype-builtins": "off",
"@typescript-eslint/no-empty-function": "off",
"semi": [2, "never"]
}
}
47 changes: 24 additions & 23 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,29 @@
name: Node.js CI

on:
push:
branches: [ master, main, develop ]
pull_request:
branches: [ master, main, develop ]
push:
branches: [ master, main, develop ]
pull_request:
branches: [ master, main, develop ]

jobs:
build:
runs-on: ${{ matrix.os }}
continue-on-error: true
strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
node-version: [ 14.x, 16.x, 18.x, 19.x ]
max-parallel: 24
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: yarn
# before the first 'run' each "run" is a script from your project:
# - run: npm run prettier
# - run: npm run linter
- run: yarn test
build:
runs-on: ${{ matrix.os }}
continue-on-error: true
strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
node-version: [ 14.x, 16.x, 18.x, 19.x ]
max-parallel: 24
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: yarn
- name: Lint
run: yarn lint
- name: Test
run: yarn test
4 changes: 2 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"semi": true,
"singleQuote": false
"semi": false,
"singleQuote": true
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"dev": "node esbuild.config.mjs",
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
"version": "node version-bump.mjs && git add manifest.json versions.json",
"lint": "eslint ./src --ext .ts",
"lint:fix": "eslint ./src --ext .ts --fix",
"format": "prettier --write ./src/**/*.ts",
"test": "jest",
"test:watch": "jest --watch",
"test:ci": "jest --ci --reporters='default' --reporters='./github-actions-reporter'",
Expand All @@ -33,6 +36,7 @@
"@typescript-eslint/parser": "5.29.0",
"builtin-modules": "3.3.0",
"esbuild": "0.14.47",
"eslint": "^8.53.0",
"jest": "^29.4.3",
"jest-cli": "^29.4.3",
"jest-junit-reporter": "^1.1.0",
Expand Down
12 changes: 6 additions & 6 deletions src/__mocks__/obsidian.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const mockObsidianApp = {
// Mock implementation of the App API
app: {
platform: () => "desktop",
platform: () => 'desktop',
plugins: {
getPlugins: () => [],
isEnabled: () => true,
Expand All @@ -11,20 +11,20 @@ export const mockObsidianApp = {
// Mock implementation of the Workspace API
workspace: {
onLayoutReady: (callback: () => void) => {
setTimeout(callback, 0);
setTimeout(callback, 0)
},
getLeavesOfType: () => [],
getConfig: () => ({}),
},

// Mock implementation of the MarkdownView API
markdownView: {
getMode: () => "source",
getMarkdown: () => "",
getMode: () => 'source',
getMarkdown: () => '',
},
};
}

// Mock implementation of the Obsidian global object
export const obsidian = {
...mockObsidianApp,
};
}
144 changes: 72 additions & 72 deletions src/__tests__/formatDate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
import { DateTime } from "luxon";
import { DEFAULT_SETTINGS } from "../settings";
import { formatDate } from "../util";
import { DateTime } from 'luxon'
import { DEFAULT_SETTINGS } from '../settings'
import { formatDate } from '../util'

const jsDate = new Date("2023-02-18 13:02:08.169");
const apiDate = jsDate.toISOString(); // API returns ISO 8601 date strings
const jsDate = new Date('2023-02-18 13:02:08.169')
const apiDate = jsDate.toISOString() // API returns ISO 8601 date strings

type testCase = {
format: string;
date: string;
expected: string;
};
format: string
date: string
expected: string
}

const luxonHierarchicalFormatWithTime = {
date: apiDate,
expected: "2023/2023-02/2023-02-18/130208",
format: "yyyy/yyyy-MM/yyyy-MM-dd/HHmmss",
};
expected: '2023/2023-02/2023-02-18/130208',
format: 'yyyy/yyyy-MM/yyyy-MM-dd/HHmmss',
}
const luxonHierarchicalFormat = {
date: apiDate,
expected: "2023/2023-02/2023-02-18",
format: "yyyy/yyyy-MM/yyyy-MM-dd",
};
expected: '2023/2023-02/2023-02-18',
format: 'yyyy/yyyy-MM/yyyy-MM-dd',
}
const defaultDateHighlightedFormatTestCase: testCase = {
date: apiDate,
expected: "2023-02-18 13:02:08",
expected: '2023-02-18 13:02:08',
format: DEFAULT_SETTINGS.dateHighlightedFormat,
};
}
const defaultDateSavedFormatTestCase: testCase = {
date: apiDate,
expected: "2023-02-18 13:02:08",
expected: '2023-02-18 13:02:08',
format: DEFAULT_SETTINGS.dateSavedFormat,
};
}
const defaultFolderDateFormatTestCase: testCase = {
date: apiDate,
expected: "2023-02-18",
expected: '2023-02-18',
format: DEFAULT_SETTINGS.folderDateFormat,
};
}
const testCases: testCase[] = [
defaultDateHighlightedFormatTestCase,
defaultDateSavedFormatTestCase,
defaultFolderDateFormatTestCase,
luxonHierarchicalFormat,
luxonHierarchicalFormatWithTime,
];
describe("ensure default formats are as expected", () => {
test("dateHighlightedFormat", () => {
expect(DEFAULT_SETTINGS.dateHighlightedFormat).toBe("yyyy-MM-dd HH:mm:ss");
});
test("dateSavedFormat", () => {
expect(DEFAULT_SETTINGS.dateSavedFormat).toBe("yyyy-MM-dd HH:mm:ss");
});
test("folderDateFormat", () => {
expect(DEFAULT_SETTINGS.folderDateFormat).toBe("yyyy-MM-dd");
});
});
]
describe('ensure default formats are as expected', () => {
test('dateHighlightedFormat', () => {
expect(DEFAULT_SETTINGS.dateHighlightedFormat).toBe('yyyy-MM-dd HH:mm:ss')
})
test('dateSavedFormat', () => {
expect(DEFAULT_SETTINGS.dateSavedFormat).toBe('yyyy-MM-dd HH:mm:ss')
})
test('folderDateFormat', () => {
expect(DEFAULT_SETTINGS.folderDateFormat).toBe('yyyy-MM-dd')
})
})

describe("formatDate on known formats", () => {
test.each(testCases)("should correctly format %s", (testCase) => {
const result = formatDate(testCase.date, testCase.format);
expect(result).toBe(testCase.expected);
});
});
describe('formatDate on known formats', () => {
test.each(testCases)('should correctly format %s', (testCase) => {
const result = formatDate(testCase.date, testCase.format)
expect(result).toBe(testCase.expected)
})
})

function generateRandomISODateStrings(quantity: number): string[] {
const randomISODateStrings: string[] = [];
const timeZones = Intl.DateTimeFormat().resolvedOptions().timeZone.split(",");
const randomISODateStrings: string[] = []
const timeZones = Intl.DateTimeFormat().resolvedOptions().timeZone.split(',')

for (let i = 0; i < quantity; i++) {
const date = new Date(
Expand All @@ -77,80 +77,80 @@ function generateRandomISODateStrings(quantity: number): string[] {
Math.floor(Math.random() * 60),
Math.floor(Math.random() * 1000)
)
);
)

// Randomly select a timezone from the available time zones
const randomTimeZone =
timeZones[Math.floor(Math.random() * timeZones.length)];
timeZones[Math.floor(Math.random() * timeZones.length)]

// Convert the generated date to the randomly selected timezone
// const dateTimeWithZone = DateTime.fromJSDate(date, { zone: randomTimeZone }).toUTC();
const jsDateTimeWithZone = new Date(
date.toLocaleString("en-US", { timeZone: randomTimeZone })
);
const luxonDate = DateTime.fromJSDate(jsDateTimeWithZone);
randomISODateStrings.push(luxonDate.toISO() as string);
date.toLocaleString('en-US', { timeZone: randomTimeZone })
)
const luxonDate = DateTime.fromJSDate(jsDateTimeWithZone)
randomISODateStrings.push(luxonDate.toISO() as string)
}

return randomISODateStrings;
return randomISODateStrings
}

describe("formatDate on random dates", () => {
describe('formatDate on random dates', () => {
test.each(generateRandomISODateStrings(100))(
"should correctly format %s",
'should correctly format %s',
(date) => {
const result = formatDate(date, "yyyy-MM-dd HH:mm:ss");
const result = formatDate(date, 'yyyy-MM-dd HH:mm:ss')
// test with regex to ensure the format is correct
expect(result).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
expect(result).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/)
}
);
});
)
})

function getCasesWithRandomDates(
testFormats: string[],
quantity = 10
): {
date: string;
luxonFormat: string;
date: string
luxonFormat: string
}[] {
return testFormats.flatMap((luxonFormat) =>
generateRandomISODateStrings(quantity).map((date) => ({
date,
luxonFormat,
}))
);
)
}

describe("round trip on random dates", () => {
describe('round trip on random dates', () => {
const testFormats = [
defaultDateHighlightedFormatTestCase.format,
defaultDateSavedFormatTestCase.format,
defaultFolderDateFormatTestCase.format,
];
]
// generate permutations of testCases.formats and 10 generated each
const casesWithRandomDates = getCasesWithRandomDates(testFormats);
const casesWithRandomDates = getCasesWithRandomDates(testFormats)
test.each(casesWithRandomDates)(
"should be unchanged after round trip %s",
'should be unchanged after round trip %s',
(testCase) => {
const result = formatDate(testCase.date, testCase.luxonFormat);
const result2 = formatDate(result, testCase.luxonFormat);
expect(result2).toBe(result);
const result = formatDate(testCase.date, testCase.luxonFormat)
const result2 = formatDate(result, testCase.luxonFormat)
expect(result2).toBe(result)
}
);
)

const atypicalFormats = [
luxonHierarchicalFormat.format,
luxonHierarchicalFormatWithTime.format,
];
]
test.each(getCasesWithRandomDates(atypicalFormats))(
"should be unchanged after round trip with atypical format %s",
'should be unchanged after round trip with atypical format %s',
(testCase) => {
const formattedDate = formatDate(testCase.date, testCase.luxonFormat);
const formattedDate = formatDate(testCase.date, testCase.luxonFormat)
const parsedDate = DateTime.fromFormat(
formattedDate,
testCase.luxonFormat
);
expect(parsedDate.isValid).toBe(true);
)
expect(parsedDate.isValid).toBe(true)
}
);
});
)
})
Loading

0 comments on commit 460c489

Please sign in to comment.