Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/dtsCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -114,27 +122,29 @@ 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,
searchDir: this.searchDir,
outDir: this.outDir,
rInputPath,
rawTokenList: keys,
resultList: result,
resultList,
messageList,
EOL: this.EOL
});
Expand Down
12 changes: 8 additions & 4 deletions src/tokenValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
}