Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/github_actions/actions/checkout-4
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed Oct 17, 2023
2 parents 6612a95 + e57b6bd commit e121899
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 30 deletions.
6 changes: 6 additions & 0 deletions blueprints/app/files/ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ module.exports = function (defaults) {

<% if (embroider) { %>const { Webpack } = require('@embroider/webpack');
return require('@embroider/compat').compatBuild(app, Webpack, {
staticAddonTestSupportTrees: true,
staticAddonTrees: true,
staticHelpers: true,
staticModifiers: true,
staticComponents: true,
staticEmberSource: true,
skipBabel: [
{
package: 'qunit',
Expand Down
6 changes: 3 additions & 3 deletions blueprints/app/files/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
"<% } %>@ember/optional-features": "^2.0.0",
"@ember/string": "^3.1.1",
"@ember/test-helpers": "^3.2.0<% if (embroider) { %>",
"@embroider/compat": "^3.2.1",
"@embroider/core": "^3.2.1",
"@embroider/webpack": "^3.1.5<% } %>",
"@embroider/compat": "^3.2.3",
"@embroider/core": "^3.3.0",
"@embroider/webpack": "^3.2.0<% } %>",
"@glimmer/component": "^1.1.2",
"@glimmer/tracking": "^1.1.2<% if (typescript) { %>",
"@glint/environment-ember-loose": "^1.1.0",
Expand Down
68 changes: 59 additions & 9 deletions lib/models/blueprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,15 +529,54 @@ let Blueprint = CoreObject.extend({
async convertToJS(fileInfo) {
let rendered = await fileInfo.render();

fileInfo.rendered = await this.removeTypes(path.extname(fileInfo.displayPath), rendered);
fileInfo.displayPath = replaceTypeScriptExtension(fileInfo.displayPath);
fileInfo.outputPath = replaceTypeScriptExtension(fileInfo.outputPath);

return fileInfo;
},

/**
@private
@method removeTypes
@param {string} extension
@param {string} code
@return {Promise}
*/
async removeTypes(extension, code) {
const { removeTypes } = require('remove-types');
const transformed = await removeTypes(rendered);

fileInfo.rendered = transformed;
if (extension === '.gts') {
const { parseTemplates } = require('ember-template-tag');
// Strip template tags
const templateTagIdentifier = (index) => `template = __TEMPLATE_TAG_${index}__;`;
const templateTagIdentifierBraces = (index) => `(template = __TEMPLATE_TAG_${index}__);`;
const templateTagMatches = parseTemplates(code).filter((match) => match.type === 'template-tag');
let strippedCode = code;
for (let i = 0; i < templateTagMatches.length; i++) {
const match = templateTagMatches[i];
const [matchStart, matchEnd] = match.range;
const templateTag = code.substring(matchStart, matchEnd);
strippedCode = strippedCode.replace(templateTag, templateTagIdentifier(i));
}

fileInfo.displayPath = replaceExtension(fileInfo.displayPath, '.js');
fileInfo.outputPath = replaceExtension(fileInfo.outputPath, '.js');
// Remove types
const transformed = await removeTypes(strippedCode);

// Readd stripped template tags
let transformedWithTemplateTag = transformed;
for (let i = 0; i < templateTagMatches.length; i++) {
const match = templateTagMatches[i];
const [matchStart, matchEnd] = match.range;
const templateTag = code.substring(matchStart, matchEnd);
transformedWithTemplateTag = transformedWithTemplateTag.replace(templateTagIdentifier(i), templateTag);
transformedWithTemplateTag = transformedWithTemplateTag.replace(templateTagIdentifierBraces(i), templateTag);
}

return fileInfo;
return transformedWithTemplateTag;
}

return await removeTypes(code);
},

/**
Expand Down Expand Up @@ -893,8 +932,8 @@ let Blueprint = CoreObject.extend({

const jsInfo = new FileInfo({
...info,
outputPath: replaceExtension(info.outputPath, '.js'),
displayPath: replaceExtension(info.displayPath, '.js'),
outputPath: replaceTypeScriptExtension(info.outputPath),
displayPath: replaceTypeScriptExtension(info.displayPath),
});

if (this.options.typescript === false) {
Expand Down Expand Up @@ -1714,12 +1753,23 @@ function replaceExtension(filePath, newExt) {
});
}

function replaceTypeScriptExtension(filePath) {
const extensionMap = {
'.ts': '.js',
'.gts': '.gjs',
};
const ext = path.extname(filePath);
const newExt = extensionMap[ext];

return replaceExtension(filePath, newExt);
}

function isTypeScriptFile(filePath) {
return path.extname(filePath) === '.ts';
return path.extname(filePath) === '.ts' || path.extname(filePath) === '.gts';
}

function isJavaScriptFile(filePath) {
return path.extname(filePath) === '.js';
return path.extname(filePath) === '.js' || path.extname(filePath) === '.gjs';
}

module.exports = Blueprint;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"ember-cli-normalize-entity-name": "^1.0.0",
"ember-cli-preprocess-registry": "^5.0.1",
"ember-cli-string-utils": "^1.1.0",
"ember-template-tag": "^2.3.8",
"ensure-posix-path": "^1.1.1",
"execa": "^5.1.1",
"exit": "^0.1.2",
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/app/embroider-no-welcome/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
"@ember/optional-features": "^2.0.0",
"@ember/string": "^3.1.1",
"@ember/test-helpers": "^3.2.0",
"@embroider/compat": "^3.2.1",
"@embroider/core": "^3.2.1",
"@embroider/webpack": "^3.1.5",
"@embroider/compat": "^3.2.3",
"@embroider/core": "^3.3.0",
"@embroider/webpack": "^3.2.0",
"@glimmer/component": "^1.1.2",
"@glimmer/tracking": "^1.1.2",
"broccoli-asset-rev": "^3.0.0",
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/app/embroider-pnpm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
"@ember/optional-features": "^2.0.0",
"@ember/string": "^3.1.1",
"@ember/test-helpers": "^3.2.0",
"@embroider/compat": "^3.2.1",
"@embroider/core": "^3.2.1",
"@embroider/webpack": "^3.1.5",
"@embroider/compat": "^3.2.3",
"@embroider/core": "^3.3.0",
"@embroider/webpack": "^3.2.0",
"@glimmer/component": "^1.1.2",
"@glimmer/tracking": "^1.1.2",
"broccoli-asset-rev": "^3.0.0",
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/app/embroider-yarn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
"@ember/optional-features": "^2.0.0",
"@ember/string": "^3.1.1",
"@ember/test-helpers": "^3.2.0",
"@embroider/compat": "^3.2.1",
"@embroider/core": "^3.2.1",
"@embroider/webpack": "^3.1.5",
"@embroider/compat": "^3.2.3",
"@embroider/core": "^3.3.0",
"@embroider/webpack": "^3.2.0",
"@glimmer/component": "^1.1.2",
"@glimmer/tracking": "^1.1.2",
"broccoli-asset-rev": "^3.0.0",
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/app/embroider/ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ module.exports = function (defaults) {

const { Webpack } = require('@embroider/webpack');
return require('@embroider/compat').compatBuild(app, Webpack, {
staticAddonTestSupportTrees: true,
staticAddonTrees: true,
staticHelpers: true,
staticModifiers: true,
staticComponents: true,
staticEmberSource: true,
skipBabel: [
{
package: 'qunit',
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/app/embroider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
"@ember/optional-features": "^2.0.0",
"@ember/string": "^3.1.1",
"@ember/test-helpers": "^3.2.0",
"@embroider/compat": "^3.2.1",
"@embroider/core": "^3.2.1",
"@embroider/webpack": "^3.1.5",
"@embroider/compat": "^3.2.3",
"@embroider/core": "^3.3.0",
"@embroider/webpack": "^3.2.0",
"@glimmer/component": "^1.1.2",
"@glimmer/tracking": "^1.1.2",
"broccoli-asset-rev": "^3.0.0",
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/app/typescript-embroider/ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ module.exports = function (defaults) {

const { Webpack } = require('@embroider/webpack');
return require('@embroider/compat').compatBuild(app, Webpack, {
staticAddonTestSupportTrees: true,
staticAddonTrees: true,
staticHelpers: true,
staticModifiers: true,
staticComponents: true,
staticEmberSource: true,
skipBabel: [
{
package: 'qunit',
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/app/typescript-embroider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"@ember/optional-features": "^2.0.0",
"@ember/string": "^3.1.1",
"@ember/test-helpers": "^3.2.0",
"@embroider/compat": "^3.2.1",
"@embroider/core": "^3.2.1",
"@embroider/webpack": "^3.1.5",
"@embroider/compat": "^3.2.3",
"@embroider/core": "^3.3.0",
"@embroider/webpack": "^3.2.0",
"@glimmer/component": "^1.1.2",
"@glimmer/tracking": "^1.1.2",
"@glint/environment-ember-loose": "^1.1.0",
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/models/blueprint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,54 @@ describe('Blueprint', function () {
td.reset();
});

describe('.removeTypes', function () {
it('returns input when passing javascript', async function () {
const output = await Blueprint.prototype.removeTypes('.js', 'const x = 1;\n');
expect(output).to.equal('const x = 1;\n');
});

it('strips types when converting ts', async function () {
const output = await Blueprint.prototype.removeTypes('.ts', 'const x: number = 1;\n');
expect(output).to.equal('const x = 1;\n');
});

it('stripes types when converting gts', async function () {
const output = await Blueprint.prototype.removeTypes(
'.gts',
'const x: number = 1;\n<template>Hello {{x}}!</template>\n'
);
expect(output).to.equal('const x = 1;\n<template>Hello {{x}}!</template>\n');
});

it('can handle template-only gts', async function () {
const output = await Blueprint.prototype.removeTypes('.gts', '<template>Hello!</template>\n');
expect(output).to.equal('<template>Hello!</template>\n');
});

it('can handle multi-line template tag', async function () {
const output = await Blueprint.prototype.removeTypes('.gts', '<template>\nHello!\n</template>\n');
expect(output).to.equal('<template>\nHello!\n</template>\n');
});

it('can handle multiple template tags in one file', async function () {
const output = await Blueprint.prototype.removeTypes(
'.gts',
'const x = <template>Hello!</template>\nconst y = <template>World!</template>\n'
);
expect(output).to.equal('const x = <template>Hello!</template>\nconst y = <template>World!</template>\n');
});

it('works in class body', async function () {
const output = await Blueprint.prototype.removeTypes(
'.gts',
'const foo: number = 1;\nexport default class Bar extends Component {\n <template>Hello {{foo}}</template>\n}\n'
);
expect(output).to.equal(
'const foo = 1;\nexport default class Bar extends Component {\n <template>Hello {{foo}}</template>\n}\n'
);
});
});

describe('.mapFile', function () {
it('replaces all occurrences of __name__ with module name', function () {
let path = Blueprint.prototype.mapFile('__name__/__name__-controller.js', {
Expand Down

0 comments on commit e121899

Please sign in to comment.