Skip to content

Commit

Permalink
Ensure .tsx files are supported. (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
nozzlegear authored and bencoveney committed Jul 14, 2017
1 parent 46e938b commit b571b0e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,21 @@ describe("builder/builder module has a", () => {
assert.equal(result, "./directory2/script");
});
});
describe("getBasename function that", () => {
it("should correctly strip .ts from the filename", () => {
const fileName = "./random/path/file.ts";
const result = Builder.getBasename(fileName);
assert.equal(result, "file");
});
it("should correctly strip .tsx from the filename", () => {
const fileName = "./random/path/file.tsx";
const result = Builder.getBasename(fileName);
assert.equal(result, "file");
});
it("should not strip extensions from non-typescript filenames", () => {
const fileName = "./random/path/file.cs";
const result = Builder.getBasename(fileName);
assert.equal(result, "file.cs");
});
});
});
13 changes: 11 additions & 2 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,18 @@ export function buildImportPath(directory: Directory, target: Location): string
if (directoryPath !== ".") {
directoryPath = `.${path.sep}${directoryPath}`;
}
// Strip off the .ts from the file name.
const fileName = path.basename(relativePath, ".ts");
// Strip off the .ts or .tsx from the file name.
const fileName = getBasename(relativePath);
// Build the final path string. Use posix-style seperators.
const location = `${directoryPath}${path.sep}${fileName}`;
return convertPathSeparator(location);
}

/** Strips the .ts or .tsx file extension from a path and returns the base filename. */
export function getBasename(relativePath: string) {
const strippedTsPath = path.basename(relativePath, ".ts");
const strippedTsxPath = path.basename(relativePath, ".tsx");

// Return whichever path is shorter. If they're the same length then nothing was stripped.
return strippedTsPath.length < strippedTsxPath.length ? strippedTsPath : strippedTsxPath;
}
5 changes: 4 additions & 1 deletion src/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ describe("utilities module has a", () => {
it("should match a typescript definition file", () => {
assert.notEqual("definitions.d.ts".search(Utilities.isTypeScriptFile), -1);
});
it("should match a typescript jsx (.tsx) file", () => {
assert.notEqual("other.tsx".search(Utilities.isTypeScriptFile), -1);
});
it("should not match a non-typescript file", () => {
assert.equal("other.tsx".search(Utilities.isTypeScriptFile), -1);
assert.equal("other.cs".search(Utilities.isTypeScriptFile), -1);
});
});
describe("nonAlphaNumeric regular expression that", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export function convertPathSeparator(path: string): string {
return path.replace(/\\+/g, "/");
}

export const isTypeScriptFile = /\.ts$/m;
export const isTypeScriptFile = /\.tsx?$/m;
export const nonAlphaNumeric = /\W+/g;
export const indentation = " ";

0 comments on commit b571b0e

Please sign in to comment.