提交 0bd44492 编写于 作者: M Matt Bierner 提交者: chrmarti

Consistently access config values in launch.config (fixes #13051)

上级 70bd724e
......@@ -302,6 +302,11 @@
"from": "object.omit@>=2.0.0 <3.0.0",
"resolved": "https://registry.npmjs.org/object.omit/-/object.omit-2.0.0.tgz"
},
"objectpath": {
"version": "1.2.1",
"from": "objectpath@>=1.2.1 <2.0.0",
"resolved": "https://registry.npmjs.org/objectpath/-/objectpath-1.2.1.tgz"
},
"oniguruma": {
"version": "6.1.1",
"from": "oniguruma@>=6.0.1 <7.0.0",
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
declare module "objectpath" {
export function parse(path: string): string[];
}
......@@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as ObjectPath from 'objectpath';
import nls = require('vs/nls');
import * as paths from 'vs/base/common/paths';
import * as types from 'vs/base/common/types';
......@@ -130,10 +131,26 @@ export class ConfigurationResolverService implements IConfigurationResolverServi
}
private resolveConfigVariable(value: string, originalValue: string): string {
let regexp = /\$\{config\.(.*?)\}/g;
return value.replace(regexp, (match: string, name: string) => {
let regexp = /\$\{config(\.|(?=\[))(.+?)\}/g;
return value.replace(regexp, (match: string, lead: string, name: string) => {
let config = this.configurationService.getConfiguration();
let newValue = new Function('_', 'try {return _.' + name + ';} catch (ex) { return "";}')(config);
let newValue: any;
try {
const keys: string[] = ObjectPath.parse(name);
if (!keys || keys.length <= 0) {
return '';
}
while (keys.length > 1) {
const key = keys.shift();
if (!config || !config.hasOwnProperty(key)) {
return '';
}
config = config[key];
}
newValue = config ? config[keys[0]] : '';
} catch (e) {
return '';
}
if (types.isString(newValue)) {
// Prevent infinite recursion and also support nested references (or tokens)
return newValue === originalValue ? '' : this.resolveString(newValue);
......
......@@ -212,6 +212,46 @@ suite('Configuration Resolver Service', () => {
assert.strictEqual(service.resolve('abc ${config.editor.fontFamily} ${config.editor.lineNumbers} ${config.editor.insertSpaces} ${config.json.schemas[0].fileMatch[1]} xyz'), 'abc foo 123 false {{/myOtherfile}} xyz');
});
test('configuration variables using bracket accessor', () => {
let configurationService: IConfigurationService;
configurationService = new MockConfigurationService({
editor: {
fontFamily: 'foo'
}
});
let service = new ConfigurationResolverService(uri.parse('file:///VSCode/workspaceLocation'), envVariables, new TestEditorService(), TestEnvironmentService, configurationService, mockCommandService);
assert.strictEqual(service.resolve("abc ${config.editor['fontFamily']} xyz"), 'abc foo xyz');
assert.strictEqual(service.resolve('abc ${config["editor"].fontFamily} xyz'), 'abc foo xyz');
assert.strictEqual(service.resolve('abc ${config["editor"]["fontFamily"]} xyz'), 'abc foo xyz');
});
test('configuration variables with invalid accessor', () => {
let configurationService: IConfigurationService;
configurationService = new MockConfigurationService({
editor: {
fontFamily: 'foo'
}
});
let service = new ConfigurationResolverService(uri.parse('file:///VSCode/workspaceLocation'), envVariables, new TestEditorService(), TestEnvironmentService, configurationService, mockCommandService);
assert.strictEqual(service.resolve("abc ${config.} xyz"), 'abc ${config.} xyz');
assert.strictEqual(service.resolve("abc ${config.editor..fontFamily} xyz"), 'abc xyz');
assert.strictEqual(service.resolve("abc ${config.editor.none.none2} xyz"), 'abc xyz');
});
test('configuration should not evaluate Javascript', () => {
let configurationService: IConfigurationService;
configurationService = new MockConfigurationService({
editor: {
a: 'foo'
}
});
let service = new ConfigurationResolverService(uri.parse('file:///VSCode/workspaceLocation'), envVariables, new TestEditorService(), TestEnvironmentService, configurationService, mockCommandService);
assert.strictEqual(service.resolve("abc ${config.editor['abc'.substr(0)]} xyz"), 'abc undefined xyz');
});
test('interactive variable simple', () => {
const configuration = {
'name': 'Attach to Process',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册