Skip to content

Commit 9351ef5

Browse files
authored
Merge pull request #101 from redhat-developer/fix-boolean
Fix for redhat-developer/vscode-yaml#116
2 parents 3b79ef3 + 85039b4 commit 9351ef5

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Parse a boolean according to the specification
3+
*
4+
* Return:
5+
* true if its a true value
6+
* false if its a false value
7+
*/
8+
export function parseYamlBoolean(input: string): boolean {
9+
if (["true", "True", "TRUE", 'y', 'Y', 'yes', 'Yes', 'YES', 'on', 'On', 'ON'].lastIndexOf(input) >= 0) {
10+
return true;
11+
}
12+
else if (["false", "False", "FALSE", 'n', 'N', 'no', 'No', 'NO', 'off', 'Off', 'OFF'].lastIndexOf(input) >= 0) {
13+
return false;
14+
}
15+
throw `Invalid boolean "${input}"`
16+
}

src/languageservice/parser/yamlParser.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Kind } from 'yaml-ast-parser'
1515
import { Schema, Type } from 'js-yaml';
1616

1717
import { getLineStartPositions, getPosition } from '../utils/documentPositionCalculator'
18+
import { parseYamlBoolean } from './scalar-type';
1819

1920
export class SingleYAMLDocument extends JSONDocument {
2021
private lines;
@@ -152,8 +153,8 @@ function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode {
152153

153154
//This is a patch for redirecting values with these strings to be boolean nodes because its not supported in the parser.
154155
let possibleBooleanValues = ['y', 'Y', 'yes', 'Yes', 'YES', 'n', 'N', 'no', 'No', 'NO', 'on', 'On', 'ON', 'off', 'Off', 'OFF'];
155-
if (possibleBooleanValues.indexOf(value.toString()) !== -1) {
156-
return new BooleanASTNode(parent, name, value, node.startPosition, node.endPosition)
156+
if (instance.plainScalar && possibleBooleanValues.indexOf(value.toString()) !== -1) {
157+
return new BooleanASTNode(parent, name, parseYamlBoolean(value), node.startPosition, node.endPosition)
157158
}
158159

159160
switch (type) {

test/schemaValidation.test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,47 @@ suite("Validation Tests", () => {
6363
}).then(done, done);
6464
});
6565

66+
it('Test that boolean value in quotations is not interpreted as boolean i.e. it errors', (done) => {
67+
let content = `analytics: "no"`;
68+
let validator = parseSetup(content);
69+
validator.then(function(result){
70+
assert.notEqual(result.length, 0);
71+
}).then(done, done);
72+
});
73+
74+
it('Test that boolean value without quotations is valid', (done) => {
75+
let content = `analytics: no`;
76+
let validator = parseSetup(content);
77+
validator.then(function(result){
78+
assert.equal(result.length, 0);
79+
}).then(done, done);
80+
});
81+
82+
it('Test that boolean is valid when inside strings', (done) => {
83+
let content = `cwd: "no"`;
84+
let validator = parseSetup(content);
85+
validator.then(function(result){
86+
assert.equal(result.length, 0);
87+
}).then(done, done);
88+
});
89+
90+
it('Test that boolean is invalid when no strings present and schema wants string', (done) => {
91+
let content = `cwd: no`;
92+
let validator = parseSetup(content);
93+
validator.then(function(result){
94+
assert.notEqual(result.length, 0);
95+
}).then(done, done);
96+
});
97+
98+
it('Basic test', (done) => {
99+
let content = `analytics: true`;
100+
let validator = parseSetup(content);
101+
validator.then(function(result){
102+
assert.equal(result.length, 0);
103+
}).then(done, done);
104+
});
105+
106+
66107
it('Basic test on nodes with children', (done) => {
67108
let content = `scripts:\n preinstall: test1\n postinstall: test2`;
68109
let validator = parseSetup(content);

0 commit comments

Comments
 (0)