-
Notifications
You must be signed in to change notification settings - Fork 13k
Add new compiler option --rootDir #2772
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
Changes from 6 commits
e7895c5
261adff
99c0adb
a2e888d
16bbedc
af661ae
b6ef323
8964b8e
b3fcae8
021f309
08a8692
ed2e105
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -453,6 +453,66 @@ module ts { | |
} | ||
} | ||
|
||
function computecommonSourceDirectory(sourceFiles: SourceFile[]): string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. compute**_C**_ommon |
||
let commonPathComponents: string[]; | ||
let currentDirectory = host.getCurrentDirectory(); | ||
forEach(files, sourceFile => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you meant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also use a for..of unless you're really hesitant to use a label. |
||
// Each file contributes into common source file path | ||
if (isDeclarationFile(sourceFile)) { | ||
return; | ||
} | ||
|
||
let sourcePathComponents = getNormalizedPathComponents(sourceFile.fileName, currentDirectory); | ||
sourcePathComponents.pop(); // FileName is not part of directory | ||
if (commonPathComponents) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (!commonPathComponents) {
// first file
commonPathComponents = sourcePathComponents;
continue; // or return if you don't change forEach to a for..of
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't seem it though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i did add the return.. not sure why i need to move the else clause though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm saying instead of having a big |
||
for (let i = 0; i < Math.min(commonPathComponents.length, sourcePathComponents.length); i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cache the min |
||
if (commonPathComponents[i] !== sourcePathComponents[i]) { | ||
if (i === 0) { | ||
diagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_find_the_common_subdirectory_path_for_the_input_files)); | ||
return; | ||
} | ||
|
||
// New common path found that is 0 -> i-1 | ||
commonPathComponents.length = i; | ||
break; | ||
} | ||
} | ||
|
||
// If the fileComponent path completely matched and less than already found update the length | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what you mean. |
||
if (sourcePathComponents.length < commonPathComponents.length) { | ||
commonPathComponents.length = sourcePathComponents.length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could technically use |
||
} | ||
} | ||
else { | ||
// first file | ||
commonPathComponents = sourcePathComponents; | ||
} | ||
|
||
}); | ||
|
||
return getNormalizedPathFromPathComponents(commonPathComponents); | ||
} | ||
|
||
function checkSourceFilesBelongToPath(soruceFiles: SourceFile[], rootDirectory: string): boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. soruceFiles -> sourceFiles There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why so soruce? |
||
let allFilesBelongToPath = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return allFilesBelongToPath; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just to report all violations instead of just the first one. |
||
if (files) { | ||
let currentDirectory = host.getCurrentDirectory(); | ||
let absoluteRootDirectoryPath = host.getCanonicalFileName(getNormalizedAbsolutePath(rootDirectory, currentDirectory)); | ||
|
||
for (var sourceFile of files) { | ||
if (!isDeclarationFile(sourceFile)) { | ||
let absoluteSourceFilePath = host.getCanonicalFileName(getNormalizedAbsolutePath(sourceFile.fileName, currentDirectory)); | ||
if (absoluteSourceFilePath.indexOf(absoluteRootDirectoryPath) !== 0) { | ||
diagnostics.add(createCompilerDiagnostic(Diagnostics.File_0_is_not_under_rootDir_1_RootDir_is_expected_to_contain_all_source_files, sourceFile.fileName, options.rootDir)); | ||
allFilesBelongToPath = false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return allFilesBelongToPath; | ||
} | ||
|
||
function verifyCompilerOptions() { | ||
if (options.separateCompilation) { | ||
if (options.sourceMap) { | ||
|
@@ -515,41 +575,16 @@ module ts { | |
(options.mapRoot && // there is --mapRoot specified and there would be multiple js files generated | ||
(!options.out || firstExternalModuleSourceFile !== undefined))) { | ||
|
||
let commonPathComponents: string[]; | ||
forEach(files, sourceFile => { | ||
// Each file contributes into common source file path | ||
if (!(sourceFile.flags & NodeFlags.DeclarationFile) | ||
&& !fileExtensionIs(sourceFile.fileName, ".js")) { | ||
let sourcePathComponents = getNormalizedPathComponents(sourceFile.fileName, host.getCurrentDirectory()); | ||
sourcePathComponents.pop(); // FileName is not part of directory | ||
if (commonPathComponents) { | ||
for (let i = 0; i < Math.min(commonPathComponents.length, sourcePathComponents.length); i++) { | ||
if (commonPathComponents[i] !== sourcePathComponents[i]) { | ||
if (i === 0) { | ||
diagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_find_the_common_subdirectory_path_for_the_input_files)); | ||
return; | ||
} | ||
|
||
// New common path found that is 0 -> i-1 | ||
commonPathComponents.length = i; | ||
break; | ||
} | ||
} | ||
|
||
// If the fileComponent path completely matched and less than already found update the length | ||
if (sourcePathComponents.length < commonPathComponents.length) { | ||
commonPathComponents.length = sourcePathComponents.length; | ||
} | ||
} | ||
else { | ||
// first file | ||
commonPathComponents = sourcePathComponents; | ||
} | ||
} | ||
}); | ||
if (options.rootDir && checkSourceFilesBelongToPath(files, options.rootDir)) { | ||
// If a rootDir is specified and is valid use it as the commonSourceDirectory | ||
commonSourceDirectory = getNormalizedAbsolutePath(options.rootDir, host.getCurrentDirectory()); | ||
} | ||
else { | ||
// Compute the commonSourceDirectory from the input files | ||
commonSourceDirectory = computecommonSourceDirectory(files); | ||
} | ||
|
||
commonSourceDirectory = getNormalizedPathFromPathComponents(commonPathComponents); | ||
if (commonSourceDirectory) { | ||
if (commonSourceDirectory && commonSourceDirectory[commonSourceDirectory.length - 1] !== directorySeparator) { | ||
// Make sure directory path ends with directory separator so this string can directly | ||
// used to replace with "" to get the relative path of the source file and the relative path doesn't | ||
// start with / making it rooted path | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use lowercase 'r' for 'RootDir', use single quotes :
"under 'rootDir' '{1}'. 'rootDir' is expected..."