Skip to content

Commit

Permalink
Use ts imports, update eslint config
Browse files Browse the repository at this point in the history
With esbuild, we can finally import `.ts` files as `.ts`, which is more
natural. The ESLint config is updated to check imports for this
extension. Some outdated ESLint rules are removed.
  • Loading branch information
ralfstx committed Feb 19, 2024
1 parent 8a9af5e commit ec8a874
Show file tree
Hide file tree
Showing 64 changed files with 233 additions and 240 deletions.
11 changes: 2 additions & 9 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,19 @@ module.exports = {
mocha: true,
},
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: 2021,
sourceType: 'module',
},
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'],
plugins: ['@typescript-eslint', 'import', 'simple-import-sort'],
rules: {
eqeqeq: ['error', 'always', { null: 'ignore' }],
// TODO disable console again, only the CLI should print to the console
'no-console': ['error', { allow: ['warn', 'error'] }],
'object-shorthand': 'error',
'prefer-const': ['error', { destructuring: 'all' }],
'simple-import-sort/imports': 'error',
// no-unresolved cannot resolve modules due to different extension
'import/no-unresolved': 'off',
'import/extensions': ['error', 'ignorePackages', { js: 'always' }],
'import/extensions': ['error', 'ignorePackages', { js: 'never', ts: 'always' }],
'import/no-default-export': 'error',
// TODO temporarily disabled to support legacy code, re-enable
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-namespace': ['error', { allowDeclarations: true }],
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
},
};
6 changes: 3 additions & 3 deletions src/api/content.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Color } from './colors.js';
import { Length } from './sizes.js';
import { Orientation, PaperSize } from './sizes.js';
import { Color } from './colors.ts';
import { Length } from './sizes.ts';
import { Orientation, PaperSize } from './sizes.ts';

/**
* The complete definition of a document to create.
Expand Down
2 changes: 1 addition & 1 deletion src/api/make-pdf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import crypto from 'node:crypto';

import { describe, expect, it } from 'vitest';

import { makePdf } from './make-pdf.js';
import { makePdf } from './make-pdf.ts';

global.crypto ??= (crypto as any).webcrypto;

Expand Down
14 changes: 7 additions & 7 deletions src/api/make-pdf.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createFontLoader, createFontStore } from '../font-loader.js';
import { createImageLoader, createImageStore } from '../image-loader.js';
import { layoutPages } from '../layout/layout.js';
import { readDocumentDefinition } from '../read-document.js';
import { renderDocument } from '../render/render-document.js';
import { readAs } from '../types.js';
import { DocumentDefinition } from './content.js';
import { createFontLoader, createFontStore } from '../font-loader.ts';
import { createImageLoader, createImageStore } from '../image-loader.ts';
import { layoutPages } from '../layout/layout.ts';
import { readDocumentDefinition } from '../read-document.ts';
import { renderDocument } from '../render/render-document.ts';
import { readAs } from '../types.ts';
import { DocumentDefinition } from './content.ts';

/**
* Generates a PDF from the given document definition.
Expand Down
2 changes: 1 addition & 1 deletion src/binary-data.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';

import { parseBinaryData } from './binary-data.js';
import { parseBinaryData } from './binary-data.ts';

describe('binary-data', () => {
const data = Uint8Array.of(1, 183, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/binary-data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { decodeFromBase64DataUri } from 'pdf-lib';

import { typeError } from './types.js';
import { typeError } from './types.ts';

export function parseBinaryData(input: unknown): Uint8Array {
if (input instanceof Uint8Array) return input;
Expand Down
2 changes: 1 addition & 1 deletion src/box.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';

import { parseEdges, parseLength, subtractEdges } from './box.js';
import { parseEdges, parseLength, subtractEdges } from './box.ts';

describe('box', () => {
describe('subtractEdges', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/box.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject, Obj, typeError } from './types.js';
import { isObject, Obj, typeError } from './types.ts';

export type Pos = { x: number; y: number };
export type Size = { width: number; height: number };
Expand Down
6 changes: 3 additions & 3 deletions src/font-loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fontkit from '@pdf-lib/fontkit';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { createFontLoader, createFontStore, FontLoader } from './font-loader.js';
import { Font, FontDef, FontSelector } from './fonts.js';
import { fakeFont, mkData } from './test/test-utils.js';
import { createFontLoader, createFontStore, FontLoader } from './font-loader.ts';
import { Font, FontDef, FontSelector } from './fonts.ts';
import { fakeFont, mkData } from './test/test-utils.ts';

describe('font-loader', () => {
let normalFont: FontDef;
Expand Down
6 changes: 3 additions & 3 deletions src/font-loader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fontkit from '@pdf-lib/fontkit';
import { toUint8Array } from 'pdf-lib';

import { FontWeight } from './api/content.js';
import { Font, FontDef, FontSelector, weightToNumber } from './fonts.js';
import { pickDefined } from './types.js';
import { FontWeight } from './api/content.ts';
import { Font, FontDef, FontSelector, weightToNumber } from './fonts.ts';
import { pickDefined } from './types.ts';

export type LoadedFont = {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion src/fonts.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';

import { readFonts, weightToNumber } from './fonts.js';
import { readFonts, weightToNumber } from './fonts.ts';

describe('fonts', () => {
describe('readFonts', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/fonts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fontkit from '@pdf-lib/fontkit';
import { CustomFontSubsetEmbedder, PDFDocument, PDFFont, PDFRef } from 'pdf-lib';

import { FontStyle, FontWeight } from './api/content.js';
import { parseBinaryData } from './binary-data.js';
import { printValue } from './print-value.js';
import { optional, readAs, readBoolean, readObject, required, types } from './types.js';
import { FontStyle, FontWeight } from './api/content.ts';
import { parseBinaryData } from './binary-data.ts';
import { printValue } from './print-value.ts';
import { optional, readAs, readBoolean, readObject, required, types } from './types.ts';

/**
* The resolved definition of a font.
Expand Down
8 changes: 4 additions & 4 deletions src/frame.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Font } from './fonts.js';
import { Image } from './images.js';
import { Color } from './read-color.js';
import { PathCommand } from './svg-paths.js';
import { Font } from './fonts.ts';
import { Image } from './images.ts';
import { Color } from './read-color.ts';
import { PathCommand } from './svg-paths.ts';

/**
* Frames are created during the layout process. They have a position
Expand Down
8 changes: 4 additions & 4 deletions src/guides.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { rgb } from 'pdf-lib';

import { ZERO_EDGES } from './box.js';
import { ZERO_EDGES } from './box.ts';
import {
CircleObject,
Frame,
GraphicsObject,
LineObject,
RectObject,
TextRowObject,
} from './frame.js';
import { Block } from './read-block.js';
import { compact } from './utils.js';
} from './frame.ts';
import { Block } from './read-block.ts';
import { compact } from './utils.ts';

export function createFrameGuides(
frame: Frame,
Expand Down
4 changes: 2 additions & 2 deletions src/image-loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { join } from 'node:path';

import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

import { createImageLoader, createImageStore, ImageLoader } from './image-loader.js';
import { ImageSelector } from './images.js';
import { createImageLoader, createImageStore, ImageLoader } from './image-loader.ts';
import { ImageSelector } from './images.ts';

global.crypto ??= (crypto as any).webcrypto;

Expand Down
6 changes: 3 additions & 3 deletions src/image-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { readFile } from 'node:fs/promises';

import { toUint8Array } from 'pdf-lib';

import { Image, ImageDef, ImageFormat, ImageSelector } from './images.js';
import { isJpeg, readJpegInfo } from './images/jpeg.js';
import { isPng, readPngInfo } from './images/png.js';
import { Image, ImageDef, ImageFormat, ImageSelector } from './images.ts';
import { isJpeg, readJpegInfo } from './images/jpeg.ts';
import { isPng, readPngInfo } from './images/png.ts';

export type LoadedImage = {
data: Uint8Array;
Expand Down
4 changes: 2 additions & 2 deletions src/images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { join } from 'node:path';

import { describe, expect, it } from 'vitest';

import { Image, readImages, registerImage } from './images.js';
import { fakePDFDocument, mkData } from './test/test-utils.js';
import { Image, readImages, registerImage } from './images.ts';
import { fakePDFDocument, mkData } from './test/test-utils.ts';

global.crypto ??= (crypto as any).webcrypto;

Expand Down
4 changes: 2 additions & 2 deletions src/images.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { JpegEmbedder, PDFDocument, PDFRef, PngEmbedder } from 'pdf-lib';

import { parseBinaryData } from './binary-data.js';
import { optional, readAs, readObject, required, types } from './types.js';
import { parseBinaryData } from './binary-data.ts';
import { optional, readAs, readObject, required, types } from './types.ts';

const imageFormats = ['jpeg', 'png'];
export type ImageFormat = (typeof imageFormats)[number];
Expand Down
2 changes: 1 addition & 1 deletion src/images/jpeg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join } from 'node:path';

import { describe, expect, it } from 'vitest';

import { isJpeg, readJpegInfo } from './jpeg.js';
import { isJpeg, readJpegInfo } from './jpeg.ts';

describe('jpeg', () => {
describe('isJpeg', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/images/png.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join } from 'node:path';

import { describe, expect, it } from 'vitest';

import { isPng, readPngInfo } from './png.js';
import { isPng, readPngInfo } from './png.ts';

describe('png', () => {
describe('isPng', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './api/content.js';
export { makePdf } from './api/make-pdf.js';
export * from './api/content.ts';
export { makePdf } from './api/make-pdf.ts';
12 changes: 6 additions & 6 deletions src/layout/layout-columns.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { beforeEach, describe, expect, it } from 'vitest';

import { Box } from '../box.js';
import { FontStore } from '../font-loader.js';
import { MakerCtx } from '../maker-ctx.js';
import { Block } from '../read-block.js';
import { fakeFont, span } from '../test/test-utils.js';
import { layoutColumnsContent } from './layout-columns.js';
import { Box } from '../box.ts';
import { FontStore } from '../font-loader.ts';
import { MakerCtx } from '../maker-ctx.ts';
import { Block } from '../read-block.ts';
import { fakeFont, span } from '../test/test-utils.ts';
import { layoutColumnsContent } from './layout-columns.ts';

const { objectContaining } = expect;

Expand Down
10 changes: 5 additions & 5 deletions src/layout/layout-columns.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Box } from '../box.js';
import { Frame } from '../frame.js';
import { MakerCtx } from '../maker-ctx.js';
import { Block, ColumnsBlock } from '../read-block.js';
import { layoutBlock, LayoutContent } from './layout.js';
import { Box } from '../box.ts';
import { Frame } from '../frame.ts';
import { MakerCtx } from '../maker-ctx.ts';
import { Block, ColumnsBlock } from '../read-block.ts';
import { layoutBlock, LayoutContent } from './layout.ts';

export async function layoutColumnsContent(
block: ColumnsBlock,
Expand Down
14 changes: 7 additions & 7 deletions src/layout/layout-image.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { Box } from '../box.js';
import { ImageStore } from '../image-loader.js';
import { ImageSelector } from '../images.js';
import { MakerCtx } from '../maker-ctx.js';
import { ImageBlock } from '../read-block.js';
import { fakeImage } from '../test/test-utils.js';
import { layoutImageContent } from './layout-image.js';
import { Box } from '../box.ts';
import { ImageStore } from '../image-loader.ts';
import { ImageSelector } from '../images.ts';
import { MakerCtx } from '../maker-ctx.ts';
import { ImageBlock } from '../read-block.ts';
import { fakeImage } from '../test/test-utils.ts';
import { layoutImageContent } from './layout-image.ts';

const { objectContaining } = expect;

Expand Down
12 changes: 6 additions & 6 deletions src/layout/layout-image.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Box, Pos, Size } from '../box.js';
import { ImageObject, RenderObject } from '../frame.js';
import { Image } from '../images.js';
import { MakerCtx } from '../maker-ctx.js';
import { ImageBlock } from '../read-block.js';
import { LayoutContent } from './layout.js';
import { Box, Pos, Size } from '../box.ts';
import { ImageObject, RenderObject } from '../frame.ts';
import { Image } from '../images.ts';
import { MakerCtx } from '../maker-ctx.ts';
import { ImageBlock } from '../read-block.ts';
import { LayoutContent } from './layout.ts';

export async function layoutImageContent(
block: ImageBlock,
Expand Down
14 changes: 7 additions & 7 deletions src/layout/layout-rows.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { beforeEach, describe, expect, it } from 'vitest';

import { Box } from '../box.js';
import { FontStore } from '../font-loader.js';
import { Frame } from '../frame.js';
import { MakerCtx } from '../maker-ctx.js';
import { Block } from '../read-block.js';
import { extractTextRows, fakeFont, range, span } from '../test/test-utils.js';
import { layoutRowsContent } from './layout-rows.js';
import { Box } from '../box.ts';
import { FontStore } from '../font-loader.ts';
import { Frame } from '../frame.ts';
import { MakerCtx } from '../maker-ctx.ts';
import { Block } from '../read-block.ts';
import { extractTextRows, fakeFont, range, span } from '../test/test-utils.ts';
import { layoutRowsContent } from './layout-rows.ts';

describe('layout-rows', () => {
let ctx: MakerCtx, box: Box;
Expand Down
12 changes: 6 additions & 6 deletions src/layout/layout-rows.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Box, ZERO_EDGES } from '../box.js';
import { Frame } from '../frame.js';
import { MakerCtx } from '../maker-ctx.js';
import { Block, RowsBlock } from '../read-block.js';
import { compact, omit } from '../utils.js';
import { isBreakPossible, layoutBlock, LayoutContent } from './layout.js';
import { Box, ZERO_EDGES } from '../box.ts';
import { Frame } from '../frame.ts';
import { MakerCtx } from '../maker-ctx.ts';
import { Block, RowsBlock } from '../read-block.ts';
import { compact, omit } from '../utils.ts';
import { isBreakPossible, layoutBlock, LayoutContent } from './layout.ts';

export async function layoutRowsContent(
block: RowsBlock,
Expand Down
12 changes: 6 additions & 6 deletions src/layout/layout-text.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { rgb } from 'pdf-lib';
import { beforeEach, describe, expect, it } from 'vitest';

import { Box } from '../box.js';
import { FontStore } from '../font-loader.js';
import { Font, FontSelector } from '../fonts.js';
import { MakerCtx } from '../maker-ctx.js';
import { extractTextRows, fakeFont, range, span } from '../test/test-utils.js';
import { layoutTextContent } from './layout-text.js';
import { Box } from '../box.ts';
import { FontStore } from '../font-loader.ts';
import { Font, FontSelector } from '../fonts.ts';
import { MakerCtx } from '../maker-ctx.ts';
import { extractTextRows, fakeFont, range, span } from '../test/test-utils.ts';
import { layoutTextContent } from './layout-text.ts';

const { objectContaining } = expect;

Expand Down
18 changes: 9 additions & 9 deletions src/layout/layout-text.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Box, Size } from '../box.js';
import { Font } from '../fonts.js';
import { LinkObject, RenderObject, TextRowObject, TextSegmentObject } from '../frame.js';
import { createRowGuides } from '../guides.js';
import { MakerCtx } from '../maker-ctx.js';
import { TextBlock } from '../read-block.js';
import { Box, Size } from '../box.ts';
import { Font } from '../fonts.ts';
import { LinkObject, RenderObject, TextRowObject, TextSegmentObject } from '../frame.ts';
import { createRowGuides } from '../guides.ts';
import { MakerCtx } from '../maker-ctx.ts';
import { TextBlock } from '../read-block.ts';
import {
breakLine,
convertToTextSpan,
extractTextSegments,
flattenTextSegments,
TextSegment,
} from '../text.js';
import { omit } from '../utils.js';
import { LayoutContent } from './layout.js';
} from '../text.ts';
import { omit } from '../utils.ts';
import { LayoutContent } from './layout.ts';

export async function layoutTextContent(
block: TextBlock,
Expand Down
Loading

0 comments on commit ec8a874

Please sign in to comment.