Skip to content

add new quick fix class for import from #791

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

Merged
merged 2 commits into from
Dec 22, 2015
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions dist/main/lang/fixmyts/quickFixRegistry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
var addClassMember_1 = require("./quickFixes/addClassMember");
var addClassMethod_1 = require("./quickFixes/addClassMethod");
var addImportFromStatement_1 = require("./quickFixes/addImportFromStatement");
var addImportStatement_1 = require("./quickFixes/addImportStatement");
var equalsToEquals_1 = require("./quickFixes/equalsToEquals");
var extractVariable_1 = require("./quickFixes/extractVariable");
Expand All @@ -15,6 +16,7 @@ var singleLineCommentToJsdoc_1 = require("./quickFixes/singleLineCommentToJsdoc"
exports.allQuickFixes = [
new addClassMethod_1.AddClassMethod(),
new addClassMember_1.AddClassMember(),
new addImportFromStatement_1.AddImportFromStatement(),
new addImportStatement_1.AddImportStatement(),
new wrapInProperty_1.WrapInProperty(),
new equalsToEquals_1.EqualsToEquals(),
Expand Down
58 changes: 58 additions & 0 deletions dist/main/lang/fixmyts/quickFixes/addImportFromStatement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"use strict";
var os_1 = require("os");
var displayPartsToString = ts.displayPartsToString, typeToDisplayParts = ts.typeToDisplayParts;
var getPathCompletions_1 = require("../../modules/getPathCompletions");
function getIdentifierAndFileNames(error, project) {
var errorText = error.messageText;
if (typeof errorText !== 'string') {
return undefined;
}
;
var match = errorText.match(/Cannot find name \'(\w+)\'./);
if (!match)
return;
var identifierName = match[1];
var files = getPathCompletions_1.getPathCompletions({
project: project,
filePath: error.file.fileName,
prefix: identifierName,
includeExternalModules: false
}).files;
var file = files.length > 0 ? files[0].relativePath : undefined;
var basename = files.length > 0 ? files[0].name : undefined;
return { identifierName: identifierName, file: file, basename: basename };
}
var AddImportFromStatement = (function () {
function AddImportFromStatement() {
this.key = AddImportFromStatement.name;
}
AddImportFromStatement.prototype.canProvideFix = function (info) {
var relevantError = info.positionErrors.filter(function (x) { return x.code == 2304; })[0];
if (!relevantError)
return;
if (info.positionNode.kind !== ts.SyntaxKind.Identifier)
return;
var matches = getIdentifierAndFileNames(relevantError, info.project);
if (!matches)
return;
var identifierName = matches.identifierName, file = matches.file;
return file ? { display: "import {" + identifierName + "} from \"" + file + "\"" } : undefined;
};
AddImportFromStatement.prototype.provideFix = function (info) {
var relevantError = info.positionErrors.filter(function (x) { return x.code == 2304; })[0];
var identifier = info.positionNode;
var identifierName = identifier.text;
var fileNameforFix = getIdentifierAndFileNames(relevantError, info.project);
var refactorings = [{
span: {
start: 0,
length: 0
},
newText: "import {" + identifierName + "} from \"" + fileNameforFix.file + "\";" + os_1.EOL,
filePath: info.sourceFile.fileName
}];
return refactorings;
};
return AddImportFromStatement;
})();
exports.AddImportFromStatement = AddImportFromStatement;
2 changes: 2 additions & 0 deletions lib/main/lang/fixmyts/quickFixRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {QuickFix} from "./quickFix";
*/
import {AddClassMember} from "./quickFixes/addClassMember";
import {AddClassMethod} from "./quickFixes/addClassMethod";
import {AddImportFromStatement} from "./quickFixes/addImportFromStatement";
import {AddImportStatement} from "./quickFixes/addImportStatement";
import {EqualsToEquals} from "./quickFixes/equalsToEquals";
import {ExtractVariable} from "./quickFixes/extractVariable";
Expand All @@ -18,6 +19,7 @@ import {SingleLineCommentToJsdoc} from "./quickFixes/singleLineCommentToJsdoc";
export var allQuickFixes: QuickFix[] = [
new AddClassMethod(),
new AddClassMember(),
new AddImportFromStatement(),
new AddImportStatement(),
new WrapInProperty(),
new EqualsToEquals(),
Expand Down
84 changes: 84 additions & 0 deletions lib/main/lang/fixmyts/quickFixes/addImportFromStatement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {QuickFix, QuickFixQueryInformation, Refactoring, CanProvideFixResponse} from "../quickFix";
import * as ast from "../astUtils";
import {EOL } from "os";
var { displayPartsToString, typeToDisplayParts } = ts;
import path = require('path');
import {Project} from "../../core/project";

import {getPathCompletions} from "../../modules/getPathCompletions";

function getIdentifierAndFileNames(error: ts.Diagnostic, project: Project) {

var errorText: string = <any>error.messageText;

// We don't support error chains yet
if (typeof errorText !== 'string') {
return undefined;
};

var match = errorText.match(/Cannot find name \'(\w+)\'./);

// If for whatever reason the error message doesn't match
if (!match) return;

var [, identifierName] = match;
var {files} = getPathCompletions({
project,
filePath: error.file.fileName,
prefix: identifierName,
includeExternalModules: false
});
var file = files.length > 0 ? files[0].relativePath : undefined;
var basename = files.length > 0 ? files[0].name : undefined;
return { identifierName, file, basename };
}

export class AddImportFromStatement implements QuickFix {
key = AddImportFromStatement.name;

canProvideFix(info: QuickFixQueryInformation): CanProvideFixResponse {
var relevantError = info.positionErrors.filter(x=> x.code == 2304)[0];
if (!relevantError) return;
if (info.positionNode.kind !== ts.SyntaxKind.Identifier) return;
var matches = getIdentifierAndFileNames(relevantError, info.project);
if (!matches) return;

var { identifierName, file} = matches;
return file ? { display: `import {${identifierName}} from \"${file}\"` } : undefined;
}

provideFix(info: QuickFixQueryInformation): Refactoring[] {
var relevantError = info.positionErrors.filter(x=> x.code == 2304)[0];
var identifier = <ts.Identifier>info.positionNode;

var identifierName = identifier.text;
var fileNameforFix = getIdentifierAndFileNames(relevantError, info.project);

// Add stuff at the top of the file
let refactorings: Refactoring[] = [{
span: {
start: 0,
length: 0
},
newText: `import {${identifierName}} from \"${fileNameforFix.file}\";${EOL}`,
filePath: info.sourceFile.fileName
}];

// Also refactor the variable name to match the file name
// TODO: the following code only takes into account location
// There may be other locations where this is used.
// Better that they trigger a *rename* explicitly later if they want to rename the variable
// if (identifierName !== fileNameforFix.basename) {
// refactorings.push({
// span: {
// start: identifier.getStart(),
// length: identifier.end - identifier.getStart()
// },
// newText: fileNameforFix.basename,
// filePath: info.srcFile.fileName
// })
// }

return refactorings;
}
}
1 change: 1 addition & 0 deletions lib/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"./main/lang/fixmyts/quickFix.ts",
"./main/lang/fixmyts/quickFixes/addClassMember.ts",
"./main/lang/fixmyts/quickFixes/addClassMethod.ts",
"./main/lang/fixmyts/quickFixes/addImportFromStatement.ts",
"./main/lang/fixmyts/quickFixes/addImportStatement.ts",
"./main/lang/fixmyts/quickFixes/equalsToEquals.ts",
"./main/lang/fixmyts/quickFixes/extractVariable.ts",
Expand Down