Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Fixed "fileExtension" setting
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsManta committed Jan 27, 2019
1 parent 91f2a44 commit ffc6b88
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
23 changes: 14 additions & 9 deletions edge/JavaScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileItem>
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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 }
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions edge/TypeScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<boolean>(tsConfig, 'compilerOptions.esModuleInterop', false)
Expand Down

0 comments on commit ffc6b88

Please sign in to comment.