Skip to content

fixes #1908 enhancement request #1909

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 6 commits into from
Feb 12, 2015
Merged

fixes #1908 enhancement request #1909

merged 6 commits into from
Feb 12, 2015

Conversation

v3nom
Copy link
Contributor

@v3nom v3nom commented Feb 3, 2015

Extended amd-dependency comment with name attribute, which is used to generate more human-readable JavaScript AMD output. It also simplifies JavaScript AMD module use in TS code. #1908

@msftclas
Copy link

msftclas commented Feb 3, 2015

Hi @v3nom, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!

In order for us to evaluate and accept your PR, we ask that you sign a contribution license agreement. It's all electronic and will take just minutes. I promise there's no faxing. https://cla.microsoft.com.

TTYL, MSBOT;

@msftclas
Copy link

msftclas commented Feb 3, 2015

@v3nom, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR.

Thanks, MSBOT;

@DickvdBrink
Copy link
Contributor

Hmz, weird, some files are now changed to 755. You might want to look at this: link

@@ -4680,7 +4680,7 @@ module ts {
function processReferenceComments(sourceFile: SourceFile): void {
var triviaScanner = createScanner(languageVersion, /*skipTrivia*/false, sourceText);
var referencedFiles: FileReference[] = [];
var amdDependencies: string[] = [];
var amdDependencies: {path:string; name:string}[] = [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: Please put space between :and string

@yuit
Copy link
Contributor

yuit commented Feb 3, 2015

Please add more tests

var amdDependencyRegEx = /^\/\/\/\s*<amd-dependency\s+path\s*=\s*('|")(.+?)\1/gim;
var amdDependencyRegEx = /^\/\/\/\s*<amd-dependency/gim;
var pathRegex = /path\s*=\s*('|")(.+?)\1/gim
var nameRegex = /name\s*=\s*('|")(.+?)\1/gim
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semicolons.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 5, 2015

For the tests, you can look at tests\cases\compiler\amdDependencyComment1.ts as a reference.

@v3nom
Copy link
Contributor Author

v3nom commented Feb 5, 2015

Thanks for comments. I have updated code to match code style. Added /s to regexp and added test cases following existing amdDependencyComment(1/2).ts example

@@ -3925,6 +3925,12 @@ module ts {
write(", ");
emit(imp.name);
});
forEach(node.amdDependencies, amdDependency => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if you have multiple dependencies, ones with names and ones without. this will get the order our of sync, since name is optional and path is not. e.g.:

///<amd-dependency path='module1' />
///<amd-dependency path='module2' name='m2'/>

will emit:

define(["require", "exports", "module1", "module2"], function (require, exports, m2) { }

which is not what you want. so either you reorder them, so that ones with names come first, which we do not do usually as we like to keep emitted code as close as possible to generated code. or you give the other ones unused random names consider using createTempVariable utility in the emitter.

Also worth adding a new test for this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Giving random names would add unnecessary garbage to emitted code. Since import order does not matter for AMD modules, reordering would make most sense.

Changing order is a bit intrusive to original code, but amd-dependency comment is not a language feature, just a utility. Its also not documented in official materials, so I think violating some project principles is justifiable.

var amdDependency = {path: pathMatchResult[2], name: nameMatchResult ? nameMatchResult[2] : undefined };
// AMD dependencies with names have to go first in define header
if (nameMatchResult) {
amdDependencies.unshift(amdDependency);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just sort the list in the emitter? i do not think we will have that many AMD imports that a sort would have a noticeable perf impact. this way the parser always have the truth, and the emitter can chose to do some extra work to ensure a valid transformation.

function sortAMDModules(amdModules: {name: string; path: string}[]) {
// AMD modules with declared variable names goes first
return amdModules.sort((moduleA, moduleB) => {
if (moduleA.name == moduleB.name) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "===" instead of "=="

@mhegazy
Copy link
Contributor

mhegazy commented Feb 10, 2015

Thanks @v3nom. I think we are good to go. just a couple of comments, and if you can merge master into your branch as well. thanks again.

@mhegazy mhegazy merged commit d94cbed into microsoft:master Feb 12, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Feb 12, 2015

thanks!

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants