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

Add defaultYAMLVersion option #114

Merged
merged 1 commit into from
Jul 28, 2022
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.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/lib
/node_modules
/tests/fixtures/**/*.json
!/tests/fixtures/**/*options.json
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,39 @@ module.exports = {
"overrides": [
{
"files": ["*.yaml", "*.yml"],
"parser": "yaml-eslint-parser"
"parser": "yaml-eslint-parser",
}
]
}
```

### Advanced Configuration

The following additional configuration options are available by specifying them in [parserOptions](https://eslint.org/docs/latest/user-guide/configuring/language-options#specifying-parser-options) in your ESLint configuration file.

Example **.eslintrc.js**:

```json5
module.exports = {
"overrides": [
{
"files": ["*.yaml", "*.yml"],
"parser": "yaml-eslint-parser",
// Additional configuration options
"parserOptions": {
"defaultYAMLVersion": "1.2"
}
}
]
}
```

#### `parserOptions.defaultYAMLVersion`

Set to `"1.2"` or `"1.1"`. Select the YAML version used by documents without a `%YAML` directive.
If not specified, If not specified, the [yaml](https://eemeli.org/yaml/)'s default `version` option (`"1.2"`) is used.
See <https://eemeli.org/yaml/#document-options> for details.

## Usage for Custom Rules / Plugins

- [AST.md](./docs/AST.md) is AST specification.
Expand Down
2 changes: 2 additions & 0 deletions docs/AST.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ interface YAMLDocument extends Node {
content: YAMLMapping | YAMLSequence | YAMLScalar | YAMLAlias | YAMLWithMeta | null
parent: YAMLProgram
anchors: { [key: string]: YAMLAnchor[] }
// YAML version
version: string
}
```

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@types/lodash": "^4.14.167",
"@types/mocha": "^9.0.0",
"@types/node": "^16.0.0",
"@types/semver": "^7.3.10",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"benchmark": "^2.1.4",
Expand All @@ -66,6 +67,7 @@
"mocha": "^10.0.0",
"nyc": "^15.1.0",
"prettier": "^2.0.5",
"semver": "^7.3.7",
"ts-node": "^10.0.0",
"typescript": "^4.0.0",
"vue-eslint-parser": "^9.0.0"
Expand Down
4 changes: 4 additions & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { YAMLVersion } from "./utils"

export type Range = [number, number]

export interface Locations {
Expand Down Expand Up @@ -65,6 +67,8 @@ export interface YAMLDocument extends BaseYAMLNode {
content: YAMLContent | YAMLWithMeta | null
parent: YAMLProgram
anchors: { [key: string]: YAMLAnchor[] }
// YAML version
version: YAMLVersion
}

interface BaseYAMLDirective extends BaseYAMLNode {
Expand Down
8 changes: 6 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { Comment, Locations, Position, Range, Token } from "./ast"
import lodash from "lodash"
import type { CST } from "yaml"
import type { CST, DocumentOptions } from "yaml"
import { ParseError } from "."
import { parserOptionsToYAMLOption } from "./options"

export class Context {
public readonly code: string

public readonly options: DocumentOptions

public readonly tokens: Token[] = []

public readonly comments: Comment[] = []
Expand All @@ -14,7 +17,8 @@ export class Context {

private readonly locsMap = new Map<number, Position>()

public constructor(origCode: string) {
public constructor(origCode: string, parserOptions: any) {
this.options = parserOptionsToYAMLOption(parserOptions)
const len = origCode.length
const lineStartIndices = [0]
for (let index = 0; index < len; ) {
Expand Down
13 changes: 6 additions & 7 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import type {
import type { Context } from "./context"
import { tagResolvers } from "./tags"
import type { YAMLVersion } from "./utils"
import { getYAMLVersion } from "./utils"
import type {
Alias,
CST,
Expand All @@ -48,6 +47,7 @@ import {
isSeq,
isMap,
} from "yaml"
import type { ParsedCSTDocs } from "./yaml-cst-parse"

type PairParsed = BasePair<ParsedNode, ParsedNode | null>
type Directives = Document.Parsed["directives"]
Expand Down Expand Up @@ -169,11 +169,8 @@ type CSTDoc = {
/**
* Convert yaml root to YAMLProgram
*/
export function convertRoot(
cstNodes: CST.Token[],
nodes: Document.Parsed[],
ctx: Context,
): YAMLProgram {
export function convertRoot(docs: ParsedCSTDocs, ctx: Context): YAMLProgram {
const { cstNodes, nodes } = docs
const ast: YAMLProgram = {
type: "Program",
body: [],
Expand Down Expand Up @@ -245,6 +242,7 @@ export function convertRoot(
content: null,
parent: ast,
anchors: {},
version: docs.streamInfo.directives.yaml.version,
...ctx.getConvertLocation(index, index),
})
}
Expand Down Expand Up @@ -278,6 +276,7 @@ function convertDocument(
content: null,
parent,
anchors: {},
version: node.directives.yaml.version,
...loc,
}

Expand Down Expand Up @@ -1180,7 +1179,7 @@ function convertPlain(
let ast: YAMLPlainScalar | YAMLWithMeta
if (loc.range[0] < loc.range[1]) {
const strValue = node.source || cst.source
const value = parseValueFromText(strValue, getYAMLVersion(doc))
const value = parseValueFromText(strValue, doc.version || "1.2")

ast = {
type: "YAMLScalar",
Expand Down
22 changes: 22 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { DocumentOptions } from "yaml"

/**
* ESLint parserOptions to `yaml`'s Composer options.
*/
export function parserOptionsToYAMLOption(options: any): DocumentOptions {
if (!options) {
return {}
}
const result: DocumentOptions = {}
const version = options.defaultYAMLVersion
if (typeof version === "string" || typeof version === "number") {
const sVer = String(version)
if (sVer === "1.2" || sVer === "1.1") {
result.version = sVer
} else {
// Treat unknown versions as next.
result.version = "next"
}
}
return result
}
6 changes: 3 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import { parseAllDocsToCST } from "./yaml-cst-parse"
*/
export function parseForESLint(
code: string,
_options?: any,
options?: any,
): {
ast: YAMLProgram
visitorKeys: SourceCode.VisitorKeys
services: { isYAML: boolean }
} {
const ctx = new Context(code)
const ctx = new Context(code, options)

const docs = parseAllDocsToCST(ctx)

const ast = convertRoot(docs.cstNodes, docs.nodes, ctx)
const ast = convertRoot(docs, ctx)

return {
ast,
Expand Down
4 changes: 2 additions & 2 deletions src/tags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
} from "./tags1.1"

export const tagResolvers = {
"1.3": tagResolversFor1_2,
next: tagResolversFor1_2,
"1.2": tagResolversFor1_2,
"1.1": tagResolversFor1_1,
}
export const tagNodeResolvers = {
"1.3": tagNodeResolversFor1_2,
next: tagNodeResolversFor1_2,
"1.2": tagNodeResolversFor1_2,
"1.1": tagNodeResolversFor1_1,
}
33 changes: 5 additions & 28 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parseDocument } from "yaml"
import type { Directives } from "yaml/dist/doc/directives"
import type {
YAMLProgram,
YAMLContent,
Expand All @@ -14,17 +15,17 @@ import type {
} from "./ast"
import { tagNodeResolvers, tagResolvers } from "./tags"

export type YAMLVersion = "1.3" | "1.2" | "1.1"
export type YAMLVersion = Directives["yaml"]["version"]

type YAMLContentValue =
export type YAMLContentValue =
| string
| number
| boolean
| null
| YAMLContentValue[]
| YAMLMappingValue

type YAMLMappingValue = {
export type YAMLMappingValue = {
[key: string]: YAMLContentValue
[key: number]: YAMLContentValue
}
Expand Down Expand Up @@ -76,9 +77,7 @@ const resolver = {
node.body.map((n) => resolver.YAMLDocument(n))
},
YAMLDocument(node: YAMLDocument) {
return node.content
? getValue(node.content, getYAMLVersion(node))
: null
return node.content ? getValue(node.content, node.version) : null
},
YAMLMapping(node: YAMLMapping, version: YAMLVersion | null) {
const result: YAMLMappingValue = {}
Expand Down Expand Up @@ -215,25 +214,3 @@ function getTaggedValue(
${tagText} ${text}`).toJSON()
return value
}

/**
* Get YAML version from then given document
*/
export function getYAMLVersion(document: YAMLDocument): YAMLVersion {
for (const dir of document.directives) {
if (dir.kind === "YAML") {
if (dir.version === "1.1") {
return "1.1"
}
if (dir.version === "1.2") {
return "1.2"
}
if (dir.version === "1.3") {
return "1.3"
}
// Other versions are not supported
return "1.2"
}
}
return "1.2"
}
12 changes: 8 additions & 4 deletions src/yaml-cst-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import type { CST, Document } from "yaml"
import { Composer, Parser } from "yaml"
import type { Context } from "./context"

/** Parse yaml to CST */
export function parseAllDocsToCST(ctx: Context): {
export type ParsedCSTDocs = {
cstNodes: CST.Token[]
nodes: Document.Parsed[]
} {
streamInfo: ReturnType<Composer["streamInfo"]>
}

/** Parse yaml to CST */
export function parseAllDocsToCST(ctx: Context): ParsedCSTDocs {
const parser = new Parser()
const composer = new Composer({
...ctx.options,
keepSourceTokens: true,
})
const cstNodes: CST.Token[] = []
Expand Down Expand Up @@ -38,5 +42,5 @@ export function parseAllDocsToCST(ctx: Context): {
processDoc(doc)
}

return { nodes, cstNodes }
return { nodes, cstNodes, streamInfo: composer.streamInfo() }
}
1 change: 1 addition & 0 deletions tests/fixtures/parser/ast/alias01-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
}
}
},
"version": "1.2",
"range": [
0,
34
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/parser/ast/anchor01-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
}
}
},
"version": "1.2",
"range": [
0,
54
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/parser/ast/astexplorer-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -7103,6 +7103,7 @@
}
}
},
"version": "1.2",
"range": [
0,
5386
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/parser/ast/block-folded01-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
}
}
},
"version": "1.2",
"range": [
0,
17
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/parser/ast/block-folded02-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
}
}
},
"version": "1.2",
"range": [
0,
134
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/parser/ast/block-literal01-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
}
}
},
"version": "1.2",
"range": [
0,
17
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/parser/ast/block-seq01-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
}
}
},
"version": "1.2",
"range": [
4,
35
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
}
}
},
"version": "1.2",
"range": [
0,
49
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
}
}
},
"version": "1.2",
"range": [
0,
49
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/parser/ast/comments01-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
}
}
},
"version": "1.2",
"range": [
0,
32
Expand Down