Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve .jsx files #826

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions plugin/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
var known = this.modules[resolved]
if (known) return known

if (/\.js$|(?:^\/)[^\.]+$/.test(resolved))
if (/\.jsx?$|(?:^\/)[^\.]+$/.test(resolved))
this.server.addFile(resolved, null, parentFile)
if (!relative) this.nonRelative[name] = resolved
return this.modules[resolved] = new infer.AVal
Expand Down Expand Up @@ -116,7 +116,7 @@
var path = parentFile ? resolvePath(dirName(parentFile), word) : baseName(word)
for (var prop in this.modules) {
if (prop != parentFile && filter(path, prop, query)) {
if (/\.js$/.test(prop)) prop = prop.slice(0, prop.length - 3)
if (/\.jsx?$/.test(prop)) prop = prop.slice(0, prop.length - 3)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be prop.length - 4 because length(".jsx") = 4.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be either 3 or 4, so jou would need two different if statements

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and 'else if' is also appreciated because it won't work with "foo.jsx.js" file name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preferably, just store the regexp match and use match[0].length to get its actual length

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found replace to work well here:
e.g.
prop = prop.replace(/.jsx?/, '')

var added = prop.slice(path.length)
tern.addCompletion(query, completions, word + added, this.modules[prop])
}
Expand Down Expand Up @@ -171,6 +171,7 @@
var server = infer.cx().parent
if (server.findFile(path)) return path
if (server.findFile(path + ".js")) return path + ".js"
if (server.findFile(path + ".jsx")) return path + ".jsx"
}

// Under node, replace completeFileName with a version that actually
Expand All @@ -195,7 +196,7 @@
var projectPath = me.server.normalizeFilename(path.relative(pDir, path.resolve(dir, file)))
if (projectPath == parentFile) return
var value = me.modules[projectPath]
if (/\.js$/.test(file)) file = file.slice(0, file.length - 3)
if (/\.jsx?$/.test(file)) file = file.slice(0, file.length - 3)
tern.addCompletion(query, completions, base + file, value)
}
})
Expand Down