yaml-eslint-parser
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -0,1 +1,2 @@ | ||
import type { YAMLVersion } from "./utils"; | ||
export declare type Range = [number, number]; | ||
@@ -44,2 +45,3 @@ export interface Locations { | ||
}; | ||
version: YAMLVersion; | ||
} | ||
@@ -46,0 +48,0 @@ interface BaseYAMLDirective extends BaseYAMLNode { |
import type { Comment, Locations, Range, Token } from "./ast"; | ||
import type { CST } from "yaml"; | ||
import type { CST, DocumentOptions } from "yaml"; | ||
import { ParseError } from "."; | ||
export declare class Context { | ||
readonly code: string; | ||
readonly options: DocumentOptions; | ||
readonly tokens: Token[]; | ||
@@ -10,3 +11,3 @@ readonly comments: Comment[]; | ||
private readonly locsMap; | ||
constructor(origCode: string); | ||
constructor(origCode: string, parserOptions: any); | ||
getLocFromIndex(index: number): { | ||
@@ -13,0 +14,0 @@ line: number; |
@@ -9,7 +9,9 @@ "use strict"; | ||
const _1 = require("."); | ||
const options_1 = require("./options"); | ||
class Context { | ||
constructor(origCode) { | ||
constructor(origCode, parserOptions) { | ||
this.tokens = []; | ||
this.comments = []; | ||
this.locsMap = new Map(); | ||
this.options = (0, options_1.parserOptionsToYAMLOption)(parserOptions); | ||
const len = origCode.length; | ||
@@ -16,0 +18,0 @@ const lineStartIndices = [0]; |
import type { YAMLProgram } from "./ast"; | ||
import type { Context } from "./context"; | ||
import type { CST, Document } from "yaml"; | ||
import type { ParsedCSTDocs } from "./yaml-cst-parse"; | ||
/** | ||
* Convert yaml root to YAMLProgram | ||
*/ | ||
export declare function convertRoot(cstNodes: CST.Token[], nodes: Document.Parsed[], ctx: Context): YAMLProgram; | ||
export declare function convertRoot(docs: ParsedCSTDocs, ctx: Context): YAMLProgram; |
@@ -5,3 +5,2 @@ "use strict"; | ||
const tags_1 = require("./tags"); | ||
const utils_1 = require("./utils"); | ||
const yaml_1 = require("yaml"); | ||
@@ -87,4 +86,5 @@ const isPair = yaml_1.isPair; | ||
*/ | ||
function convertRoot(cstNodes, nodes, ctx) { | ||
function convertRoot(docs, ctx) { | ||
var _a; | ||
const { cstNodes, nodes } = docs; | ||
const ast = Object.assign({ type: "Program", body: [], comments: ctx.comments, sourceType: "module", tokens: ctx.tokens, parent: null }, ctx.getConvertLocation(0, ctx.code.length)); | ||
@@ -142,3 +142,3 @@ let directives = []; | ||
const index = skipSpaces(ctx.code, 0); | ||
ast.body.push(Object.assign({ type: "YAMLDocument", directives: [], content: null, parent: ast, anchors: {} }, ctx.getConvertLocation(index, index))); | ||
ast.body.push(Object.assign({ type: "YAMLDocument", directives: [], content: null, parent: ast, anchors: {}, version: docs.streamInfo.directives.yaml.version }, ctx.getConvertLocation(index, index))); | ||
} | ||
@@ -159,3 +159,3 @@ sort(ctx.comments); | ||
const loc = ctx.getConvertLocation(skipSpaces(ctx.code, startIndex), node.range[1]); | ||
const ast = Object.assign({ type: "YAMLDocument", directives: [], content: null, parent, anchors: {} }, loc); | ||
const ast = Object.assign({ type: "YAMLDocument", directives: [], content: null, parent, anchors: {}, version: node.directives.yaml.version }, loc); | ||
ast.directives.push(...convertDocumentHead(node.directives, directives, ctx, ast)); | ||
@@ -693,3 +693,3 @@ let last = ast.directives[ast.directives.length - 1]; | ||
const strValue = node.source || cst.source; | ||
const value = parseValueFromText(strValue, (0, utils_1.getYAMLVersion)(doc)); | ||
const value = parseValueFromText(strValue, doc.version || "1.2"); | ||
ast = Object.assign({ type: "YAMLScalar", style: "plain", strValue, | ||
@@ -696,0 +696,0 @@ value, raw: ctx.code.slice(...loc.range), parent }, loc); |
@@ -6,3 +6,3 @@ import type { SourceCode } from "eslint"; | ||
*/ | ||
export declare function parseForESLint(code: string, _options?: any): { | ||
export declare function parseForESLint(code: string, options?: any): { | ||
ast: YAMLProgram; | ||
@@ -9,0 +9,0 @@ visitorKeys: SourceCode.VisitorKeys; |
@@ -11,6 +11,6 @@ "use strict"; | ||
*/ | ||
function parseForESLint(code, _options) { | ||
const ctx = new context_1.Context(code); | ||
function parseForESLint(code, options) { | ||
const ctx = new context_1.Context(code, options); | ||
const docs = (0, yaml_cst_parse_1.parseAllDocsToCST)(ctx); | ||
const ast = (0, convert_1.convertRoot)(docs.cstNodes, docs.nodes, ctx); | ||
const ast = (0, convert_1.convertRoot)(docs, ctx); | ||
return { | ||
@@ -17,0 +17,0 @@ ast, |
export declare const tagResolvers: { | ||
"1.3": (import("./commons").TagResolver<null> | import("./commons").TagResolver<true> | import("./commons").TagResolver<false> | import("./commons").TagResolver<number> | import("./commons").TagResolver<string>)[]; | ||
next: (import("./commons").TagResolver<null> | import("./commons").TagResolver<true> | import("./commons").TagResolver<false> | import("./commons").TagResolver<number> | import("./commons").TagResolver<string>)[]; | ||
"1.2": (import("./commons").TagResolver<null> | import("./commons").TagResolver<true> | import("./commons").TagResolver<false> | import("./commons").TagResolver<number> | import("./commons").TagResolver<string>)[]; | ||
@@ -7,5 +7,5 @@ "1.1": (import("./commons").TagResolver<null> | import("./commons").TagResolver<true> | import("./commons").TagResolver<false> | import("./commons").TagResolver<number> | import("./commons").TagResolver<string>)[]; | ||
export declare const tagNodeResolvers: { | ||
"1.3": import("./commons").TagNodeResolver<Record<any, any>>[]; | ||
next: import("./commons").TagNodeResolver<Record<any, any>>[]; | ||
"1.2": import("./commons").TagNodeResolver<Record<any, any>>[]; | ||
"1.1": import("./commons").TagNodeResolver<Record<any, any>>[]; | ||
}; |
@@ -7,3 +7,3 @@ "use strict"; | ||
exports.tagResolvers = { | ||
"1.3": tags1_2_1.tagResolvers, | ||
next: tags1_2_1.tagResolvers, | ||
"1.2": tags1_2_1.tagResolvers, | ||
@@ -13,5 +13,5 @@ "1.1": tags1_1_1.tagResolvers, | ||
exports.tagNodeResolvers = { | ||
"1.3": tags1_2_1.tagNodeResolvers, | ||
next: tags1_2_1.tagNodeResolvers, | ||
"1.2": tags1_2_1.tagNodeResolvers, | ||
"1.1": tags1_1_1.tagNodeResolvers, | ||
}; |
@@ -0,5 +1,6 @@ | ||
import type { Directives } from "yaml/dist/doc/directives"; | ||
import type { YAMLProgram, YAMLContent, YAMLDocument, YAMLMapping, YAMLSequence, YAMLScalar, YAMLAlias, YAMLPair, YAMLWithMeta } from "./ast"; | ||
export declare type YAMLVersion = "1.3" | "1.2" | "1.1"; | ||
declare type YAMLContentValue = string | number | boolean | null | YAMLContentValue[] | YAMLMappingValue; | ||
declare type YAMLMappingValue = { | ||
export declare type YAMLVersion = Directives["yaml"]["version"]; | ||
export declare type YAMLContentValue = string | number | boolean | null | YAMLContentValue[] | YAMLMappingValue; | ||
export declare type YAMLMappingValue = { | ||
[key: string]: YAMLContentValue; | ||
@@ -12,6 +13,1 @@ [key: number]: YAMLContentValue; | ||
export declare function getStaticYAMLValue(node: YAMLAlias | YAMLProgram | YAMLDocument | YAMLContent | YAMLPair | YAMLWithMeta): YAMLContentValue; | ||
/** | ||
* Get YAML version from then given document | ||
*/ | ||
export declare function getYAMLVersion(document: YAMLDocument): YAMLVersion; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getYAMLVersion = exports.getStaticYAMLValue = void 0; | ||
exports.getStaticYAMLValue = void 0; | ||
const yaml_1 = require("yaml"); | ||
@@ -30,5 +30,3 @@ const tags_1 = require("./tags"); | ||
YAMLDocument(node) { | ||
return node.content | ||
? getValue(node.content, getYAMLVersion(node)) | ||
: null; | ||
return node.content ? getValue(node.content, node.version) : null; | ||
}, | ||
@@ -142,23 +140,1 @@ YAMLMapping(node, version) { | ||
} | ||
/** | ||
* Get YAML version from then given document | ||
*/ | ||
function getYAMLVersion(document) { | ||
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"; | ||
} | ||
exports.getYAMLVersion = getYAMLVersion; |
import type { CST, Document } from "yaml"; | ||
import { Composer } from "yaml"; | ||
import type { Context } from "./context"; | ||
/** Parse yaml to CST */ | ||
export declare function parseAllDocsToCST(ctx: Context): { | ||
export declare type ParsedCSTDocs = { | ||
cstNodes: CST.Token[]; | ||
nodes: Document.Parsed[]; | ||
streamInfo: ReturnType<Composer["streamInfo"]>; | ||
}; | ||
/** Parse yaml to CST */ | ||
export declare function parseAllDocsToCST(ctx: Context): ParsedCSTDocs; |
@@ -8,5 +8,3 @@ "use strict"; | ||
const parser = new yaml_1.Parser(); | ||
const composer = new yaml_1.Composer({ | ||
keepSourceTokens: true, | ||
}); | ||
const composer = new yaml_1.Composer(Object.assign(Object.assign({}, ctx.options), { keepSourceTokens: true })); | ||
const cstNodes = []; | ||
@@ -36,4 +34,4 @@ const nodes = []; | ||
} | ||
return { nodes, cstNodes }; | ||
return { nodes, cstNodes, streamInfo: composer.streamInfo() }; | ||
} | ||
exports.parseAllDocsToCST = parseAllDocsToCST; |
144
package.json
{ | ||
"name": "yaml-eslint-parser", | ||
"version": "1.0.1", | ||
"description": "A YAML parser that produces output compatible with ESLint", | ||
"main": "lib/index.js", | ||
"files": [ | ||
"lib" | ||
], | ||
"engines": { | ||
"node": "^14.17.0 || >=16.0.0" | ||
}, | ||
"scripts": { | ||
"prebuild": "npm run -s clean", | ||
"build": "tsc --project ./tsconfig.build.json", | ||
"clean": "rimraf .nyc_output lib coverage", | ||
"lint": "eslint . --ext .js,.ts,.json", | ||
"eslint-fix": "npm run lint -- --fix", | ||
"test": "mocha --require ts-node/register \"tests/src/**/*.ts\" --reporter dot --timeout 60000", | ||
"cover": "nyc --reporter=lcov npm run test", | ||
"debug": "mocha --require ts-node/register/transpile-only \"tests/src/**/*.ts\" --reporter dot", | ||
"preversion": "npm run lint && npm test", | ||
"update-fixtures": "ts-node ./tools/update-fixtures.ts", | ||
"benchmark": "ts-node --transpile-only benchmark/index.ts" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/ota-meshi/yaml-eslint-parser.git" | ||
}, | ||
"keywords": [ | ||
"yaml", | ||
"eslint", | ||
"parser" | ||
], | ||
"author": "Yosuke Ota", | ||
"funding": "https://github.com/sponsors/ota-meshi", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/ota-meshi/yaml-eslint-parser/issues" | ||
}, | ||
"homepage": "https://github.com/ota-meshi/yaml-eslint-parser#readme", | ||
"dependencies": { | ||
"eslint-visitor-keys": "^3.0.0", | ||
"lodash": "^4.17.21", | ||
"yaml": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@ota-meshi/eslint-plugin": "^0.10.0", | ||
"@types/benchmark": "^2.1.1", | ||
"@types/eslint": "^8.0.0", | ||
"@types/eslint-visitor-keys": "^1.0.0", | ||
"@types/lodash": "^4.14.167", | ||
"@types/mocha": "^9.0.0", | ||
"@types/node": "^16.0.0", | ||
"@typescript-eslint/eslint-plugin": "^5.0.0", | ||
"@typescript-eslint/parser": "^5.0.0", | ||
"benchmark": "^2.1.4", | ||
"eslint": "^8.0.0", | ||
"eslint-config-prettier": "^8.0.0", | ||
"eslint-plugin-eslint-comments": "^3.2.0", | ||
"eslint-plugin-json-schema-validator": "^2.0.0", | ||
"eslint-plugin-jsonc": "^2.0.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"eslint-plugin-regexp": "^1.0.0", | ||
"eslint-plugin-vue": "^9.0.0", | ||
"mocha": "^10.0.0", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.0.5", | ||
"ts-node": "^10.0.0", | ||
"typescript": "^4.0.0", | ||
"vue-eslint-parser": "^9.0.0" | ||
} | ||
"name": "yaml-eslint-parser", | ||
"version": "1.1.0", | ||
"description": "A YAML parser that produces output compatible with ESLint", | ||
"main": "lib/index.js", | ||
"files": [ | ||
"lib" | ||
], | ||
"engines": { | ||
"node": "^14.17.0 || >=16.0.0" | ||
}, | ||
"scripts": { | ||
"prebuild": "npm run -s clean", | ||
"build": "tsc --project ./tsconfig.build.json", | ||
"clean": "rimraf .nyc_output lib coverage", | ||
"lint": "eslint . --ext .js,.ts,.json", | ||
"eslint-fix": "npm run lint -- --fix", | ||
"test": "mocha --require ts-node/register \"tests/src/**/*.ts\" --reporter dot --timeout 60000", | ||
"cover": "nyc --reporter=lcov npm run test", | ||
"debug": "mocha --require ts-node/register/transpile-only \"tests/src/**/*.ts\" --reporter dot", | ||
"preversion": "npm run lint && npm test", | ||
"update-fixtures": "ts-node ./tools/update-fixtures.ts", | ||
"benchmark": "ts-node --transpile-only benchmark/index.ts" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/ota-meshi/yaml-eslint-parser.git" | ||
}, | ||
"keywords": [ | ||
"yaml", | ||
"eslint", | ||
"parser" | ||
], | ||
"author": "Yosuke Ota", | ||
"funding": "https://github.com/sponsors/ota-meshi", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/ota-meshi/yaml-eslint-parser/issues" | ||
}, | ||
"homepage": "https://github.com/ota-meshi/yaml-eslint-parser#readme", | ||
"dependencies": { | ||
"eslint-visitor-keys": "^3.0.0", | ||
"lodash": "^4.17.21", | ||
"yaml": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@ota-meshi/eslint-plugin": "^0.11.0", | ||
"@types/benchmark": "^2.1.1", | ||
"@types/eslint": "^8.0.0", | ||
"@types/eslint-visitor-keys": "^1.0.0", | ||
"@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", | ||
"eslint": "^8.0.0", | ||
"eslint-config-prettier": "^8.0.0", | ||
"eslint-plugin-eslint-comments": "^3.2.0", | ||
"eslint-plugin-json-schema-validator": "^4.0.0", | ||
"eslint-plugin-jsonc": "^2.0.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"eslint-plugin-regexp": "^1.0.0", | ||
"eslint-plugin-vue": "^9.0.0", | ||
"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" | ||
} | ||
} |
@@ -36,3 +36,3 @@ # yaml-eslint-parser | ||
"files": ["*.yaml", "*.yml"], | ||
"parser": "yaml-eslint-parser" | ||
"parser": "yaml-eslint-parser", | ||
} | ||
@@ -43,2 +43,29 @@ ] | ||
### 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 | ||
@@ -45,0 +72,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86686
37
2244
108
27