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

Commit

Permalink
Added ability to import TypeScript files to JavaScript files
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsManta committed Jun 15, 2018
1 parent 2295d6a commit 24df825
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 26 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 1.5.0
- Added ability to import TypeScript files to JavaScript files.

### 1.4.0
- Amended picking the closest `package.json` from the current active document.
- Fixed TypeScript compilation errors.
Expand Down
45 changes: 26 additions & 19 deletions edge/JavaScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,51 @@ export interface LanguageOptions {
filteredFileList: object
}

export interface ExclusiveLanguageOptions extends LanguageOptions {
allowTypeScriptFiles: boolean
}

const TYPESCRIPT_EXTENSION = /^tsx?$/

export default class JavaScript implements Language {
protected baseConfig: RootConfigurations
private fileItemCache: Array<FileItem>
private nodeItemCache: Array<NodeItem>

protected SUPPORTED_LANGUAGE = /^javascript(react)?/i
protected SUPPORTED_EXTENSION = /^jsx?$/
protected WORKING_LANGUAGE = /^javascript(react)?/i
protected WORKING_EXTENSION = /^jsx?$/
protected allowTypeScriptFiles = false

constructor(baseConfig: RootConfigurations) {
this.baseConfig = baseConfig
}

protected rejectSomeFiles(item: FileItem) {
// Remove TypeScript files as JavaScript does not recognize them anyway
return /^tsx?$/.test(item.fileInfo.fileExtensionWithoutLeadingDot)
const exclusiveConfig: ExclusiveLanguageOptions = this.baseConfig.javascript
if (exclusiveConfig && exclusiveConfig.allowTypeScriptFiles) {
this.allowTypeScriptFiles = true

this.WORKING_EXTENSION = /^(j|t)sx?$/
}
}

protected getLanguageOptions() {
return this.baseConfig.javascript
return this.baseConfig.javascript as LanguageOptions
}

async getItems(document: vscode.TextDocument) {
if (this.SUPPORTED_LANGUAGE.test(document.languageId) === false) {
if (this.WORKING_LANGUAGE.test(document.languageId) === false) {
return null
}

let items: Array<Item>

const documentFileInfo = new FileInfo(document.fileName)
const documentIsJavaScript = /^jsx?$/.test(documentFileInfo.fileExtensionWithoutLeadingDot)
const rootPath = vscode.workspace.getWorkspaceFolder(document.uri).uri.fsPath

if (!this.fileItemCache) {
const fileLinks = await vscode.workspace.findFiles('**/*.*')

this.fileItemCache = fileLinks
.map(fileLink => new FileItem(fileLink.fsPath, rootPath, this.getLanguageOptions(), this.SUPPORTED_EXTENSION))
.map(fileLink => new FileItem(fileLink.fsPath, rootPath, this.getLanguageOptions(), this.WORKING_EXTENSION))
}

const fileFilterRule = _.toPairs(this.getLanguageOptions().filteredFileList)
Expand All @@ -64,7 +72,8 @@ export default class JavaScript implements Language {

items = _.chain(this.fileItemCache)
.reject(item => item.fileInfo.fullPath === documentFileInfo.fullPath) // Remove the current file
.reject(item => this.rejectSomeFiles(item))
.filter(item => this.allowTypeScriptFiles ||
!TYPESCRIPT_EXTENSION.test(item.fileInfo.fileExtensionWithoutLeadingDot))
.filter(item => fileFilterRule ? fileFilterRule.filePathPattern.test(item.fileInfo.fullPathForPOSIX) : true)
.forEach(item => item.sortablePath = getSortablePath(item.fileInfo, documentFileInfo))
.value()
Expand Down Expand Up @@ -102,7 +111,7 @@ export default class JavaScript implements Language {
addItem(filePath: string) {
if (this.fileItemCache) {
const rootPath = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(filePath)).uri.fsPath
this.fileItemCache.push(new FileItem(filePath, rootPath, this.getLanguageOptions(), this.SUPPORTED_EXTENSION))
this.fileItemCache.push(new FileItem(filePath, rootPath, this.getLanguageOptions(), this.WORKING_EXTENSION))
}
}

Expand All @@ -117,12 +126,10 @@ export default class JavaScript implements Language {
}

async fixImport(editor: vscode.TextEditor, document: vscode.TextDocument, cancellationToken: vscode.CancellationToken) {
if (this.SUPPORTED_LANGUAGE.test(document.languageId) === false) {
if (this.WORKING_LANGUAGE.test(document.languageId) === false) {
return false
}

const actions: Array<(worker: vscode.TextEditorEdit) => void> = []

const documentFileInfo = new FileInfo(document.fileName)
const rootPath = vscode.workspace.getWorkspaceFolder(document.uri).uri.fsPath

Expand Down Expand Up @@ -158,7 +165,7 @@ export default class JavaScript implements Language {
readonly description: string
readonly fullPath: string

constructor(fullPath: string, originalRelativePath: string) {
constructor(fullPath: string) {
this.fullPath = fullPath
this.label = fullPath.substring(rootPath.length).replace(/\\/g, '/')
}
Expand Down Expand Up @@ -206,7 +213,7 @@ export default class JavaScript implements Language {

} else if (matchingFullPaths.length === 1) {
await editor.edit(worker => {
const { path } = new FileItem(matchingFullPaths[0], rootPath, this.getLanguageOptions(), this.SUPPORTED_EXTENSION).getNameAndRelativePath(documentFileInfo.directoryPath)
const { path } = new FileItem(matchingFullPaths[0], rootPath, this.getLanguageOptions(), this.WORKING_EXTENSION).getNameAndRelativePath(documentFileInfo.directoryPath)
worker.replace(item.editableRange, `${item.quoteChar}${path}${item.quoteChar}`)
})

Expand All @@ -218,7 +225,7 @@ export default class JavaScript implements Language {
for (const item of manualSolvableImports) {
const matchingFullPaths = await item.search()

const candidateItems = matchingFullPaths.map(path => new FileItemForFixingImport(path, item.originalRelativePath))
const candidateItems = matchingFullPaths.map(path => new FileItemForFixingImport(path))
const selectedItem = await vscode.window.showQuickPick(candidateItems, { placeHolder: item.originalRelativePath, ignoreFocusOut: true })
if (!selectedItem) {
return null
Expand All @@ -229,7 +236,7 @@ export default class JavaScript implements Language {
}

await editor.edit(worker => {
const { path } = new FileItem(selectedItem.fullPath, rootPath, this.getLanguageOptions(), this.SUPPORTED_EXTENSION).getNameAndRelativePath(documentFileInfo.directoryPath)
const { path } = new FileItem(selectedItem.fullPath, rootPath, this.getLanguageOptions(), this.WORKING_EXTENSION).getNameAndRelativePath(documentFileInfo.directoryPath)
worker.replace(item.editableRange, `${item.quoteChar}${path}${item.quoteChar}`)
})
}
Expand Down
13 changes: 8 additions & 5 deletions edge/TypeScript.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import JavaScript, { FileItem } from './JavaScript'
import { RootConfigurations } from './global'
import JavaScript from './JavaScript'

export default class TypeScript extends JavaScript {
protected SUPPORTED_LANGUAGE = /^typescript(react)?/i
protected SUPPORTED_EXTENSION = /^(j|t)sx?$/
protected WORKING_LANGUAGE = /^typescript(react)?/i
protected WORKING_EXTENSION = /^(j|t)sx?$/

protected rejectSomeFiles(item: FileItem) {
return false
constructor(baseConfig: RootConfigurations) {
super(baseConfig)

this.allowTypeScriptFiles = true
}

protected getLanguageOptions() {
Expand Down
2 changes: 1 addition & 1 deletion edge/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as Stylus from './Stylus'

export interface RootConfigurations {
history: number
javascript: JavaScript.LanguageOptions
javascript: JavaScript.ExclusiveLanguageOptions
typescript: JavaScript.LanguageOptions
stylus: Stylus.LanguageOptions
}
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@
},
"default": {}
},
"codeQuicken.javascript.allowTypeScriptFiles": {
"description": "Allow inserting TypeScript files.",
"type": "boolean",
"default": false
},
"codeQuicken.typescript.syntax": {
"description": "Insert either ES2015's import or AMD's require syntax.",
"type": "string",
Expand Down Expand Up @@ -255,4 +260,4 @@
"lodash": "^4.17.10",
"stylus": "^0.54.5"
}
}
}

0 comments on commit 24df825

Please sign in to comment.