Skip to content

Commit 1904d86

Browse files
author
Björn Büttner
committed
ignore typescript false positives
1 parent 93b9f0f commit 1904d86

File tree

10 files changed

+96
-59
lines changed

10 files changed

+96
-59
lines changed

.npmignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/test
2+
.eslintrc.yml
3+
/.nyc_output
4+
/coverage
5+
/.mocharc.cjs
6+
/.nycrc.json
7+
/fixtures
8+
/tools

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This is a small library used to teach the default react routers file based routi
44

55
## Defining a page
66

7-
A page is by default any ìndex.tsx within the src/pages folder. The folder name will be taken as the page url, with two exceptions:
7+
A page is by default any `ìndex.tsx` within the `src/pages` folder. The folder name will be taken as the page url, with two exceptions by default:
88

99
- home will be the root
1010
- not-found will be the 404 page
@@ -54,6 +54,6 @@ Default options are overwritten first by the file based settings and then by the
5454

5555
You will need to do three things for the complete package:
5656

57-
- Define a call to generate-routes in you package.json as afterInstall and early in your build process to generate the routes.tsx and the sitemap
58-
- Optionally define a call to generate-folders in your package.json as a late build step to optimise the index.html files
59-
- Import the generated src/routes.tsx, so that you can use createBrowserRouter or similar functions to create your router
57+
- Define a call to generate-routes(`irfbrgr` or `react-file-based-routes-generate-routes`) in you package.json as afterInstall and early in your build process to generate the routes.tsx and the sitemap
58+
- Optionally define a call to generate-folders(`ìrfbrgf` or `react-file-based-routes-generate-folders`) in your package.json as a late build step to optimise the index.html files
59+
- Default import the generated `src/routes.tsx`, so that you can use createBrowserRouter or similar functions to create your router. It requires getting a `Loader` element passed to it - any react element will do

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
"version": "1.0.0",
44
"description": "A simple file based routing library, that does not force itself on you.",
55
"type": "module",
6+
"keywords": [
7+
"react",
8+
"routing",
9+
"file-based-routing",
10+
"library"
11+
],
612
"bin": {
713
"react-file-based-routes-generate-routes": "bin/generate-routes.js",
814
"react-file-based-routes-generate-folders": "bin/generate-folders.js",
@@ -25,6 +31,9 @@
2531
"react": "^18.2.0"
2632
},
2733
"scripts": {
28-
"tsc": "tsc -p tsconfig.json"
34+
"tsc": "tsc -p tsconfig.json",
35+
"prepublishOnly": "npm run tsc",
36+
"clear": "rm src/*.js",
37+
"postpublish": "npm run clear"
2938
}
3039
}

src/configuration.ts

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,41 @@ import {
33
existsSync,
44
} from 'fs';
55

6-
export interface Configuration {
7-
sitemap: {
8-
domain: string,
9-
build: boolean,
10-
},
11-
routes: {
12-
build: boolean,
13-
type: 'tsx'|'jsx',
14-
overridePathMappings: {
15-
[filesystemPath: string]: string,
16-
},
17-
},
18-
htmlMinify: {
19-
collapseBooleanAttributes: boolean,
20-
conservativeCollapse: boolean,
21-
collapseWhitespace: boolean,
22-
removeAttributeQuotes: boolean,
23-
removeComments: boolean,
24-
},
25-
fileBuilder: {
26-
buildIndex: boolean,
27-
minifyPages: boolean,
28-
appendPageChunk: boolean,
29-
},
30-
fileFinder: {
31-
fileName: string,
32-
pagesRoot: string,
33-
distJSRoot: string,
6+
interface Sitemap {
7+
domain: string,
8+
build: boolean,
9+
}
10+
interface Routes {
11+
build: boolean,
12+
type: 'tsx'|'jsx',
13+
overridePathMappings: {
14+
[filesystemPath: string]: string,
3415
},
3516
}
17+
interface HTMLMinify {
18+
collapseBooleanAttributes: boolean,
19+
conservativeCollapse: boolean,
20+
collapseWhitespace: boolean,
21+
removeAttributeQuotes: boolean,
22+
removeComments: boolean,
23+
}
24+
interface FileBuilder {
25+
buildIndex: boolean,
26+
minifyPages: boolean,
27+
appendPageChunk: boolean,
28+
}
29+
interface FileFinder {
30+
fileName: string,
31+
pagesRoot: string,
32+
distJSRoot: string,
33+
}
34+
export interface Configuration {
35+
sitemap: Sitemap,
36+
routes: Routes,
37+
htmlMinify: HTMLMinify,
38+
fileBuilder: FileBuilder,
39+
fileFinder: FileFinder,
40+
}
3641

3742
export default (cwd: string, cliArguments: string[]): Configuration => {
3843
const config: Configuration = {
@@ -68,11 +73,11 @@ export default (cwd: string, cliArguments: string[]): Configuration => {
6873
};
6974
const configFile = cwd + '/.idrinth-react-file-based-routes.json';
7075
if (existsSync(configFile)) {
71-
const userconfig: Partial<Configuration> = JSON.parse(readFileSync(configFile, 'utf-8'));
76+
const userconfig: Configuration = JSON.parse(readFileSync(configFile, 'utf-8'));
7277
if (typeof userconfig === 'object') {
73-
for (const prop of Object.keys(config)) {
78+
for (const prop of Object.keys(config) as [keyof Configuration]) {
7479
if (typeof userconfig[prop] === 'object' && typeof config[prop] === 'object') {
75-
for (const setting of Object.keys(config[prop])) {
80+
for (const setting of Object.keys(config[prop]) as [keyof Configuration[typeof prop]]) {
7681
if (typeof config[prop][setting] === typeof userconfig[prop][setting]) {
7782
config[prop][setting] = userconfig[prop][setting];
7883
}
@@ -83,12 +88,24 @@ export default (cwd: string, cliArguments: string[]): Configuration => {
8388
}
8489
for (const param of cliArguments) {
8590
if (param.startsWith('--')) {
86-
const [setting, value,] = param.substring(2).split('=');
87-
const [group, detail,] = setting.split('.');
91+
const [setting, value,] = param.substring(2).split('=') as [string, undefined|string];
92+
const [group, detail,] = setting.split('.') as [keyof Configuration, keyof Sitemap|keyof Routes|keyof FileFinder|keyof FileBuilder|keyof HTMLMinify];
8893
if (group && typeof config[group] === 'object' && detail) {
94+
// @ts-ignore TS7053
8995
if (typeof config[group][detail] === 'boolean') {
90-
config[group][detail] = !config[group][detail];
91-
} else if(typeof config[group][detail] === 'string' && value) {
96+
if (value === 'true') {
97+
// @ts-ignore TS7053
98+
config[group][detail] = true;
99+
} else if (value === 'false') {
100+
// @ts-ignore TS7053
101+
config[group][detail] = false;
102+
} else {
103+
// @ts-ignore TS7053
104+
config[group][detail] = !config[group][detail];
105+
}
106+
// @ts-ignore TS7053
107+
} else if (typeof config[group][detail] === 'string' && value) {
108+
// @ts-ignore TS7053
92109
config[group][detail] = value;
93110
}
94111
}
@@ -97,5 +114,8 @@ export default (cwd: string, cliArguments: string[]): Configuration => {
97114
if (config.sitemap.build && !config.sitemap.domain) {
98115
throw new Error('sitemap.domain must be set if a sitemap should be build.');
99116
}
117+
if (config.routes.type !== 'tsx' && config.routes.type !== 'jsx') {
118+
throw new Error('config.routes.type must be set to either tsx or jsx.');
119+
}
100120
return config;
101121
}

src/generate-routes.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {readdirSync, statSync, writeFileSync,} from 'fs';
22
import paddedDate from './padded-date.js';
33
import {Configuration} from "./configuration.js";
4-
import TsxWriter from "./tsx-writer";
5-
import JsxWriter from "./jsx-writer";
6-
import SitemapWriter from "./sitemap-writer";
7-
import Writer from "./writer";
4+
import TsxWriter from './tsx-writer.js';
5+
import JsxWriter from './jsx-writer.js';
6+
import SitemapWriter from './sitemap-writer.js';
7+
import Writer from './writer.js';
88

99
export default (cwd: string, configuration: Configuration) => {
1010
const writers: Writer[] = [];
@@ -15,7 +15,7 @@ export default (cwd: string, configuration: Configuration) => {
1515
writers.push(new JsxWriter())
1616
}
1717
if (configuration.sitemap.build) {
18-
writers.push(new SitemapWriter());
18+
writers.push(new SitemapWriter(configuration.sitemap.domain));
1919
}
2020
for (const file of readdirSync(cwd + '/' + configuration.fileFinder.pagesRoot, {encoding: 'utf8', recursive: true})) {
2121
if (file.endsWith('/' + configuration.fileFinder.fileName) || file.endsWith('\\' + configuration.fileFinder.fileName)) {
@@ -24,7 +24,7 @@ export default (cwd: string, configuration: Configuration) => {
2424
const path = file.replace(/\\/ug, '/').substring(0, file.length - configuration.fileFinder.fileName.length - 1);
2525
const url = typeof configuration.routes.overridePathMappings[path] === 'string' ? configuration.routes.overridePathMappings[path] : `/${path}/`;
2626
for (const writer of writers) {
27-
writer.add(path, url, configuration.sitemap.domain, changed);
27+
writer.add(path, url, changed);
2828
}
2929
}
3030
}

src/jsx-writer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export default class JsxWriter implements Writer {
55
return 'src/routes.jsx';
66
}
77
private items: string[] = [];
8-
add(path: string, url: string, domain: string, changed: string): void
8+
// @ts-ignore TS6133
9+
add(path: string, url: string, changed: string): void
910
{
1011
this.items.push(' ' + `(() => {
1112
const LazyElement = lazy(() => import(

src/sitemap-writer.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ export default class SitemapWriter implements Writer {
55
return 'public/sitemap.xml';
66
}
77
private items: string[] = [];
8-
add(path: string, url: string, domain: string, changed: string): void
8+
constructor(private readonly domain: string) {
9+
}
10+
// @ts-ignore TS6133
11+
add(path: string, url: string, changed: string): void
912
{
1013
if (url === '*') {
1114
return;
1215
}
13-
this.items.push(`<url><loc>https://${domain}/${url}/</loc><lastmod>${changed}</lastmod></url>`);
16+
this.items.push(`<url><loc>https://${this.domain}/${url}/</loc><lastmod>${changed}</lastmod></url>`);
1417
}
1518
toString(): string {
1619
return '<?xml version="1.0" encoding="UTF-8"?>' +

src/tsx-writer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export default class TsxWriter implements Writer {
55
return 'src/routes.tsx';
66
}
77
private items: string[] = [];
8-
add(path: string, url: string, domain: string, changed: string): void
8+
// @ts-ignore TS6133
9+
add(path: string, url: string, changed: string): void
910
{
1011
this.items.push(' ' + `(() => {
1112
const LazyElement = lazy(() => import(

src/writer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default interface Writer {
2-
add(path: string, url: string, domain: string, changed: string): void;
2+
add(path: string, url: string, changed: string): void;
33
toString(): string;
44
fileName(): string;
55
}

tsconfig.json

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@
22
"compilerOptions": {
33
"noImplicitAny": true,
44
"target": "ES2020",
5-
"useDefineForClassFields": true,
6-
"module": "ESNext",
7-
"skipLibCheck": true,
8-
"moduleResolution": "bundler",
9-
"resolveJsonModule": true,
10-
"isolatedModules": true,
5+
"module": "NodeNext",
116
"jsx": "react",
127
"strict": true,
138
"noUnusedLocals": true,
149
"noUnusedParameters": true,
1510
"noFallthroughCasesInSwitch": true,
16-
"incremental": true,
17-
"allowSyntheticDefaultImports": true
11+
"allowSyntheticDefaultImports": true,
12+
"moduleResolution": "NodeNext"
1813
},
1914
"include": [
2015
"src/**/*.ts"

0 commit comments

Comments
 (0)