From ffc6b88f84bba641d6a378cdb48b180b18744a42 Mon Sep 17 00:00:00 2001 From: "Anantachai Saothong (Manta)" Date: Sun, 27 Jan 2019 20:31:32 +0700 Subject: [PATCH] Fixed "fileExtension" setting --- CHANGELOG.md | 3 +++ edge/JavaScript.ts | 23 ++++++++++++++--------- edge/TypeScript.ts | 6 ++++++ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b58121f..066eae0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### 2.6.3 +- Fixed `codeQuicken.javascript.fileExtension` setting so it does not put TS and TSX file extensions regardless of the setting. + ### 2.6.2 - Amended `codeQuicken.javascript.predefinedVariableNames` and `codeQuicken.typescript.predefinedVariableNames` settings so it accepts a workspace file path as a regular expression. - Amended `codeQuicken.javascript.fileExtension` and `codeQuicken.typescript.fileExtension` settings so it has the default value of `true`. diff --git a/edge/JavaScript.ts b/edge/JavaScript.ts index 0f0e853..f978fa1 100644 --- a/edge/JavaScript.ts +++ b/edge/JavaScript.ts @@ -20,7 +20,7 @@ export interface LanguageOptions { filteredFileList: { [currentFilePattern: string]: string } } -const SUPPORTED_EXTENSION = /^(j|t)sx?$/i +const SUPPORTED_EXTENSION = /\.(j|t)sx?$/i export default class JavaScript implements Language { private fileItemCache: Array @@ -46,6 +46,10 @@ export default class JavaScript implements Language { return true } + checkIfFileExtensionShouldBeRemoved(fileExtensionWithLeadingDot: string, document: vscode.TextDocument) { + return this.options.fileExtension === false && SUPPORTED_EXTENSION.test(fileExtensionWithLeadingDot) + } + async getItems(document: vscode.TextDocument) { if (hasFileExtensionOf(document, await this.getCompatibleFileExtensions()) === false) { return null @@ -440,7 +444,7 @@ export class FileItem implements Item { this.label = this.fileInfo.directoryName this.description = _.trim(this.fileInfo.fullPath.substring(rootPath.length), fp.sep) } else { - this.label = this.language.options.fileExtension ? this.fileInfo.fileNameWithExtension : this.fileInfo.fileNameWithoutExtension + this.label = this.fileInfo.fileNameWithExtension this.description = _.trim(fp.dirname(this.fileInfo.fullPath.substring(rootPath.length)), fp.sep) } } @@ -460,9 +464,9 @@ export class FileItem implements Item { // Remove "/index.js" from the path path = fp.dirname(path) - } else if (options.fileExtension === false && SUPPORTED_EXTENSION.test(this.fileInfo.fileExtensionWithoutLeadingDot)) { - // Remove file extension from the path only if it matches the working document - path = path.replace(new RegExp('\\.' + _.escapeRegExp(this.fileInfo.fileExtensionWithoutLeadingDot) + '$'), '') + } else if (this.language.checkIfFileExtensionShouldBeRemoved(fp.extname(this.fileInfo.fileNameWithExtension), document)) { + // Remove file extension from the path + path = path.replace(new RegExp(_.escapeRegExp(fp.extname(this.fileInfo.fileNameWithExtension)) + '$'), '') } return { name, path } @@ -476,7 +480,7 @@ export class FileItem implements Item { const existingImports = getExistingImports(codeTree) - if (SUPPORTED_EXTENSION.test(this.fileInfo.fileExtensionWithoutLeadingDot)) { + if (SUPPORTED_EXTENSION.test(this.fileInfo.fileNameWithExtension)) { const pattern = await this.getImportPatternForJavaScript(existingImports, document) if (!pattern) { return null @@ -684,11 +688,12 @@ export class FileItem implements Item { } let indexFileRelativePath = new FileInfo(indexFilePath).getRelativePath(workingDirectory) + const fileExtension = fp.extname(indexFileRelativePath) if (options.indexFile === false) { indexFileRelativePath = fp.dirname(indexFileRelativePath) - } else if (options.fileExtension === false) { - indexFileRelativePath = indexFileRelativePath.replace(new RegExp(_.escapeRegExp(fp.extname(indexFileRelativePath)) + '$'), '') + } else if (this.language.checkIfFileExtensionShouldBeRemoved(fileExtension, document)) { + indexFileRelativePath = indexFileRelativePath.replace(new RegExp(_.escapeRegExp(fileExtension) + '$'), '') } const duplicateImportForIndexFile = getDuplicateImport(existingImports, indexFileRelativePath) @@ -1027,7 +1032,7 @@ class NodeItem implements Item { function checkIfIndexFile(fileNameWithExtension: string) { const parts = fileNameWithExtension.split('.') - return parts.length === 2 && parts[0] === 'index' && SUPPORTED_EXTENSION.test(parts[1]) + return parts.length === 2 && parts[0] === 'index' && SUPPORTED_EXTENSION.test(fileNameWithExtension) } function getRequirePath(node: ts.Node) { diff --git a/edge/TypeScript.ts b/edge/TypeScript.ts index f8d978b..b5595d3 100644 --- a/edge/TypeScript.ts +++ b/edge/TypeScript.ts @@ -7,6 +7,7 @@ import JavaScript from './JavaScript' import * as ts from 'typescript' const JAVASCRIPT_EXTENSION = /\.jsx?$/i +const TYPESCRIPT_EXTENSION = /\.tsx?$/i export default class TypeScript extends JavaScript { constructor(config: Configurations, fileWatch: vscode.FileSystemWatcher) { @@ -31,6 +32,11 @@ export default class TypeScript extends JavaScript { return ['ts', 'tsx'] } + checkIfFileExtensionShouldBeRemoved(targetFileExtension: string, document: vscode.TextDocument) { + return super.checkIfFileExtensionShouldBeRemoved(targetFileExtension, document) || + document.languageId.startsWith('typescript') && TYPESCRIPT_EXTENSION.test(targetFileExtension) + } + async checkIfImportDefaultIsPreferredOverNamespace() { const tsConfig = await this.getTypeScriptConfigurations() return _.get(tsConfig, 'compilerOptions.esModuleInterop', false)