From bd3d77ce2ed23e2deb3fb997cba39ddeee6eb365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Delma=CC=80s?= Date: Mon, 14 Jan 2019 23:20:05 +0000 Subject: [PATCH] allow for all names of tokens to be emitted --- src/dtsCreator.js | 20 +++++++++++++++----- src/tokenValidator.js | 12 ++++++++---- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/dtsCreator.js b/src/dtsCreator.js index 7fa2fad5..6cd7b50f 100644 --- a/src/dtsCreator.js +++ b/src/dtsCreator.js @@ -48,7 +48,15 @@ class DtsContent { get formatted() { if(!this.resultList || !this.resultList.length || this.resultList.length === 0) return ''; - return this.resultList.join(this.EOL) + this.EOL; + + const formatted = `declare const styles: { +${this.resultList.join('\n')} +}; + +export = styles; +`; + + return formatted.replace(/\n/g, this.EOL); } get tokens() { @@ -114,19 +122,21 @@ export class DtsCreator { var messageList = []; var convertKey = this.getConvertKeyMethod(this.camelCase); + const resultList = []; keys.forEach(key => { const convertedKey = convertKey(key); var ret = validator.validate(convertedKey); - if(ret.isValid) { + if(ret.isValidVariableName) { validKeys.push(convertedKey); + resultList.push(` readonly ${convertedKey}: string;`); + }else if (ret.isValidObjectKeyName) { + resultList.push(` readonly '${convertedKey}': string;`); }else{ messageList.push(ret.message); } }); - var result = validKeys.map(k => ('export const ' + k + ': string;')); - var content = new DtsContent({ dropExtension: this.dropExtension, rootDir: this.rootDir, @@ -134,7 +144,7 @@ export class DtsCreator { outDir: this.outDir, rInputPath, rawTokenList: keys, - resultList: result, + resultList, messageList, EOL: this.EOL }); diff --git a/src/tokenValidator.js b/src/tokenValidator.js index 8a273a12..e9b3f3da 100644 --- a/src/tokenValidator.js +++ b/src/tokenValidator.js @@ -6,24 +6,28 @@ export class TokenValidator { validate(key) { if(!key) { return { - isValid: false, + isValidVariableName: false, + isValidObjectKeyName: false, message: 'empty token' }; } if(!/^[$_a-zA-ZÀ-ÿ][0-9a-zA-ZÀ-ÿ$_]*$/.test(key)) { return { - isValid: false, + isValidVariableName: false, + isValidObjectKeyName: true, message: key + ' is not valid TypeScript variable name.' }; } if(RESERVED_WORDS.some(w => w === key)) { return { - isValid: false, + isValidVariableName: false, + isValidObjectKeyName: true, message: key + ' is TypeScript reserved word.' }; } return { - isValid: true + isValidVariableName: true, + isValidObjectKeyName: true, }; } }