Skip to content

Commit 8ef1f32

Browse files
committed
Merge pull request #791 from jschwarty/master
add new quick fix class for import from
2 parents 9a5c29f + 2412c5c commit 8ef1f32

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed

dist/main/lang/fixmyts/quickFixRegistry.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22
var addClassMember_1 = require("./quickFixes/addClassMember");
33
var addClassMethod_1 = require("./quickFixes/addClassMethod");
4+
var addImportFromStatement_1 = require("./quickFixes/addImportFromStatement");
45
var addImportStatement_1 = require("./quickFixes/addImportStatement");
56
var equalsToEquals_1 = require("./quickFixes/equalsToEquals");
67
var extractVariable_1 = require("./quickFixes/extractVariable");
@@ -15,6 +16,7 @@ var singleLineCommentToJsdoc_1 = require("./quickFixes/singleLineCommentToJsdoc"
1516
exports.allQuickFixes = [
1617
new addClassMethod_1.AddClassMethod(),
1718
new addClassMember_1.AddClassMember(),
19+
new addImportFromStatement_1.AddImportFromStatement(),
1820
new addImportStatement_1.AddImportStatement(),
1921
new wrapInProperty_1.WrapInProperty(),
2022
new equalsToEquals_1.EqualsToEquals(),
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"use strict";
2+
var os_1 = require("os");
3+
var displayPartsToString = ts.displayPartsToString, typeToDisplayParts = ts.typeToDisplayParts;
4+
var getPathCompletions_1 = require("../../modules/getPathCompletions");
5+
function getIdentifierAndFileNames(error, project) {
6+
var errorText = error.messageText;
7+
if (typeof errorText !== 'string') {
8+
return undefined;
9+
}
10+
;
11+
var match = errorText.match(/Cannot find name \'(\w+)\'./);
12+
if (!match)
13+
return;
14+
var identifierName = match[1];
15+
var files = getPathCompletions_1.getPathCompletions({
16+
project: project,
17+
filePath: error.file.fileName,
18+
prefix: identifierName,
19+
includeExternalModules: false
20+
}).files;
21+
var file = files.length > 0 ? files[0].relativePath : undefined;
22+
var basename = files.length > 0 ? files[0].name : undefined;
23+
return { identifierName: identifierName, file: file, basename: basename };
24+
}
25+
var AddImportFromStatement = (function () {
26+
function AddImportFromStatement() {
27+
this.key = AddImportFromStatement.name;
28+
}
29+
AddImportFromStatement.prototype.canProvideFix = function (info) {
30+
var relevantError = info.positionErrors.filter(function (x) { return x.code == 2304; })[0];
31+
if (!relevantError)
32+
return;
33+
if (info.positionNode.kind !== ts.SyntaxKind.Identifier)
34+
return;
35+
var matches = getIdentifierAndFileNames(relevantError, info.project);
36+
if (!matches)
37+
return;
38+
var identifierName = matches.identifierName, file = matches.file;
39+
return file ? { display: "import {" + identifierName + "} from \"" + file + "\"" } : undefined;
40+
};
41+
AddImportFromStatement.prototype.provideFix = function (info) {
42+
var relevantError = info.positionErrors.filter(function (x) { return x.code == 2304; })[0];
43+
var identifier = info.positionNode;
44+
var identifierName = identifier.text;
45+
var fileNameforFix = getIdentifierAndFileNames(relevantError, info.project);
46+
var refactorings = [{
47+
span: {
48+
start: 0,
49+
length: 0
50+
},
51+
newText: "import {" + identifierName + "} from \"" + fileNameforFix.file + "\";" + os_1.EOL,
52+
filePath: info.sourceFile.fileName
53+
}];
54+
return refactorings;
55+
};
56+
return AddImportFromStatement;
57+
})();
58+
exports.AddImportFromStatement = AddImportFromStatement;

lib/main/lang/fixmyts/quickFixRegistry.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {QuickFix} from "./quickFix";
44
*/
55
import {AddClassMember} from "./quickFixes/addClassMember";
66
import {AddClassMethod} from "./quickFixes/addClassMethod";
7+
import {AddImportFromStatement} from "./quickFixes/addImportFromStatement";
78
import {AddImportStatement} from "./quickFixes/addImportStatement";
89
import {EqualsToEquals} from "./quickFixes/equalsToEquals";
910
import {ExtractVariable} from "./quickFixes/extractVariable";
@@ -18,6 +19,7 @@ import {SingleLineCommentToJsdoc} from "./quickFixes/singleLineCommentToJsdoc";
1819
export var allQuickFixes: QuickFix[] = [
1920
new AddClassMethod(),
2021
new AddClassMember(),
22+
new AddImportFromStatement(),
2123
new AddImportStatement(),
2224
new WrapInProperty(),
2325
new EqualsToEquals(),
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import {QuickFix, QuickFixQueryInformation, Refactoring, CanProvideFixResponse} from "../quickFix";
2+
import * as ast from "../astUtils";
3+
import {EOL } from "os";
4+
var { displayPartsToString, typeToDisplayParts } = ts;
5+
import path = require('path');
6+
import {Project} from "../../core/project";
7+
8+
import {getPathCompletions} from "../../modules/getPathCompletions";
9+
10+
function getIdentifierAndFileNames(error: ts.Diagnostic, project: Project) {
11+
12+
var errorText: string = <any>error.messageText;
13+
14+
// We don't support error chains yet
15+
if (typeof errorText !== 'string') {
16+
return undefined;
17+
};
18+
19+
var match = errorText.match(/Cannot find name \'(\w+)\'./);
20+
21+
// If for whatever reason the error message doesn't match
22+
if (!match) return;
23+
24+
var [, identifierName] = match;
25+
var {files} = getPathCompletions({
26+
project,
27+
filePath: error.file.fileName,
28+
prefix: identifierName,
29+
includeExternalModules: false
30+
});
31+
var file = files.length > 0 ? files[0].relativePath : undefined;
32+
var basename = files.length > 0 ? files[0].name : undefined;
33+
return { identifierName, file, basename };
34+
}
35+
36+
export class AddImportFromStatement implements QuickFix {
37+
key = AddImportFromStatement.name;
38+
39+
canProvideFix(info: QuickFixQueryInformation): CanProvideFixResponse {
40+
var relevantError = info.positionErrors.filter(x=> x.code == 2304)[0];
41+
if (!relevantError) return;
42+
if (info.positionNode.kind !== ts.SyntaxKind.Identifier) return;
43+
var matches = getIdentifierAndFileNames(relevantError, info.project);
44+
if (!matches) return;
45+
46+
var { identifierName, file} = matches;
47+
return file ? { display: `import {${identifierName}} from \"${file}\"` } : undefined;
48+
}
49+
50+
provideFix(info: QuickFixQueryInformation): Refactoring[] {
51+
var relevantError = info.positionErrors.filter(x=> x.code == 2304)[0];
52+
var identifier = <ts.Identifier>info.positionNode;
53+
54+
var identifierName = identifier.text;
55+
var fileNameforFix = getIdentifierAndFileNames(relevantError, info.project);
56+
57+
// Add stuff at the top of the file
58+
let refactorings: Refactoring[] = [{
59+
span: {
60+
start: 0,
61+
length: 0
62+
},
63+
newText: `import {${identifierName}} from \"${fileNameforFix.file}\";${EOL}`,
64+
filePath: info.sourceFile.fileName
65+
}];
66+
67+
// Also refactor the variable name to match the file name
68+
// TODO: the following code only takes into account location
69+
// There may be other locations where this is used.
70+
// Better that they trigger a *rename* explicitly later if they want to rename the variable
71+
// if (identifierName !== fileNameforFix.basename) {
72+
// refactorings.push({
73+
// span: {
74+
// start: identifier.getStart(),
75+
// length: identifier.end - identifier.getStart()
76+
// },
77+
// newText: fileNameforFix.basename,
78+
// filePath: info.srcFile.fileName
79+
// })
80+
// }
81+
82+
return refactorings;
83+
}
84+
}

lib/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"./main/lang/fixmyts/quickFix.ts",
6969
"./main/lang/fixmyts/quickFixes/addClassMember.ts",
7070
"./main/lang/fixmyts/quickFixes/addClassMethod.ts",
71+
"./main/lang/fixmyts/quickFixes/addImportFromStatement.ts",
7172
"./main/lang/fixmyts/quickFixes/addImportStatement.ts",
7273
"./main/lang/fixmyts/quickFixes/equalsToEquals.ts",
7374
"./main/lang/fixmyts/quickFixes/extractVariable.ts",

0 commit comments

Comments
 (0)