yaml-eslint-parser
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -27,3 +27,3 @@ export declare type Range = [number, number]; | ||
} | ||
export declare type YAMLNode = YAMLProgram | YAMLDocument | YAMLDirective | YAMLContent | YAMLPair | YAMLAnchor | YAMLTag; | ||
export declare type YAMLNode = YAMLProgram | YAMLDocument | YAMLDirective | YAMLContent | YAMLPair | YAMLWithMark | YAMLAnchor | YAMLTag; | ||
export interface YAMLProgram extends BaseYAMLNode { | ||
@@ -40,3 +40,3 @@ type: "Program"; | ||
directives: YAMLDirective[]; | ||
content: YAMLContent | null; | ||
content: YAMLContent | YAMLWithMark | null; | ||
parent: YAMLProgram; | ||
@@ -52,6 +52,13 @@ anchors: { | ||
} | ||
export interface YAMLWithMark extends BaseYAMLNode { | ||
type: "YAMLWithMark"; | ||
anchor: YAMLAnchor | null; | ||
tag: YAMLTag | null; | ||
value: YAMLContent | null; | ||
parent: YAMLDocument | YAMLPair | YAMLSequence; | ||
} | ||
export interface YAMLAnchor extends BaseYAMLNode { | ||
type: "YAMLAnchor"; | ||
name: string; | ||
parent: YAMLContent; | ||
parent: YAMLWithMark; | ||
} | ||
@@ -61,8 +68,6 @@ export interface YAMLTag extends BaseYAMLNode { | ||
tag: string; | ||
parent: YAMLContent; | ||
parent: YAMLWithMark; | ||
} | ||
interface BaseYAMLContentNode extends BaseYAMLNode { | ||
anchor: null | YAMLAnchor; | ||
tag: null | YAMLTag; | ||
parent: YAMLDocument | YAMLPair | YAMLSequence; | ||
parent: YAMLDocument | YAMLPair | YAMLSequence | YAMLWithMark; | ||
} | ||
@@ -83,4 +88,4 @@ export declare type YAMLContent = YAMLMapping | YAMLSequence | YAMLScalar | YAMLAlias; | ||
type: "YAMLPair"; | ||
key: YAMLContent | null; | ||
value: YAMLContent | null; | ||
key: YAMLContent | YAMLWithMark | null; | ||
value: YAMLContent | YAMLWithMark | null; | ||
parent: YAMLMapping; | ||
@@ -92,3 +97,3 @@ } | ||
style: "block"; | ||
entries: YAMLContent[]; | ||
entries: (YAMLContent | YAMLWithMark)[]; | ||
} | ||
@@ -98,3 +103,3 @@ export interface YAMLFlowSequence extends BaseYAMLContentNode { | ||
style: "flow"; | ||
entries: YAMLContent[]; | ||
entries: (YAMLContent | YAMLWithMark)[]; | ||
} | ||
@@ -106,3 +111,3 @@ export declare type YAMLScalar = YAMLPlainScalar | YAMLDoubleQuotedScalar | YAMLSingleQuotedScalar | YAMLBlockLiteralScalar | YAMLBlockFoldedScalar; | ||
strValue: string; | ||
readonly value: string | number | boolean | null; | ||
value: string | number | boolean | null; | ||
} | ||
@@ -113,3 +118,4 @@ export interface YAMLDoubleQuotedScalar extends BaseYAMLContentNode { | ||
strValue: string; | ||
readonly value: string | number | boolean | null; | ||
value: string; | ||
raw: string; | ||
} | ||
@@ -120,3 +126,4 @@ export interface YAMLSingleQuotedScalar extends BaseYAMLContentNode { | ||
strValue: string; | ||
readonly value: string | number | boolean | null; | ||
value: string; | ||
raw: string; | ||
} | ||
@@ -123,0 +130,0 @@ export interface YAMLBlockLiteralScalar extends BaseYAMLContentNode { |
@@ -8,2 +8,3 @@ "use strict"; | ||
const yaml_1 = __importDefault(require("yaml")); | ||
const utils_1 = require("./utils"); | ||
/** | ||
@@ -56,3 +57,17 @@ * Convert yaml-unist-parser root to YAMLProgram | ||
} | ||
tokens.sort((a, b) => a.range[0] - b.range[0]); | ||
tokens.sort((a, b) => { | ||
if (a.range[0] > b.range[0]) { | ||
return 1; | ||
} | ||
if (a.range[0] < b.range[0]) { | ||
return -1; | ||
} | ||
if (a.range[1] > b.range[1]) { | ||
return 1; | ||
} | ||
if (a.range[1] < b.range[1]) { | ||
return -1; | ||
} | ||
return 0; | ||
}); | ||
return ast; | ||
@@ -183,10 +198,7 @@ /** | ||
const loc = getConvertLocation(node); | ||
const ast = Object.assign({ type: "YAMLMapping", style: "block", pairs: [], anchor: null, tag: null, parent }, loc); | ||
const ast = Object.assign({ type: "YAMLMapping", style: "block", pairs: [], parent }, loc); | ||
for (const n of node.children) { | ||
ast.pairs.push(convertMappingItem(n, tokens, code, ast, doc)); | ||
} | ||
const { anchor, tag } = convertAnchorAndTag(node, tokens, code, ast, doc); | ||
ast.anchor = anchor; | ||
ast.tag = tag; | ||
return ast; | ||
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast); | ||
} | ||
@@ -198,10 +210,7 @@ /** | ||
const loc = getConvertLocation(node); | ||
const ast = Object.assign({ type: "YAMLMapping", style: "flow", pairs: [], anchor: null, tag: null, parent }, loc); | ||
const ast = Object.assign({ type: "YAMLMapping", style: "flow", pairs: [], parent }, loc); | ||
for (const n of node.children) { | ||
ast.pairs.push(convertMappingItem(n, tokens, code, ast, doc)); | ||
} | ||
const { anchor, tag } = convertAnchorAndTag(node, tokens, code, ast, doc); | ||
ast.anchor = anchor; | ||
ast.tag = tag; | ||
return ast; | ||
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast); | ||
} | ||
@@ -241,10 +250,7 @@ /** | ||
const loc = getConvertLocation(node); | ||
const ast = Object.assign({ type: "YAMLSequence", style: "block", entries: [], anchor: null, tag: null, parent }, loc); | ||
const ast = Object.assign({ type: "YAMLSequence", style: "block", entries: [], parent }, loc); | ||
for (const n of node.children) { | ||
ast.entries.push(...convertSequenceItem(n, tokens, code, ast, doc)); | ||
} | ||
const { anchor, tag } = convertAnchorAndTag(node, tokens, code, ast, doc); | ||
ast.anchor = anchor; | ||
ast.tag = tag; | ||
return ast; | ||
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast); | ||
} | ||
@@ -256,3 +262,3 @@ /** | ||
const loc = getConvertLocation(node); | ||
const ast = Object.assign({ type: "YAMLSequence", style: "flow", entries: [], anchor: null, tag: null, parent }, loc); | ||
const ast = Object.assign({ type: "YAMLSequence", style: "flow", entries: [], parent }, loc); | ||
for (const n of node.children) { | ||
@@ -263,3 +269,3 @@ if (n.type === "flowSequenceItem") { | ||
if (n.type === "flowMappingItem") { | ||
const map = Object.assign({ type: "YAMLMapping", style: "block", pairs: [], anchor: null, tag: null, parent }, getConvertLocation(n)); | ||
const map = Object.assign({ type: "YAMLMapping", style: "block", pairs: [], parent }, getConvertLocation(n)); | ||
const pair = convertMappingItem(n, tokens, code, map, doc); | ||
@@ -270,6 +276,3 @@ map.pairs.push(pair); | ||
} | ||
const { anchor, tag } = convertAnchorAndTag(node, tokens, code, ast, doc); | ||
ast.anchor = anchor; | ||
ast.tag = tag; | ||
return ast; | ||
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast); | ||
} | ||
@@ -289,47 +292,39 @@ /** | ||
const loc = getConvertLocation(node); | ||
const strValue = node.value; | ||
let value; | ||
let tokenValue; | ||
if (isTrue(strValue)) { | ||
tokenValue = true; | ||
if (loc.range[0] < loc.range[1]) { | ||
const strValue = node.value; | ||
let value; | ||
if (utils_1.isTrue(strValue)) { | ||
value = true; | ||
} | ||
else if (utils_1.isFalse(strValue)) { | ||
value = false; | ||
} | ||
else if (utils_1.isNull(strValue)) { | ||
value = null; | ||
} | ||
else if (needParse(strValue)) { | ||
value = yaml_1.default.parse(strValue) || strValue; | ||
} | ||
else { | ||
value = strValue; | ||
} | ||
const ast = Object.assign({ type: "YAMLScalar", style: "plain", strValue, | ||
value, | ||
parent }, loc); | ||
const type = typeof value; | ||
if (type === "boolean") { | ||
addToken(tokens, "Boolean", clone(loc), code); | ||
} | ||
else if (type === "number" && isFinite(Number(value))) { | ||
addToken(tokens, "Numeric", clone(loc), code); | ||
} | ||
else if (value === null) { | ||
addToken(tokens, "Null", clone(loc), code); | ||
} | ||
else { | ||
addToken(tokens, "Identifier", clone(loc), code); | ||
} | ||
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, loc); | ||
} | ||
else if (isFalse(strValue)) { | ||
tokenValue = false; | ||
} | ||
else if (isNull(strValue)) { | ||
tokenValue = null; | ||
} | ||
else if (needParse(strValue)) { | ||
tokenValue = yaml_1.default.parse(strValue) || strValue; | ||
} | ||
else { | ||
tokenValue = strValue; | ||
} | ||
const ast = Object.assign({ type: "YAMLScalar", style: "plain", strValue, | ||
get value() { | ||
if (value !== undefined) { | ||
return value; | ||
} | ||
if (node.tag) { | ||
return (value = getTaggedValue(node.tag, strValue, strValue)); | ||
} | ||
return (value = tokenValue); | ||
}, anchor: null, tag: null, parent }, loc); | ||
const { anchor, tag } = convertAnchorAndTag(node, tokens, code, ast, doc); | ||
ast.anchor = anchor; | ||
ast.tag = tag; | ||
const type = typeof tokenValue; | ||
if (type === "boolean") { | ||
addToken(tokens, "Boolean", clone(loc), code); | ||
} | ||
else if (type === "number" && isFinite(Number(tokenValue))) { | ||
addToken(tokens, "Numeric", clone(loc), code); | ||
} | ||
else if (tokenValue === null) { | ||
addToken(tokens, "Null", clone(loc), code); | ||
} | ||
else { | ||
addToken(tokens, "Identifier", clone(loc), code); | ||
} | ||
return ast; | ||
return convertAnchorAndTag(node, tokens, code, parent, null, doc, loc); | ||
/** | ||
@@ -363,19 +358,5 @@ * Checks if the given string needs to be parsed | ||
const strValue = node.value; | ||
let value; | ||
const ast = Object.assign({ type: "YAMLScalar", style: "double-quoted", strValue, | ||
get value() { | ||
if (value !== undefined) { | ||
return value; | ||
} | ||
if (node.tag) { | ||
const text = code.slice(...loc.range); | ||
return (value = getTaggedValue(node.tag, text, node.value)); | ||
} | ||
return (value = strValue); | ||
}, anchor: null, tag: null, parent }, loc); | ||
const { anchor, tag } = convertAnchorAndTag(node, tokens, code, ast, doc); | ||
ast.anchor = anchor; | ||
ast.tag = tag; | ||
const ast = Object.assign({ type: "YAMLScalar", style: "double-quoted", strValue, value: strValue, raw: code.slice(...loc.range), parent }, loc); | ||
addToken(tokens, "String", clone(loc), code); | ||
return ast; | ||
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast); | ||
} | ||
@@ -388,19 +369,5 @@ /** | ||
const strValue = node.value; | ||
let value; | ||
const ast = Object.assign({ type: "YAMLScalar", style: "single-quoted", strValue, | ||
get value() { | ||
if (value !== undefined) { | ||
return value; | ||
} | ||
if (node.tag) { | ||
const text = code.slice(...loc.range); | ||
return (value = getTaggedValue(node.tag, text, node.value)); | ||
} | ||
return (value = strValue); | ||
}, anchor: null, tag: null, parent }, loc); | ||
const { anchor, tag } = convertAnchorAndTag(node, tokens, code, ast, doc); | ||
ast.anchor = anchor; | ||
ast.tag = tag; | ||
const ast = Object.assign({ type: "YAMLScalar", style: "single-quoted", strValue, value: strValue, raw: code.slice(...loc.range), parent }, loc); | ||
addToken(tokens, "String", clone(loc), code); | ||
return ast; | ||
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast); | ||
} | ||
@@ -413,6 +380,4 @@ /** | ||
const value = node.value; | ||
const ast = Object.assign({ type: "YAMLScalar", style: "literal", chomping: node.chomping, indent: node.indent, value, anchor: null, tag: null, parent }, loc); | ||
const { anchor, tag } = convertAnchorAndTag(node, tokens, code, ast, doc); | ||
ast.anchor = anchor; | ||
ast.tag = tag; | ||
const ast = Object.assign({ type: "YAMLScalar", style: "literal", chomping: node.chomping, indent: node.indent, value, | ||
parent }, loc); | ||
const text = code.slice(...loc.range); | ||
@@ -476,3 +441,3 @@ if (text.startsWith("|")) { | ||
} | ||
return ast; | ||
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast); | ||
} | ||
@@ -485,6 +450,4 @@ /** | ||
const value = node.value; | ||
const ast = Object.assign({ type: "YAMLScalar", style: "folded", chomping: node.chomping, indent: node.indent, value, anchor: null, tag: null, parent }, loc); | ||
const { anchor, tag } = convertAnchorAndTag(node, tokens, code, ast, doc); | ||
ast.anchor = anchor; | ||
ast.tag = tag; | ||
const ast = Object.assign({ type: "YAMLScalar", style: "folded", chomping: node.chomping, indent: node.indent, value, | ||
parent }, loc); | ||
const text = code.slice(...loc.range); | ||
@@ -548,3 +511,3 @@ if (text.startsWith(">")) { | ||
} | ||
return ast; | ||
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast); | ||
} | ||
@@ -557,6 +520,3 @@ /** | ||
const value = node.value; | ||
const ast = Object.assign({ type: "YAMLAlias", name: value, anchor: null, tag: null, parent }, loc); | ||
const { anchor, tag } = convertAnchorAndTag(node, tokens, code, ast, doc); | ||
ast.anchor = anchor; | ||
ast.tag = tag; | ||
const ast = Object.assign({ type: "YAMLAlias", name: value, parent }, loc); | ||
const text = code.slice(...loc.range); | ||
@@ -588,3 +548,3 @@ if (text.startsWith("*")) { | ||
} | ||
return ast; | ||
return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast); | ||
} | ||
@@ -594,9 +554,33 @@ /** | ||
*/ | ||
function convertAnchorAndTag(node, tokens, code, parent, doc) { | ||
return { | ||
anchor: node.anchor | ||
? convertAnchor(node.anchor, tokens, code, parent, doc) | ||
: null, | ||
tag: node.tag ? convertTag(node.tag, tokens, code, parent) : null, | ||
}; | ||
function convertAnchorAndTag(node, tokens, code, parent, value, doc, valueLoc) { | ||
if (node.anchor || node.tag) { | ||
const ast = { | ||
type: "YAMLWithMark", | ||
anchor: null, | ||
tag: null, | ||
value, | ||
parent, | ||
range: clone(valueLoc.range), | ||
loc: clone(valueLoc.loc), | ||
}; | ||
if (value) { | ||
value.parent = ast; | ||
} | ||
if (node.anchor) { | ||
const anchor = convertAnchor(node.anchor, tokens, code, ast, doc); | ||
ast.anchor = anchor; | ||
ast.range[0] = anchor.range[0]; | ||
ast.loc.start = clone(anchor.loc.start); | ||
} | ||
if (node.tag) { | ||
const tag = convertTag(node.tag, tokens, code, ast); | ||
ast.tag = tag; | ||
if (tag.range[0] < ast.range[0]) { | ||
ast.range[0] = tag.range[0]; | ||
ast.loc.start = clone(tag.loc.start); | ||
} | ||
} | ||
return ast; | ||
} | ||
return value; | ||
} | ||
@@ -676,30 +660,2 @@ /** | ||
/** | ||
* Get tagged value | ||
*/ | ||
function getTaggedValue(tag, text, str) { | ||
if (tag.value === "tag:yaml.org,2002:str") { | ||
return str; | ||
} | ||
else if (tag.value === "tag:yaml.org,2002:int") { | ||
if (/^(?:[1-9]\d*|0)$/u.test(str)) { | ||
return parseInt(str, 10); | ||
} | ||
} | ||
else if (tag.value === "tag:yaml.org,2002:bool") { | ||
if (isTrue(str)) { | ||
return true; | ||
} | ||
if (isFalse(str)) { | ||
return false; | ||
} | ||
} | ||
else if (tag.value === "tag:yaml.org,2002:null") { | ||
if (isNull(str) || str === "") { | ||
return null; | ||
} | ||
} | ||
const tagText = tag.value.startsWith("!") ? tag.value : `!<${tag.value}>`; | ||
return yaml_1.default.parseDocument(`${tagText} ${text}`).toJSON(); | ||
} | ||
/** | ||
* Get the location information of the given node. | ||
@@ -747,19 +703,1 @@ * @param node The node. | ||
} | ||
/** | ||
* Checks if the given string is true | ||
*/ | ||
function isTrue(str) { | ||
return str === "true" || str === "True" || str === "TRUE"; | ||
} | ||
/** | ||
* Checks if the given string is false | ||
*/ | ||
function isFalse(str) { | ||
return str === "false" || str === "False" || str === "FALSE"; | ||
} | ||
/** | ||
* Checks if the given string is null | ||
*/ | ||
function isNull(str) { | ||
return str === "null" || str === "Null" || str === "NULL" || str === "~"; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { YAMLProgram, YAMLContent, YAMLDocument, YAMLMapping, YAMLSequence, YAMLScalar, YAMLAlias, YAMLPair } from "./ast"; | ||
import { YAMLProgram, YAMLContent, YAMLDocument, YAMLMapping, YAMLSequence, YAMLScalar, YAMLAlias, YAMLPair, YAMLWithMark } from "./ast"; | ||
declare type YAMLContentValue = string | number | boolean | null | YAMLContentValue[] | YAMLMappingValue; | ||
@@ -11,3 +11,15 @@ declare type YAMLMappingValue = { | ||
export declare function getStaticYAMLValue(node: YAMLAlias): YAMLContentValue; | ||
export declare function getStaticYAMLValue(node: YAMLProgram | YAMLDocument | YAMLContent | YAMLPair): YAMLContentValue; | ||
export declare function getStaticYAMLValue(node: YAMLProgram | YAMLDocument | YAMLContent | YAMLPair | YAMLWithMark): YAMLContentValue; | ||
/** | ||
* Checks if the given string is true | ||
*/ | ||
export declare function isTrue(str: string): boolean; | ||
/** | ||
* Checks if the given string is false | ||
*/ | ||
export declare function isFalse(str: string): boolean; | ||
/** | ||
* Checks if the given string is null | ||
*/ | ||
export declare function isNull(str: string): boolean; | ||
export {}; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getStaticYAMLValue = void 0; | ||
exports.isNull = exports.isFalse = exports.isTrue = exports.getStaticYAMLValue = void 0; | ||
const yaml_1 = __importDefault(require("yaml")); | ||
/** | ||
@@ -53,2 +57,22 @@ * Gets the static value for the given node. | ||
}, | ||
YAMLWithMark(node) { | ||
if (node.tag) { | ||
if (node.value == null) { | ||
return getTaggedValue(node.tag, "", ""); | ||
} | ||
if (node.value.type === "YAMLScalar") { | ||
if (node.value.style === "plain") { | ||
return getTaggedValue(node.tag, node.value.strValue, node.value.strValue); | ||
} | ||
if (node.value.style === "double-quoted" || | ||
node.value.style === "single-quoted") { | ||
return getTaggedValue(node.tag, node.value.raw, node.value.strValue); | ||
} | ||
} | ||
} | ||
if (node.value == null) { | ||
return null; | ||
} | ||
return getStaticYAMLValue(node.value); | ||
}, | ||
}; | ||
@@ -70,1 +94,50 @@ /** | ||
} | ||
/** | ||
* Get tagged value | ||
*/ | ||
function getTaggedValue(tag, text, str) { | ||
if (tag.tag === "tag:yaml.org,2002:str") { | ||
return str; | ||
} | ||
else if (tag.tag === "tag:yaml.org,2002:int") { | ||
if (/^(?:[1-9]\d*|0)$/u.test(str)) { | ||
return parseInt(str, 10); | ||
} | ||
} | ||
else if (tag.tag === "tag:yaml.org,2002:bool") { | ||
if (isTrue(str)) { | ||
return true; | ||
} | ||
if (isFalse(str)) { | ||
return false; | ||
} | ||
} | ||
else if (tag.tag === "tag:yaml.org,2002:null") { | ||
if (isNull(str) || str === "") { | ||
return null; | ||
} | ||
} | ||
const tagText = tag.tag.startsWith("!") ? tag.tag : `!<${tag.tag}>`; | ||
return yaml_1.default.parseDocument(`${tagText} ${text}`).toJSON(); | ||
} | ||
/** | ||
* Checks if the given string is true | ||
*/ | ||
function isTrue(str) { | ||
return str === "true" || str === "True" || str === "TRUE"; | ||
} | ||
exports.isTrue = isTrue; | ||
/** | ||
* Checks if the given string is false | ||
*/ | ||
function isFalse(str) { | ||
return str === "false" || str === "False" || str === "FALSE"; | ||
} | ||
exports.isFalse = isFalse; | ||
/** | ||
* Checks if the given string is null | ||
*/ | ||
function isNull(str) { | ||
return str === "null" || str === "Null" || str === "NULL" || str === "~"; | ||
} | ||
exports.isNull = isNull; |
@@ -9,7 +9,8 @@ "use strict"; | ||
YAMLDirective: [], | ||
YAMLMapping: ["pairs", "anchor", "tag"], | ||
YAMLMapping: ["pairs"], | ||
YAMLPair: ["key", "value"], | ||
YAMLSequence: ["entries", "anchor", "tag"], | ||
YAMLScalar: ["anchor", "tag"], | ||
YAMLAlias: ["anchor", "tag"], | ||
YAMLSequence: ["entries"], | ||
YAMLWithMark: ["anchor", "tag", "value"], | ||
YAMLScalar: [], | ||
YAMLAlias: [], | ||
YAMLAnchor: [], | ||
@@ -16,0 +17,0 @@ YAMLTag: [], |
{ | ||
"name": "yaml-eslint-parser", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "A YAML parser that produces output compatible with ESLint", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
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
41892
1122