Refactoring getFileReferenceFromReferencePath for both parser and language service
This commit is contained in:
parent
6b5f50afb7
commit
3182751462
|
@ -20,7 +20,7 @@ module ts {
|
|||
interface ReferenceComments {
|
||||
referencedFiles: FileReference[];
|
||||
amdDependencies: string[];
|
||||
}
|
||||
}
|
||||
|
||||
export function getSourceFileOfNode(node: Node): SourceFile {
|
||||
while (node && node.kind !== SyntaxKind.SourceFile) node = node.parent;
|
||||
|
@ -641,6 +641,54 @@ module ts {
|
|||
nodeIsNestedInLabel(label: Identifier, requireIterationStatement: boolean, stopAtFunctionBoundary: boolean): ControlBlockContext;
|
||||
}
|
||||
|
||||
|
||||
|
||||
interface ReferencePathMatchResult {
|
||||
fileReference: FileReference
|
||||
diagnostic: DiagnosticMessage
|
||||
isNoDefaultLib: boolean
|
||||
}
|
||||
|
||||
// TODO (yuisu) : add referencedFiles array for filling in
|
||||
export function getFileReferenceFromReferencePath(comment: string, commentRange: CommentRange): ReferencePathMatchResult {
|
||||
var simpleReferenceRegEx = /^\/\/\/\s*<reference\s+/gim;
|
||||
var isNoDefaultLibRegEx = /^(\/\/\/\s*<reference\s+no-default-lib\s*=\s*)('|")(.+?)\2\s*\/>/gim;
|
||||
if (simpleReferenceRegEx.exec(comment)) {
|
||||
if (isNoDefaultLibRegEx.exec(comment)) {
|
||||
return {
|
||||
fileReference: undefined,
|
||||
diagnostic: undefined,
|
||||
isNoDefaultLib: true
|
||||
}
|
||||
}
|
||||
else {
|
||||
var matchResult = fullTripleSlashReferencePathRegEx.exec(comment);
|
||||
if (matchResult) {
|
||||
var start = commentRange.pos;
|
||||
var end = commentRange.end;
|
||||
var fileRef = {
|
||||
pos: start,
|
||||
end: end,
|
||||
filename: matchResult[3]
|
||||
};
|
||||
return {
|
||||
fileReference: fileRef,
|
||||
diagnostic: undefined,
|
||||
isNoDefaultLib: false
|
||||
};
|
||||
}
|
||||
else {
|
||||
return {
|
||||
fileReference: undefined,
|
||||
diagnostic: Diagnostics.Invalid_reference_directive_syntax,
|
||||
isNoDefaultLib: false
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function isKeyword(token: SyntaxKind): boolean {
|
||||
return SyntaxKind.FirstKeyword <= token && token <= SyntaxKind.LastKeyword;
|
||||
}
|
||||
|
@ -3896,28 +3944,16 @@ module ts {
|
|||
for (var i = 0; i < commentRanges.length; i++) {
|
||||
var range = commentRanges[i];
|
||||
var comment = sourceText.substring(range.pos, range.end);
|
||||
var simpleReferenceRegEx = /^\/\/\/\s*<reference\s+/gim;
|
||||
if (simpleReferenceRegEx.exec(comment)) {
|
||||
var isNoDefaultLibRegEx = /^(\/\/\/\s*<reference\s+no-default-lib=)('|")(.+?)\2\s*\/>/gim;
|
||||
if (isNoDefaultLibRegEx.exec(comment)) {
|
||||
file.hasNoDefaultLib = true;
|
||||
var referencePathMatchResult = getFileReferenceFromReferencePath(comment, range);
|
||||
if (referencePathMatchResult) {
|
||||
var fileReference = referencePathMatchResult.fileReference;
|
||||
file.hasNoDefaultLib = referencePathMatchResult.isNoDefaultLib;
|
||||
var diagnostic = referencePathMatchResult.diagnostic;
|
||||
if (fileReference) {
|
||||
referencedFiles.push(fileReference);
|
||||
}
|
||||
else {
|
||||
var matchResult = fullTripleSlashReferencePathRegEx.exec(comment);
|
||||
var start = range.pos;
|
||||
var end = range.end;
|
||||
var length = end - start;
|
||||
|
||||
if (!matchResult) {
|
||||
errorAtPos(start, length, Diagnostics.Invalid_reference_directive_syntax);
|
||||
}
|
||||
else {
|
||||
referencedFiles.push({
|
||||
pos: start,
|
||||
end: end,
|
||||
filename: matchResult[3]
|
||||
});
|
||||
}
|
||||
if (diagnostic) {
|
||||
errorAtPos(range.pos, range.end - range.pos, diagnostic);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -1915,57 +1915,32 @@ module ts {
|
|||
}
|
||||
|
||||
export var tripleSlashReferenceRegExp = /^(\/\/\/\s*<reference\s+path=)('|")(.+?)\2\s*(static=('|")(.+?)\5\s*)*\/>/;
|
||||
export function preProcessFile(fileName: string, sourceText: TypeScript.IScriptSnapshot, readImportFiles = true): PreProcessedFileInfo {
|
||||
|
||||
var reportDiagnostic = () => { }
|
||||
var text = sourceText.getText(0, sourceText.getLength());
|
||||
export function preProcessFile(sourceText: string, readImportFiles = true): PreProcessedFileInfo {
|
||||
var referencedFiles: IFileReference[] = [];
|
||||
var importedFiles: IFileReference[] = [];
|
||||
|
||||
function isNoDefaultLibMatch(comment: string): RegExpExecArray {
|
||||
var isNoDefaultLibRegex = /^(\/\/\/\s*<reference\s+no-default-lib=)('|")(.+?)\2\s*\/>/gim;
|
||||
return isNoDefaultLibRegex.exec(comment);
|
||||
}
|
||||
|
||||
// TODO : merge this function with processReferenceComments in parser as both do similar functionality
|
||||
function getFileReferenceFromReferencePath(comment: string): IFileReference {
|
||||
var simpleReferenceRegEx = /^\/\/\/\s*<reference\s+/gim;
|
||||
|
||||
if (simpleReferenceRegEx.exec(comment)) {
|
||||
var isNoDefaultLib = isNoDefaultLibMatch(comment);
|
||||
if (!isNoDefaultLib) {
|
||||
var fullReferenceRegEx = tripleSlashReferenceRegExp;
|
||||
var fullReference = fullReferenceRegEx.exec(comment);
|
||||
var path: string = normalizePath(fullReference[3]);
|
||||
if (fullReference) {
|
||||
var path: string = normalizePath(fullReference[3]);
|
||||
var adjustedPath = normalizePath(path);
|
||||
|
||||
return {
|
||||
path: switchToForwardSlashes(adjustedPath),
|
||||
position: 0,
|
||||
length: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var isNoDefaultLib = false;
|
||||
|
||||
function processTripleSlashDirectives(): void {
|
||||
var commentRanges = getLeadingCommentRanges(text, 0);
|
||||
var commentRanges = getLeadingCommentRanges(sourceText, 0);
|
||||
forEach(commentRanges, commentRange => {
|
||||
var comment = text.substring(commentRange.pos, commentRange.end);
|
||||
var referencedFile = getFileReferenceFromReferencePath(comment);
|
||||
if (referencedFile) {
|
||||
referencedFile.position = commentRange.pos;
|
||||
referencedFile.length = commentRange.end - commentRange.pos;
|
||||
referencedFiles.push(referencedFile);
|
||||
var comment = sourceText.substring(commentRange.pos, commentRange.end);
|
||||
var referencePathMatchResult = getFileReferenceFromReferencePath(comment, commentRange);
|
||||
if (referencePathMatchResult) {
|
||||
isNoDefaultLib = referencePathMatchResult.isNoDefaultLib;
|
||||
var fileReference = referencePathMatchResult.fileReference;
|
||||
if (fileReference) {
|
||||
referencedFiles.push({
|
||||
path: switchToForwardSlashes(normalizePath(fileReference.filename)),
|
||||
position: fileReference.pos,
|
||||
length: fileReference.end - fileReference.pos,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function processImport(): void {
|
||||
var scanner = createScanner(getDefaultCompilerOptions().target, /*skipTrivia*/true, text);
|
||||
var scanner = createScanner(getDefaultCompilerOptions().target, /*skipTrivia*/true, sourceText);
|
||||
|
||||
var token = scanner.scan();
|
||||
// Look for:
|
||||
|
@ -1999,9 +1974,13 @@ module ts {
|
|||
token = scanner.scan();
|
||||
}
|
||||
}
|
||||
processImport();
|
||||
|
||||
if (readImportFiles) {
|
||||
processImport();
|
||||
}
|
||||
processTripleSlashDirectives();
|
||||
return { referencedFiles: referencedFiles, importedFiles: importedFiles, isLibFile: false, diagnostics: [] };
|
||||
// TODO (yuisu) : remove diagnostics array
|
||||
return { referencedFiles: referencedFiles, importedFiles: importedFiles, isLibFile: isNoDefaultLib, diagnostics: [] };
|
||||
}
|
||||
|
||||
/// Helpers
|
||||
|
|
|
@ -842,11 +842,11 @@ module ts {
|
|||
return forwardJSONCall(this.logger, actionDescription, action);
|
||||
}
|
||||
|
||||
public getPreProcessedFileInfo(fileName: string, sourceText: TypeScript.IScriptSnapshot): string {
|
||||
public getPreProcessedFileInfo(fileName: string, sourceTextSnapshot: TypeScript.IScriptSnapshot): string {
|
||||
return this.forwardJSONCall(
|
||||
"getPreProcessedFileInfo('" + fileName + "')",
|
||||
() => {
|
||||
var result = preProcessFile(fileName, sourceText); //TypeScript.preProcessFile(fileName, sourceText);
|
||||
var result = preProcessFile(sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength()));
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue