Skip to content

Commit 58d8c83

Browse files
committed
restore mocha and eslint for typescript
1 parent e802a1c commit 58d8c83

File tree

11 files changed

+427
-34
lines changed

11 files changed

+427
-34
lines changed

.eslintrc.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{
2-
"extends": ["eslint:recommended", "prettier"],
3-
"parserOptions": {
4-
"sourceType": "module",
5-
"ecmaVersion": 2021
6-
},
2+
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "prettier"],
3+
"plugins": ["@typescript-eslint"],
4+
"parser": "@typescript-eslint/parser",
75
"env": {
86
"es2020": true,
97
"node": true,
@@ -14,6 +12,9 @@
1412
"no-constant-condition": 0,
1513
"no-sparse-arrays": 0,
1614
"no-unexpected-multiline": 0,
17-
"no-unused-vars": ["error", {"ignoreRestSiblings": true}]
15+
"@typescript-eslint/no-empty-function": 0,
16+
"@typescript-eslint/no-explicit-any": 0,
17+
"@typescript-eslint/no-this-alias": 0,
18+
"@typescript-eslint/no-unused-vars": ["error", {"ignoreRestSiblings": true}]
1819
}
1920
}

.mocharc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"require": ["module-alias/register.js"]
2+
"require": ["module-alias/register.js"],
3+
"loader": "./loader.js",
4+
"extensions": ["js", "ts"]
35
}

jsconfig.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

loader.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {transform, applySourceMap, installSourceMapSupport, resolveTsPath} from "@esbuild-kit/core-utils";
2+
import {getTsconfig} from "get-tsconfig";
3+
4+
const sourcemaps = installSourceMapSupport();
5+
const tsExtensionsPattern = /\.([cm]?ts|[tj]sx)$/;
6+
const tsconfig = getTsconfig();
7+
const tsconfigRaw = tsconfig?.config;
8+
9+
export async function resolve(specifier, context, defaultResolve) {
10+
const tsPath = resolveTsPath(specifier);
11+
if (tsPath) {
12+
try {
13+
return await resolve(tsPath, context, defaultResolve, true);
14+
} catch (error) {
15+
if (error.code !== "ERR_MODULE_NOT_FOUND") {
16+
throw error;
17+
}
18+
}
19+
}
20+
const resolved = await defaultResolve(specifier, context, defaultResolve);
21+
return resolved.format === undefined ? {...resolved, format: "module"} : resolved;
22+
}
23+
24+
export async function load(url, context, defaultLoad) {
25+
const loaded = await defaultLoad(url, context, defaultLoad);
26+
if (tsExtensionsPattern.test(url)) {
27+
const code = loaded.source.toString();
28+
const transformed = await transform(code, url, {tsconfigRaw});
29+
return {
30+
format: "module",
31+
source: applySourceMap(transformed, url, sourcemaps)
32+
};
33+
}
34+
return loaded;
35+
}

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
"src/**/*.d.ts"
2828
],
2929
"scripts": {
30-
"test": "yarn test:mocha && yarn test:lint",
30+
"test": "yarn test:mocha && yarn test:lint && yarn test:tsc",
3131
"test:mocha": "mkdir -p test/output && mocha 'test/**/*-test.*' 'test/plot.js'",
3232
"test:lint": "eslint src test",
33+
"test:tsc": "tsc",
3334
"prepublishOnly": "rm -rf build dist && node scripts/readme-to-jsdoc.js && rollup -c",
3435
"postpublish": "git push && git push --tags",
3536
"dev": "vite"
@@ -41,14 +42,18 @@
4142
"./src/plot.js"
4243
],
4344
"devDependencies": {
45+
"@esbuild-kit/core-utils": "^2.0.2",
4446
"@rollup/plugin-commonjs": "^23.0.4",
4547
"@rollup/plugin-json": "^5.0.2",
4648
"@rollup/plugin-node-resolve": "^15.0.1",
4749
"@rollup/plugin-terser": "^0.1.0",
50+
"@typescript-eslint/eslint-plugin": "^5.54.1",
51+
"@typescript-eslint/parser": "^5.54.1",
4852
"canvas": "^2.0.0",
4953
"d3-geo-projection": "^4.0.0",
5054
"eslint": "^8.16.0",
5155
"eslint-config-prettier": "^8.5.0",
56+
"get-tsconfig": "^4.1.0",
5257
"htl": "^0.3.0",
5358
"js-beautify": "1",
5459
"jsdom": "^21.0.0",
@@ -58,6 +63,7 @@
5863
"prettier": "^2.7.1",
5964
"rollup": "^3.7.0",
6065
"topojson-client": "^3.1.0",
66+
"typescript": "^4.9.5",
6167
"vite": "^4.0.0"
6268
},
6369
"dependencies": {

src/mark.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {Dimensions} from "./dimensions.js";
22
import type {ChannelDomainSort, ChannelSpec, ChannelValue, ChannelValueSpec} from "./channel.js";
33
import type {Context} from "./context.js";
44
import type {Facet, FacetAnchor} from "./facet.js";
5+
import type {plot} from "./plot.js";
56

67
export type Data = any[]; // TODO iterable, arquero, arrow, etc.
78

@@ -61,3 +62,6 @@ export interface MarkOptions {
6162

6263
/** @jsdoc Mark */
6364
export class Mark {} // TODO
65+
66+
/** @jsdoc marks */
67+
export function marks(...marks: Markish[]): Markish[] & {plot: typeof plot};

src/plot.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,3 @@ export interface Plot {
7272

7373
/** @jsdoc plot */
7474
export function plot(options?: PlotOptions): (SVGSVGElement | HTMLElement) & Plot;
75-
76-
/** @jsdoc marks */
77-
export function marks(...marks: Markish[]): Markish[] & {plot: typeof plot};

test/plots/empty.js renamed to test/plots/empty.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as Plot from "@observablehq/plot";
22
import {svg} from "htl";
33

4-
export default async function () {
4+
export async function empty() {
55
return Plot.plot({
66
grid: true,
77
inset: 6,

test/plots/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ export {default as dotSort} from "./dot-sort.js";
6060
export {default as downloads} from "./downloads.js";
6161
export {default as downloadsOrdinal} from "./downloads-ordinal.js";
6262
export {default as driving} from "./driving.js";
63-
export {default as empty} from "./empty.js";
6463
export {default as emptyFacet} from "./empty-facet.js";
6564
export {default as emptyLegend} from "./empty-legend.js";
6665
export {default as emptyX} from "./empty-x.js";
@@ -287,6 +286,7 @@ export * from "./axis-labels.js";
287286
export * from "./bigint.js";
288287
export * from "./bin-1m.js";
289288
export * from "./electricity-demand.js";
289+
export * from "./empty.ts";
290290
export * from "./federal-funds.js";
291291
export * from "./frame.js";
292292
export * from "./function-contour.js";

tsconfig.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"noEmit": true,
5+
"module": "Node16",
6+
"baseUrl": "./src",
7+
"paths": {
8+
"@observablehq/plot": ["index"]
9+
}
10+
},
11+
"include": [
12+
"src/**/*.d.ts",
13+
"src/**/*.js",
14+
"src/**/*.ts",
15+
"test/**/*.js",
16+
"test/**/*.ts",
17+
]
18+
}

0 commit comments

Comments
 (0)