Skip to content

Commit

Permalink
Merge pull request #6 from rwjblue/add-prettier
Browse files Browse the repository at this point in the history
Prettier!
  • Loading branch information
rwjblue authored Jun 3, 2018
2 parents 8b8e2f3 + b2fe546 commit dcd71be
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 182 deletions.
7 changes: 5 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ module.exports = {
sourceType: 'module'
},
plugins: [
'ember'
'ember',
'prettier',
],
extends: [
'eslint:recommended',
'plugin:ember/recommended'
'plugin:ember/recommended',
'prettier',
],
env: {
browser: true
},
rules: {
'prettier/prettier': 'error',
},
overrides: [
// node files
Expand Down
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"trailingComma": "es5",
"printWidth": 100
}
48 changes: 24 additions & 24 deletions config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,66 @@ module.exports = function() {
return Promise.all([
getChannelURL('release'),
getChannelURL('beta'),
getChannelURL('canary')
]).then((urls) => {
getChannelURL('canary'),
]).then(urls => {
return {
useYarn: true,
scenarios: [
{
name: 'ember-lts-2.12',
npm: {
devDependencies: {
'ember-source': '~2.12.0'
}
}
'ember-source': '~2.12.0',
},
},
},
{
name: 'ember-lts-2.16',
npm: {
devDependencies: {
'ember-source': '~2.16.0'
}
}
'ember-source': '~2.16.0',
},
},
},
{
name: 'ember-lts-2.18',
npm: {
devDependencies: {
'ember-source': '~2.18.0'
}
}
'ember-source': '~2.18.0',
},
},
},
{
name: 'ember-release',
npm: {
devDependencies: {
'ember-source': urls[0]
}
}
'ember-source': urls[0],
},
},
},
{
name: 'ember-beta',
npm: {
devDependencies: {
'ember-source': urls[1]
}
}
'ember-source': urls[1],
},
},
},
{
name: 'ember-canary',
npm: {
devDependencies: {
'ember-source': urls[2]
}
}
'ember-source': urls[2],
},
},
},
{
name: 'ember-default',
npm: {
devDependencies: {}
}
}
]
devDependencies: {},
},
},
],
};
});
};
2 changes: 1 addition & 1 deletion config/environment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

module.exports = function(/* environment, appConfig */) {
return { };
return {};
};
20 changes: 12 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,32 @@ module.exports = {
plugin: require('./lib/angle-bracket-invocation-polyfill'),
baseDir() {
return __dirname;
}
},
});
}
},

included() {
this._super.included.apply(this, arguments);

if (!this.shouldPolyfill) { return; }
if (!this.shouldPolyfill) {
return;
}

this.import("vendor/angle-bracket-invocation-polyfill/index.js");
this.import('vendor/angle-bracket-invocation-polyfill/index.js');
},

treeForVendor(rawVendorTree) {
if (!this.shouldPolyfill) { return; }
if (!this.shouldPolyfill) {
return;
}

let babelAddon = this.addons.find(addon => addon.name === "ember-cli-babel");
let babelAddon = this.addons.find(addon => addon.name === 'ember-cli-babel');

let transpiledVendorTree = babelAddon.transpileTree(rawVendorTree, {
"ember-cli-babel": {
compileModules: false
}
'ember-cli-babel': {
compileModules: false,
},
});

return transpiledVendorTree;
Expand Down
115 changes: 63 additions & 52 deletions lib/angle-bracket-invocation-polyfill.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use strict";
'use strict';

const reLines = /(.*?(?:\r\n?|\n|$))/gm;
const ALPHA = /[A-Za-z]/;
Expand All @@ -17,7 +17,7 @@ class AngleBracketPolyfill {
// in order to debug in https://astexplorer.net/#/gist/5e923e7322de5052a26a5a292f8c3995/
// **** copy from here ****
function dasherize(string) {
return string.replace(/[A-Z]/g, function (char, index) {
return string.replace(/[A-Z]/g, function(char, index) {
return (index !== 0 ? '-' : '') + char.toLowerCase();
});
}
Expand All @@ -43,38 +43,42 @@ class AngleBracketPolyfill {

// politely lifted from https://github.com/glimmerjs/glimmer-vm/blob/v0.35.0/packages/%40glimmer/syntax/lib/parser.ts#L113-L149
function sourceForNode(node) {
let firstLine = node.loc.start.line - 1;
let currentLine = firstLine - 1;
let firstColumn = node.loc.start.column;
let string = [];
let line;

let lastLine = node.loc.end.line - 1;
let lastColumn = node.loc.end.column;

while (currentLine < lastLine) {
currentLine++;
line = sourceLines[currentLine];

if (currentLine === firstLine) {
if (firstLine === lastLine) {
string.push(line.slice(firstColumn, lastColumn));
} else {
string.push(line.slice(firstColumn));
}
} else if (currentLine === lastLine) {
string.push(line.slice(0, lastColumn));
let firstLine = node.loc.start.line - 1;
let currentLine = firstLine - 1;
let firstColumn = node.loc.start.column;
let string = [];
let line;

let lastLine = node.loc.end.line - 1;
let lastColumn = node.loc.end.column;

while (currentLine < lastLine) {
currentLine++;
line = sourceLines[currentLine];

if (currentLine === firstLine) {
if (firstLine === lastLine) {
string.push(line.slice(firstColumn, lastColumn));
} else {
string.push(line);
string.push(line.slice(firstColumn));
}
} else if (currentLine === lastLine) {
string.push(line.slice(0, lastColumn));
} else {
string.push(line);
}
}

return string.join('\n');
return string.join('\n');
}

function getSelfClosing(element) {
if ('selfClosing' in element) { return element.selfClosing; }
if (!hasSourceAvailable) { return false; }
if ('selfClosing' in element) {
return element.selfClosing;
}
if (!hasSourceAvailable) {
return false;
}

let nodeSource = sourceForNode(element);
let firstClosingBracketIndex = nodeSource.indexOf('>');
Expand All @@ -91,16 +95,18 @@ class AngleBracketPolyfill {
*/
function getTag(element) {
// if we have no source, we must use whatever element.tag has
if (!hasSourceAvailable) { return element.tag; }
if (!hasSourceAvailable) {
return element.tag;
}

let nodeSource = sourceForNode(element);

let firstChar;
for (let i = 0; i < nodeSource.length; i++) {
let char = nodeSource[i];
if (char == '@' || ALPHA.test(char)) {
firstChar = char;
break;
firstChar = char;
break;
}
}

Expand All @@ -115,21 +121,23 @@ class AngleBracketPolyfill {
let [maybeLocal] = tag.split('.');

let isLocal = locals.indexOf(maybeLocal) !== -1;
let isUpperCase = invocationFirstChar === invocationFirstChar.toUpperCase() && invocationFirstChar !== invocationFirstChar.toLowerCase();
let isUpperCase =
invocationFirstChar === invocationFirstChar.toUpperCase() &&
invocationFirstChar !== invocationFirstChar.toLowerCase();
let selfClosing = getSelfClosing(element);

if (isLocal || isNamedArgument || isThisPath) {
return {
kind: 'DynamicComponent',
path: b.path(tag),
selfClosing
}
selfClosing,
};
} else if (isUpperCase) {
return {
kind: 'StaticComponent',
componentName: dasherize(tag),
selfClosing,
}
};
} else {
return { kind: 'Element' };
}
Expand All @@ -146,26 +154,26 @@ class AngleBracketPolyfill {
for (let i = 0; i < node.blockParams.length; i++) {
locals.pop();
}
}
},
},

ElementNode(node) {
let invocation = getInvocationDetails(node);

if (invocation.kind === 'Element') { return; }
if (invocation.kind === 'Element') {
return;
}

let { children, blockParams } = node;

let attributes = node.attributes.filter(node => node.name[0] !== '@');
let args = node.attributes.filter(node => node.name[0] === '@');

let hash = b.hash(args.map(arg =>
b.pair(
arg.name.slice(1),
expressionForAttributeValue(arg.value),
arg.loc
let hash = b.hash(
args.map(arg =>
b.pair(arg.name.slice(1), expressionForAttributeValue(arg.value), arg.loc)
)
));
);

if (attributes.length > 0) {
hash.pairs.push(
Expand All @@ -174,9 +182,11 @@ class AngleBracketPolyfill {
b.sexpr(
'hash',
[],
b.hash(attributes.map(attr =>
b.pair(attr.name, expressionForAttributeValue(attr.value), attr.loc)
))
b.hash(
attributes.map(attr =>
b.pair(attr.name, expressionForAttributeValue(attr.value), attr.loc)
)
)
)
)
);
Expand All @@ -186,17 +196,18 @@ class AngleBracketPolyfill {
if (invocation.selfClosing === true) {
return b.mustache(invocation.componentName, null, hash, false, node.loc);
} else {
return b.block(invocation.componentName, null, hash, b.program(children, blockParams), null, node.loc);
}
} else {
if (invocation.selfClosing === true) {
return b.mustache(
'component',
[invocation.path],
return b.block(
invocation.componentName,
null,
hash,
b.program(children, blockParams),
null,
node.loc
);
}
} else {
if (invocation.selfClosing === true) {
return b.mustache('component', [invocation.path], hash, null, node.loc);
} else {
return b.block(
'component',
Expand Down
2 changes: 1 addition & 1 deletion lib/sample-compile-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ compiler.registerPlugin('ast', require('./angle-bracket-invocation-polyfill'));

let template = '<FooBar />';
let output = compiler.precompile(template, { contents: template });
console.log(output); // eslint-disable-line no-console
console.log(output); // eslint-disable-line no-console
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@
"ember-source": "~3.2.0-beta.2",
"ember-source-channel-url": "^1.0.1",
"ember-try": "^0.2.23",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-ember": "^5.0.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-prettier": "^2.6.0",
"loader.js": "^4.2.3",
"prettier": "^1.13.4",
"qunit-dom": "^0.6.2"
},
"engines": {
Expand Down
Loading

0 comments on commit dcd71be

Please sign in to comment.