Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use .ts extension in relative source imports #591

Merged
merged 8 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Deno

on:
pull_request:
branches: [main]
push:
branches: [main]
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
deno-version: [v1.x, v2.x]

steps:
- uses: actions/checkout@v4
with: { submodules: true }
- uses: denoland/setup-deno@v2
with:
deno-version: ${{ matrix.deno-version }}
- run: deno src/public-api.ts
2 changes: 1 addition & 1 deletion config/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ switch (process.env.npm_lifecycle_event) {
'^yaml$': '<rootDir>/dist/index.js',
'^yaml/cli$': '<rootDir>/dist/cli.mjs',
'^yaml/util$': '<rootDir>/dist/util.js',
'^../src/test-events$': '<rootDir>/dist/test-events.js'
'^../src/test-events.ts$': '<rootDir>/dist/test-events.js'
}
transform['[/\\\\]dist[/\\\\].*\\.mjs$'] = babel
break
Expand Down
62 changes: 60 additions & 2 deletions config/rollup.node-config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,53 @@
import typescript from '@rollup/plugin-typescript'
import * as ts from 'typescript'

/**
* Drop .ts extension from import & export paths in .d.ts files
* to support older TS versions.
*
* @param {ts.TransformationContext} context
*/
function fixDeclarationImportPaths(context) {
/** @param {ts.SourceFile} source */
return function fixPaths(source) {
/** @param {ts.Node} node */
function visitor(node) {
if (ts.isStringLiteral(node) && /^\.+\/.*\.ts$/.test(node.text)) {
return ts.factory.createStringLiteral(node.text.slice(0, -3), true)
}
return ts.visitEachChild(node, visitor, context)
}
return ts.visitNode(source, visitor)
}
}

/**
* Strip out TS relative import path rewrite helper from dynamic import() calls
*
* Required due to
* https://github.com/rollup/plugins/issues/1820
*
* @param {ts.TransformationContext} context
*/
function fixDynamicImportRewrite(context) {
/** @param {ts.SourceFile} source */
return function fixDynamicImport(source) {
/** @param {ts.Node} node */
function visitor(node) {
if (
ts.isCallExpression(node) &&
ts.isIdentifier(node.expression) &&
String(node.expression.escapedText) ===
'___rewriteRelativeImportExtension' &&
node.arguments.length === 1
) {
return node.arguments[0]
}
return ts.visitEachChild(node, visitor, context)
}
return ts.visitNode(source, visitor)
}
}

export default [
{
Expand All @@ -13,13 +62,22 @@ export default [
esModule: false,
preserveModules: true
},
plugins: [typescript()],
plugins: [
typescript({
transformers: { afterDeclarations: [fixDeclarationImportPaths] }
})
],
treeshake: { moduleSideEffects: false, propertyReadSideEffects: false }
},
{
input: 'src/cli.ts',
output: { file: 'dist/cli.mjs' },
external: () => true,
plugins: [typescript()]
plugins: [
typescript({
declaration: false,
transformers: { after: [fixDynamicImportRewrite] }
})
]
}
]
16 changes: 16 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,25 @@ export default [
'no-control-regex': 'off',
'no-fallthrough': ['error', { commentPattern: 'fallthrough' }],
'no-implicit-globals': 'error',
'no-restricted-imports': [
'error',
{
patterns: [
{
regex: '^\\..*(?<!\\.ts)$',
message: 'Relative imports must use .ts extension.'
}
]
}
],
'no-template-curly-in-string': 'warn',
'no-var': 'error',
'prefer-const': ['warn', { destructuring: 'all' }],
'@typescript-eslint/consistent-type-exports': 'error',
'@typescript-eslint/consistent-type-imports': [
'error',
{ fixStyle: 'separate-type-imports' }
],
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-namespace': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@eslint/js": "^9.9.1",
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-replace": "^5.0.2",
"@rollup/plugin-typescript": "^11.0.0",
"@rollup/plugin-typescript": "^12.1.1",
"@types/jest": "^29.2.4",
"@types/node": "^20.11.20",
"babel-jest": "^29.0.1",
Expand All @@ -86,8 +86,8 @@
"jest-ts-webcompat-resolver": "^1.0.0",
"prettier": "^3.0.2",
"rollup": "^4.12.0",
"tslib": "^2.1.0",
"typescript": "^5.0.3",
"tslib": "^2.8.1",
"typescript": "^5.7.2",
"typescript-eslint": "^8.4.0"
},
"engines": {
Expand Down
18 changes: 10 additions & 8 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { resolve } from 'node:path'
import { parseArgs } from 'node:util'

import { type Token, prettyToken } from './parse/cst.js'
import { Lexer } from './parse/lexer.js'
import { Parser } from './parse/parser.js'
import { Composer } from './compose/composer.js'
import { LineCounter } from './parse/line-counter.js'
import { type Document } from './doc/Document.js'
import { prettifyError } from './errors.js'
import { visit, type visitor } from './visit.js'
import type { Token } from './parse/cst.ts'
import { prettyToken } from './parse/cst.ts'
import { Lexer } from './parse/lexer.ts'
import { Parser } from './parse/parser.ts'
import { Composer } from './compose/composer.ts'
import { LineCounter } from './parse/line-counter.ts'
import type { Document } from './doc/Document.ts'
import { prettifyError } from './errors.ts'
import type { visitor } from './visit.ts'
import { visit } from './visit.ts'

export const help = `\
yaml: A command-line YAML processor and inspector
Expand Down
24 changes: 12 additions & 12 deletions src/compose/compose-collection.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { isNode } from '../nodes/identity.js'
import type { ParsedNode } from '../nodes/Node.js'
import { Scalar } from '../nodes/Scalar.js'
import { YAMLMap } from '../nodes/YAMLMap.js'
import { YAMLSeq } from '../nodes/YAMLSeq.js'
import { isNode } from '../nodes/identity.ts'
import type { ParsedNode } from '../nodes/Node.ts'
import { Scalar } from '../nodes/Scalar.ts'
import { YAMLMap } from '../nodes/YAMLMap.ts'
import { YAMLSeq } from '../nodes/YAMLSeq.ts'
import type {
BlockMap,
BlockSequence,
FlowCollection,
SourceToken
} from '../parse/cst.js'
import { CollectionTag } from '../schema/types.js'
import type { ComposeContext, ComposeNode } from './compose-node.js'
import type { ComposeErrorHandler } from './composer.js'
import { resolveBlockMap } from './resolve-block-map.js'
import { resolveBlockSeq } from './resolve-block-seq.js'
import { resolveFlowCollection } from './resolve-flow-collection.js'
} from '../parse/cst.ts'
import type { CollectionTag } from '../schema/types.ts'
import type { ComposeContext, ComposeNode } from './compose-node.ts'
import type { ComposeErrorHandler } from './composer.ts'
import { resolveBlockMap } from './resolve-block-map.ts'
import { resolveBlockSeq } from './resolve-block-seq.ts'
import { resolveFlowCollection } from './resolve-flow-collection.ts'

function resolveCollection(
CN: ComposeNode,
Expand Down
20 changes: 10 additions & 10 deletions src/compose/compose-doc.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import type { Directives } from '../doc/directives.js'
import { Document } from '../doc/Document.js'
import type { ParsedNode } from '../nodes/Node.js'
import type { Directives } from '../doc/directives.ts'
import { Document } from '../doc/Document.ts'
import type { ParsedNode } from '../nodes/Node.ts'
import type {
DocumentOptions,
ParseOptions,
SchemaOptions
} from '../options.js'
import type * as CST from '../parse/cst.js'
} from '../options.ts'
import type * as CST from '../parse/cst.ts'
import {
ComposeContext,
type ComposeContext,
composeEmptyNode,
composeNode
} from './compose-node.js'
import type { ComposeErrorHandler } from './composer.js'
import { resolveEnd } from './resolve-end.js'
import { resolveProps } from './resolve-props.js'
} from './compose-node.ts'
import type { ComposeErrorHandler } from './composer.ts'
import { resolveEnd } from './resolve-end.ts'
import { resolveProps } from './resolve-props.ts'

export function composeDoc<
Contents extends ParsedNode = ParsedNode,
Expand Down
24 changes: 12 additions & 12 deletions src/compose/compose-node.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { Directives } from '../doc/directives.js'
import { Alias } from '../nodes/Alias.js'
import { isScalar } from '../nodes/identity.js'
import type { ParsedNode } from '../nodes/Node.js'
import type { ParseOptions } from '../options.js'
import type { FlowScalar, SourceToken, Token } from '../parse/cst.js'
import type { Schema } from '../schema/Schema.js'
import { composeCollection } from './compose-collection.js'
import { composeScalar } from './compose-scalar.js'
import type { ComposeErrorHandler } from './composer.js'
import { resolveEnd } from './resolve-end.js'
import { emptyScalarPosition } from './util-empty-scalar-position.js'
import type { Directives } from '../doc/directives.ts'
import { Alias } from '../nodes/Alias.ts'
import { isScalar } from '../nodes/identity.ts'
import type { ParsedNode } from '../nodes/Node.ts'
import type { ParseOptions } from '../options.ts'
import type { FlowScalar, SourceToken, Token } from '../parse/cst.ts'
import type { Schema } from '../schema/Schema.ts'
import { composeCollection } from './compose-collection.ts'
import { composeScalar } from './compose-scalar.ts'
import type { ComposeErrorHandler } from './composer.ts'
import { resolveEnd } from './resolve-end.ts'
import { emptyScalarPosition } from './util-empty-scalar-position.ts'

export interface ComposeContext {
atKey: boolean
Expand Down
18 changes: 9 additions & 9 deletions src/compose/compose-scalar.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { isScalar, SCALAR } from '../nodes/identity.js'
import { Scalar } from '../nodes/Scalar.js'
import type { BlockScalar, FlowScalar, SourceToken } from '../parse/cst.js'
import type { Schema } from '../schema/Schema.js'
import type { ScalarTag } from '../schema/types.js'
import type { ComposeContext } from './compose-node.js'
import type { ComposeErrorHandler } from './composer.js'
import { resolveBlockScalar } from './resolve-block-scalar.js'
import { resolveFlowScalar } from './resolve-flow-scalar.js'
import { isScalar, SCALAR } from '../nodes/identity.ts'
import { Scalar } from '../nodes/Scalar.ts'
import type { BlockScalar, FlowScalar, SourceToken } from '../parse/cst.ts'
import type { Schema } from '../schema/Schema.ts'
import type { ScalarTag } from '../schema/types.ts'
import type { ComposeContext } from './compose-node.ts'
import type { ComposeErrorHandler } from './composer.ts'
import { resolveBlockScalar } from './resolve-block-scalar.ts'
import { resolveFlowScalar } from './resolve-flow-scalar.ts'

export function composeScalar(
ctx: ComposeContext,
Expand Down
19 changes: 10 additions & 9 deletions src/compose/composer.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Directives } from '../doc/directives.js'
import { Document } from '../doc/Document.js'
import { ErrorCode, YAMLParseError, YAMLWarning } from '../errors.js'
import { isCollection, isPair } from '../nodes/identity.js'
import type { ParsedNode, Range } from '../nodes/Node.js'
import { Directives } from '../doc/directives.ts'
import { Document } from '../doc/Document.ts'
import type { ErrorCode } from '../errors.ts'
import { YAMLParseError, YAMLWarning } from '../errors.ts'
import { isCollection, isPair } from '../nodes/identity.ts'
import type { ParsedNode, Range } from '../nodes/Node.ts'
import type {
DocumentOptions,
ParseOptions,
SchemaOptions
} from '../options.js'
import type { Token } from '../parse/cst.js'
import { composeDoc } from './compose-doc.js'
import { resolveEnd } from './resolve-end.js'
} from '../options.ts'
import type { Token } from '../parse/cst.ts'
import { composeDoc } from './compose-doc.ts'
import { resolveEnd } from './resolve-end.ts'

type ErrorSource =
| number
Expand Down
22 changes: 11 additions & 11 deletions src/compose/resolve-block-map.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { ParsedNode } from '../nodes/Node.js'
import { Pair } from '../nodes/Pair.js'
import { YAMLMap } from '../nodes/YAMLMap.js'
import type { BlockMap } from '../parse/cst.js'
import { CollectionTag } from '../schema/types.js'
import type { ComposeContext, ComposeNode } from './compose-node.js'
import type { ComposeErrorHandler } from './composer.js'
import { resolveProps } from './resolve-props.js'
import { containsNewline } from './util-contains-newline.js'
import { flowIndentCheck } from './util-flow-indent-check.js'
import { mapIncludes } from './util-map-includes.js'
import type { ParsedNode } from '../nodes/Node.ts'
import { Pair } from '../nodes/Pair.ts'
import { YAMLMap } from '../nodes/YAMLMap.ts'
import type { BlockMap } from '../parse/cst.ts'
import type { CollectionTag } from '../schema/types.ts'
import type { ComposeContext, ComposeNode } from './compose-node.ts'
import type { ComposeErrorHandler } from './composer.ts'
import { resolveProps } from './resolve-props.ts'
import { containsNewline } from './util-contains-newline.ts'
import { flowIndentCheck } from './util-flow-indent-check.ts'
import { mapIncludes } from './util-map-includes.ts'

const startColMsg = 'All mapping items must start at the same column'

Expand Down
10 changes: 5 additions & 5 deletions src/compose/resolve-block-scalar.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Range } from '../nodes/Node.js'
import { Scalar } from '../nodes/Scalar.js'
import type { BlockScalar } from '../parse/cst.js'
import type { ComposeContext } from './compose-node.js'
import type { ComposeErrorHandler } from './composer.js'
import type { Range } from '../nodes/Node.ts'
import { Scalar } from '../nodes/Scalar.ts'
import type { BlockScalar } from '../parse/cst.ts'
import type { ComposeContext } from './compose-node.ts'
import type { ComposeErrorHandler } from './composer.ts'

export function resolveBlockScalar(
ctx: ComposeContext,
Expand Down
Loading