Skip to content

Commit

Permalink
Define exports and improve getTypeInfo help (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed May 17, 2017
1 parent d018300 commit b751a56
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
10 changes: 7 additions & 3 deletions src/_bin.ts
Expand Up @@ -281,9 +281,13 @@ function startRepl () {
useGlobal: false
})

const undo = appendEval('')
// Hard fix for TypeScript forcing `Object.defineProperty(exports, ...)`.
appendEval('exports = module.exports\n')

repl.on('reset', undo)
// Bookmark the point where we should reset the REPL state.
const reset = appendEval('')

repl.on('reset', reset)

repl.defineCommand('type', {
help: 'Check the type of a TypeScript identifier',
Expand All @@ -294,7 +298,7 @@ function startRepl () {
}

const undo = appendEval(identifier)
const { name, comment } = service.getTypeInfo(EVAL_PATH, EVAL_INSTANCE.input.length)
const { name, comment } = service.getTypeInfo(EVAL_INSTANCE.input, EVAL_PATH, EVAL_INSTANCE.input.length)

undo()

Expand Down
33 changes: 15 additions & 18 deletions src/index.ts
Expand Up @@ -116,7 +116,7 @@ export interface Register {
cwd: string
extensions: string[]
compile (code: string, fileName: string, lineOffset?: number): string
getTypeInfo (fileName: string, position: number): TypeInfo
getTypeInfo (code: string, fileName: string, position: number): TypeInfo
}

/**
Expand Down Expand Up @@ -146,7 +146,12 @@ export function register (options: Options = {}): Register {
const cacheDirectory = options.cacheDirectory || DEFAULTS.cacheDirectory || getTmpDir()
const compilerOptions = Object.assign({}, DEFAULTS.compilerOptions, options.compilerOptions)
const originalJsHandler = require.extensions['.js']
const cache: Cache = { contents: {}, versions: {}, sourceMaps: {} }

const cache: Cache = {
contents: Object.create(null),
versions: Object.create(null),
sourceMaps: Object.create(null)
}

const ignore = arrify(
(
Expand Down Expand Up @@ -244,36 +249,29 @@ export function register (options: Options = {}): Register {
getExtension
)

let getTypeInfo = function (_fileName: string, _position: number): TypeInfo {
let getTypeInfo = function (_code: string, _fileName: string, _position: number): TypeInfo {
throw new TypeError(`No type information available under "--fast" mode`)
}

// Use full language services when the fast option is disabled.
if (!fast) {
// Add the file to the project.
const addVersion = function (fileName: string) {
if (!cache.versions.hasOwnProperty(fileName)) {
cache.versions[fileName] = 1
}
}

// Set the file contents into cache.
const addCache = function (code: string, fileName: string) {
const setCache = function (code: string, fileName: string) {
cache.contents[fileName] = code
cache.versions[fileName] += 1
cache.versions[fileName] = (cache.versions[fileName] + 1) || 1
}

// Create the compiler host for type checking.
const serviceHost = {
getScriptFileNames: () => Object.keys(cache.versions),
getScriptVersion: (fileName: string) => String(cache.versions[fileName]),
getScriptSnapshot (fileName: string) {
if (!cache.contents.hasOwnProperty(fileName)) {
if (!cache.contents[fileName]) {
if (!fileExists(fileName)) {
return undefined
}

cache.contents[fileName] = getFile(fileName)
setCache(getFile(fileName), fileName)
}

return ts.ScriptSnapshot.fromString(cache.contents[fileName])
Expand Down Expand Up @@ -327,16 +325,15 @@ export function register (options: Options = {}): Register {
fileExists,
cache,
function (code: string, fileName: string, lineOffset?: number) {
addVersion(fileName)
addCache(code, fileName)
setCache(code, fileName)

return getOutput(code, fileName, lineOffset)
},
getExtension
)

getTypeInfo = function (fileName: string, position: number) {
addVersion(fileName)
getTypeInfo = function (code: string, fileName: string, position: number) {
setCache(code, fileName)

const info = service.getQuickInfoAtPosition(fileName, position)
const name = ts.displayPartsToString(info ? info.displayParts : [])
Expand Down

0 comments on commit b751a56

Please sign in to comment.