Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Update Tailor using ES6 features(fixes #102) #109

Merged
merged 16 commits into from
Jan 16, 2017
Merged

Update Tailor using ES6 features(fixes #102) #109

merged 16 commits into from
Jan 16, 2017

Conversation

addityasingh
Copy link
Contributor

  • Use ES6 de-structuring wherever applicable
  • Use multiline string templates using ` `
  • Use () => {} and remove verbose code, wherever applicable
  • Use Array methods like .includes(), wherever applicable
  • Update Tailor to use node-7.x

@@ -1,8 +1,7 @@
sudo: false
language: node_js
node_js:
- '4.4.4'
- '6.0.0'
Copy link
Collaborator

Choose a reason for hiding this comment

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

keep 6 as well.. We shall drop 4 dependency alone.


const stripUrl = fileUrl => path.normalize(fileUrl.replace('file://', ''));
const getPipeAttributes = attributes => {
const { primary, id} = attributes;
Copy link
Collaborator

Choose a reason for hiding this comment

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

space after id for consistency.

fs.readFile(path, 'utf-8', (err, data) => {
if (err) {
reject(new TemplateError(err));
return;
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

else not needed here..

module.exports = function fetchTemplate (templatesPath, baseTemplateFn) {
return (request, parseTemplate) => {
module.exports = (templatesPath, baseTemplateFn) =>
(request, parseTemplate) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

would keep the consistency of keeping parenthesis everywhere.. keeping some function without parenthesis and others with it doesn't look good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am keeping parenthesis only when returning object. Since , this function returns a promise there is no need of keeping it. Thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

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

keeping parentheses for args looks better to me and make you feel its a function.. Its just my thoughts though... If we keep it here,I would also keep it in all arrow functions to be consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

const attribs = node.attribs;
return node.name === 'script' &&
attribs && attribs.type === 'slot';
const { attribs, name} = node;
Copy link
Collaborator

Choose a reason for hiding this comment

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

space after name

Copy link
Collaborator

Choose a reason for hiding this comment

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

not sure if eslint rule for this exist but would be cool to add this rule.

Copy link
Contributor Author

@addityasingh addityasingh Jan 17, 2017

Choose a reason for hiding this comment

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

@vigneshshanmugam We can use http://eslint.org/docs/rules/object-curly-spacing for this but the problem is the existing code is not consistent for any of the never or always rules. It throws 58 and 60 errors respectively with never and always rules. Which one do you think should be used across? I am planning to add this and make it consistent

@@ -24,7 +24,7 @@
"test"
],
"engines": {
"node": ">4.4.3"
"node": ">7.0.0"
Copy link
Collaborator

Choose a reason for hiding this comment

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

6.0.0

const slotNodes = slotMap.get(slot) || [];
const updatedSlotNodes = [...slotNodes, node];
slotMap.set(slot, updatedSlotNodes);
this._pushText(node.next, updatedSlotNodes);
Copy link
Collaborator

Choose a reason for hiding this comment

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

dont think this works though. updatedSlotNodes doesnt have reference to the slotMap and would not update the text nodes.. I think it should be slotMap.get(slot)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think updatedSlotNodes contains the result of line 59-63 which is what is getting used as second parameter of this._pushText. So,

[...slotNodes, node]  ≈  (slotMap.get(attribs.slot).push(node); || slotMap.set(attribs.slot, [node]);)

This is just our understanding of the syntax and seems same in functionality. But, you can confirm if the functionality is broken or not?

Copy link
Collaborator

@vigneshshanmugam vigneshshanmugam Jan 16, 2017

Choose a reason for hiding this comment

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

I am 99% sure its broken, I will confirm you anyways :) I may be wrong as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I am wrong.. Its working as expected.. sorry for that! :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool. I was hoping to not have to change this :) @diogodadalt You were right on this one

Choose a reason for hiding this comment

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

@vigneshshanmugam @Addi90 thanks for taking the time to review it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@diogodadalt No issues man.. I just came to know there should be a test for this as well.. Will add it after this PR has landed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

added f91bcff

@vigneshshanmugam
Copy link
Collaborator

@Addi90 Huge thanks for the PR :)

@vigneshshanmugam
Copy link
Collaborator

/cc @grassator @dazld Thoughts?

@vigneshshanmugam vigneshshanmugam changed the title Update Tailor using ES6 features Update Tailor using ES6 features(fixes #102) Jan 16, 2017
path.join(process.cwd(), 'templates')
),
fragmentTag: 'fragment',
handledTags: [],
handleTag: () => '',
requestFragment,
pipeDefinition: (pipeInstanceName) => pipeChunk(amdLoaderUrl, pipeInstanceName),
pipeDefinition: pipeInstanceName => pipeChunk(amdLoaderUrl, pipeInstanceName),
Copy link
Contributor

Choose a reason for hiding this comment

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

how about pipeChunk.bind(null, amdLoaderUrl) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. We can curry a new function since pipeInstanceName is anyway the second param

@@ -19,8 +19,8 @@ const requiredHeaders = {
* @param {Object} request - HTTP request stream
* @returns {Promise} Response from the fragment server
*/
module.exports = function requestFragment (fragmentUrl, fragmentAttributes, request) {
return new Promise((resolve, reject) => {
module.exports = (fragmentUrl, fragmentAttributes, request) =>
Copy link
Contributor

@dazld dazld Jan 16, 2017

Choose a reason for hiding this comment

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

think this should probably stay as a named function for stack traces.

Copy link
Collaborator

@vigneshshanmugam vigneshshanmugam Jan 16, 2017

Choose a reason for hiding this comment

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

good catch @dazld

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dazld But wouldn't the stack trace use the name from the name of the import. E.g.:

const requestFragment = require('./lib/request-fragment');

I may be wrong but based on my understanding, with any error, the stack trace would use the name of the import i.e. requestFragment.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe you can test it out by throwing error manually?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Irrespective of the syntax of the function, if its the default module function the error thrown is the same for me and defined by the file and line number. E.g. on throwing error in request-fragment.js, the below error is thrown with both the syntaxes:

if (response.statusCode >= 500) {
    reject(new Error('Internal Server Error'));
} else {
    throw new Error('test the stack trace');
    resolve(response);
}

Uncaught Error: test the stack trace
at OverriddenClientRequest.fragmentRequest.on (lib/request-fragment.js:43:23)
at respond (node_modules/nock/lib/request_overrider.js:441:17)
at node_modules/nock/lib/request_overrider.js:479:11
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)

Copy link
Collaborator

Choose a reason for hiding this comment

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

alright then 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have fixed the other comment from @dazld and pushed. Check if it makes sense to merge it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

awesome 👍

@codecov-io
Copy link

codecov-io commented Jan 16, 2017

Current coverage is 91.26% (diff: 90.54%)

No coverage report found for master at 3c39e2e.

Powered by Codecov. Last update 3c39e2e...d87256a

@vigneshshanmugam vigneshshanmugam merged commit 07f07dc into zalando:master Jan 16, 2017
@addityasingh addityasingh deleted the update-es6-features branch January 17, 2017 07:35
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