Skip to content

Commit

Permalink
style: Tighten ESLint config
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Apr 3, 2022
1 parent 73e9ff2 commit 5658858
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 23 deletions.
16 changes: 13 additions & 3 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
root: true
parser: '@typescript-eslint/parser'
parserOptions:
project: ./tsconfig.json
env:
node: true

Expand All @@ -9,12 +11,12 @@ plugins:
extends:
- eslint:recommended
- plugin:@typescript-eslint/recommended
- plugin:@typescript-eslint/recommended-requiring-type-checking
- prettier

rules:
array-callback-return: error
camelcase: error
consistent-return: 0
eqeqeq: [error, always, 'null': ignore]
no-constant-condition: [error, checkLoops: false]
no-control-regex: 0
Expand All @@ -23,13 +25,21 @@ rules:
no-template-curly-in-string: warn
no-var: error
prefer-const: [warn, destructuring: all]
'@typescript-eslint/ban-ts-comment': off
'@typescript-eslint/explicit-module-boundary-types': off
'@typescript-eslint/no-explicit-any': off
'@typescript-eslint/no-namespace': off
'@typescript-eslint/no-unsafe-argument': off
'@typescript-eslint/no-unsafe-assignment': off
'@typescript-eslint/no-unsafe-member-access': off
'@typescript-eslint/no-unused-vars': [warn, argsIgnorePattern: '^_']
'@typescript-eslint/prefer-includes': warn
'@typescript-eslint/restrict-template-expressions': off

overrides:
- files: [config/**]
parser: espree
parserOptions:
ecmaVersion: latest

- files: [tests/**]
env:
jest: true
Expand Down
2 changes: 1 addition & 1 deletion src/compose/resolve-flow-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,5 @@ export function resolveFlowCollection(
coll.range = [fc.offset, cePos, cePos]
}

return coll as YAMLMap.Parsed | YAMLSeq.Parsed
return coll
}
6 changes: 3 additions & 3 deletions src/doc/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export class Document<T = unknown> {
* boolean to add/remove the item from the set.
*/
setIn(path: Iterable<unknown>, value: unknown) {
if (isEmptyPath(path)) this.contents = value as unknown as T
if (isEmptyPath(path)) this.contents = value as T
else if (this.contents == null) {
this.contents = collectionFromPath(
this.schema,
Expand Down Expand Up @@ -413,7 +413,7 @@ export class Document<T = unknown> {
maxAliasCount,
onAnchor,
reviver
}: ToJSOptions & { json?: boolean; jsonArg?: string | null } = {}) {
}: ToJSOptions & { json?: boolean; jsonArg?: string | null } = {}): any {
const ctx: ToJSContext = {
anchors: new Map(),
doc: this,
Expand All @@ -437,7 +437,7 @@ export class Document<T = unknown> {
* @param jsonArg Used by `JSON.stringify` to indicate the array index or
* property name.
*/
toJSON(jsonArg?: string | null, onAnchor?: ToJSOptions['onAnchor']) {
toJSON(jsonArg?: string | null, onAnchor?: ToJSOptions['onAnchor']): any {
return this.toJS({ json: true, jsonArg, mapAsMap: false, onAnchor })
}

Expand Down
4 changes: 2 additions & 2 deletions src/doc/anchors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function createNodeAnchors(doc: Document, prefix: string) {
let prevAnchors: Set<string> | null = null

return {
onAnchor(source: unknown) {
onAnchor: (source: unknown) => {
aliasObjects.push(source)
if (!prevAnchors) prevAnchors = anchorNames(doc)
const anchor = findNewAnchor(prefix, prevAnchors)
Expand All @@ -57,7 +57,7 @@ export function createNodeAnchors(doc: Document, prefix: string) {
* of its child nodes are. This is why anchors are set only after all of
* the nodes have been created.
*/
setAnchors() {
setAnchors: () => {
for (const source of aliasObjects) {
const ref = sourceObjects.get(source)
if (
Expand Down
8 changes: 5 additions & 3 deletions src/doc/createNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const defaultTagPrefix = 'tag:yaml.org,2002:'
export interface CreateNodeContext {
aliasDuplicateObjects: boolean
keepUndefined: boolean
onAnchor(source: unknown): string
onAnchor: (source: unknown) => string
onTagObj?: (tagObj: ScalarTag | CollectionTag) => void
sourceObjects: Map<unknown, { anchor: string | null; node: Node | null }>
replacer?: Replacer
Expand Down Expand Up @@ -76,8 +76,10 @@ export function createNode(

let tagObj = findTagObject(value, tagName, schema.tags)
if (!tagObj) {
if (value && typeof (value as any).toJSON === 'function')
if (value && typeof (value as any).toJSON === 'function') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
value = (value as any).toJSON()
}
if (!value || typeof value !== 'object') {
const node = new Scalar(value)
if (ref) ref.node = node
Expand All @@ -91,7 +93,7 @@ export function createNode(
: schema[MAP]
}
if (onTagObj) {
onTagObj(tagObj as ScalarTag | CollectionTag)
onTagObj(tagObj)
delete ctx.onTagObj
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/Scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class Scalar<T = unknown> extends NodeBase {
this.value = value
}

toJSON(arg?: any, ctx?: ToJSContext) {
toJSON(arg?: any, ctx?: ToJSContext): any {
return ctx && ctx.keep ? this.value : toJS(this.value, arg, ctx)
}

Expand Down
6 changes: 5 additions & 1 deletion src/nodes/YAMLMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ export class YAMLMap<K = unknown, V = unknown> extends Collection {
* @param {Class} Type - If set, forces the returned collection type
* @returns Instance of Type, Map, or Object
*/
toJSON(_?: unknown, ctx?: ToJSContext, Type?: any) {
toJSON<T = unknown>(
_?: unknown,
ctx?: ToJSContext,
Type?: { new (): T }
): any {
const map = Type ? new Type() : ctx && ctx.mapAsMap ? new Map() : {}
if (ctx && ctx.onCreate) ctx.onCreate(map)
for (const item of this.items) addPairToJSMap(ctx, map, item)
Expand Down
2 changes: 2 additions & 0 deletions src/nodes/toJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ export interface ToJSContext {
* stringification.
*/
export function toJS(value: any, arg: string | null, ctx?: ToJSContext): any {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
if (Array.isArray(value)) return value.map((v, i) => toJS(v, String(i), ctx))
if (value && typeof value.toJSON === 'function') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
if (!ctx || !hasAnchor(value)) return value.toJSON(arg, ctx)
const data: AnchorData = { aliasCount: 0, count: 1, res: undefined }
ctx.anchors.set(value, data)
Expand Down
2 changes: 1 addition & 1 deletion src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export function parse(
| Reviver
| (ParseOptions & DocumentOptions & SchemaOptions & ToJSOptions),
options?: ParseOptions & DocumentOptions & SchemaOptions & ToJSOptions
) {
): any {
let _reviver: Reviver | undefined = undefined
if (typeof reviver === 'function') {
_reviver = reviver
Expand Down
2 changes: 1 addition & 1 deletion src/schema/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class Schema {
}

clone(): Schema {
const copy = Object.create(
const copy: Schema = Object.create(
Schema.prototype,
Object.getOwnPropertyDescriptors(this)
)
Expand Down
2 changes: 1 addition & 1 deletion src/schema/yaml-1.1/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class YAMLSet<T = unknown> extends YAMLMap<T, Scalar<null> | null> {
}
}

toJSON(_?: unknown, ctx?: ToJSContext) {
toJSON(_?: unknown, ctx?: ToJSContext): any {
return super.toJSON(_, ctx, Set)
}

Expand Down
3 changes: 1 addition & 2 deletions src/stringify/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ function getTagObject(tags: Array<ScalarTag | CollectionTag>, item: Node) {
}

if (!tagObj) {
// @ts-ignore
const name = obj && obj.constructor ? obj.constructor.name : typeof obj
const name = (obj as any)?.constructor?.name ?? typeof obj
throw new Error(`Tag not resolved for ${name} value`)
}
return tagObj
Expand Down
4 changes: 2 additions & 2 deletions src/stringify/stringifyString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,15 @@ function plainString(
// - '\n ', ': ' or ' \n' anywhere
// - '#' not preceded by a non-space char
// - end with ' ' or ':'
return implicitKey || inFlow || value.indexOf('\n') === -1
return implicitKey || inFlow || !value.includes('\n')
? quotedString(value, ctx)
: blockString(item, ctx, onComment, onChompKeep)
}
if (
!implicitKey &&
!inFlow &&
type !== Scalar.PLAIN &&
value.indexOf('\n') !== -1
value.includes('\n')
) {
// Where allowed & type not set explicitly, prefer block style for multiline strings
return blockString(item, ctx, onComment, onChompKeep)
Expand Down
3 changes: 1 addition & 2 deletions src/test-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
ParsedNode
} from './nodes/Node.js'
import type { Pair } from './nodes/Pair.js'
import type { Scalar } from './nodes/Scalar.js'
import { parseAllDocuments } from './public-api.js'
import { visit } from './visit.js'

Expand Down Expand Up @@ -126,7 +125,7 @@ function addEvents(
} else {
const scalar = scalarChar[String(node.type)]
if (!scalar) throw new Error(`Unexpected node type ${node.type}`)
const value = (node as Scalar.Parsed).source
const value = node.source
.replace(/\\/g, '\\\\')
.replace(/\0/g, '\\0')
.replace(/\x07/g, '\\a')
Expand Down

0 comments on commit 5658858

Please sign in to comment.