Skip to content
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

WIP: Decorator Implementation #17226

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion broccoli/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const WriteFile = require('broccoli-file-creator');
const StringReplace = require('broccoli-string-replace');
const GlimmerTemplatePrecompiler = require('./glimmer-template-compiler');
const VERSION_PLACEHOLDER = /VERSION_STRING_PLACEHOLDER/g;
const transfromBabelPlugins = require('./transforms/transform-babel-plugins');

const debugTree = BroccoliDebug.buildDebugCallback('ember-source');

Expand Down Expand Up @@ -83,6 +84,13 @@ module.exports.getPackagesES = function getPackagesES() {
exclude: ['**/*.ts'],
});

// tsc / typescript handles decorators and class properties on its own
// so for non ts, transpile the proposal features (decorators, etc)
let transpiledProposals = debugTree(
transfromBabelPlugins(debugTree(nonTypeScriptContents, `get-packages-es:babel-plugins:input`)),
`get-packages-es:babel-plugins:output`
);

let typescriptContents = new Funnel(debuggedCompiledTemplatesAndTypeScript, {
include: ['**/*.ts'],
});
Expand All @@ -95,7 +103,7 @@ module.exports.getPackagesES = function getPackagesES() {

let debuggedCompiledTypescript = debugTree(typescriptCompiled, `get-packages-es:ts:output`);

let mergedFinalOutput = new MergeTrees([nonTypeScriptContents, debuggedCompiledTypescript], {
let mergedFinalOutput = new MergeTrees([transpiledProposals, debuggedCompiledTypescript], {
overwrite: true,
});

Expand Down
13 changes: 13 additions & 0 deletions broccoli/transforms/transform-babel-plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Babel = require('broccoli-babel-transpiler');

module.exports = function(tree) {
let options = {
sourceMaps: true,
plugins: [
['@babel/plugin-proposal-decorators', { decoratorsBeforeExport: true, legacy: false }],
['@babel/plugin-proposal-class-properties'],
],
};

return new Babel(tree, options);
};
11 changes: 11 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2016",
"experimentalDecorators": true
},
"exclude": [
"node_modules",
"**/node_modules/*"
]
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
"@babel/plugin-transform-arrow-functions": "^7.2.0",
"@babel/plugin-transform-block-scoping": "^7.2.0",
"@babel/plugin-transform-classes": "^7.2.2",
"@babel/plugin-proposal-class-properties": "^7.2.1",
"@babel/plugin-proposal-decorators": "^7.2.0",
"@babel/plugin-transform-computed-properties": "^7.2.0",
"@babel/plugin-transform-destructuring": "^7.2.0",
"@babel/plugin-transform-literals": "^7.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// import { NEEDS_STAGE_1_DECORATORS } from 'ember-decorators-flags';

// let isStage1FieldDescriptor;

// if (NEEDS_STAGE_1_DECORATORS) {
// isStage1FieldDescriptor = function isStage1FieldDescriptor(possibleDesc) {
// if (possibleDesc.length === 3) {
// let [target, key, desc] = possibleDesc;

// return (
// typeof target === 'object' &&
// target !== null &&
// typeof key === 'string' &&
// ((typeof desc === 'object' &&
// desc !== null &&
// 'enumerable' in desc &&
// 'configurable' in desc) ||
// desc === undefined) // TS compatibility
// );
// } else if (possibleDesc.length === 1) {
// let [target] = possibleDesc;

// return typeof target === 'function' && 'prototype' in target;
// }

// return false;
// }
// }

export function isFieldDescriptor(possibleDesc) {
let isDescriptor = isStage2FieldDescriptor(possibleDesc);

// if (NEEDS_STAGE_1_DECORATORS) {
// isDescriptor = isDescriptor || isStage1FieldDescriptor(possibleDesc);
// }

return isDescriptor;
}



export function isStage2FieldDescriptor(possibleDesc) {
return possibleDesc && possibleDesc.toString() === '[object Descriptor]';
}
141 changes: 141 additions & 0 deletions packages/@ember/-internals/metal/lib/-private/decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { assert } from '@ember/debug';

// import { NEEDS_STAGE_1_DECORATORS } from 'ember-decorators-flags';
// import { deprecate } from '@ember/application/deprecations';

import { isFieldDescriptor, isStage2FieldDescriptor } from './class-field-descriptor';

function kindForDesc(desc) {
if ('value' in desc && desc.enumerable === true) {
return 'field';
} else {
return 'method';
}
}

function placementForKind(kind) {
return kind === 'method' ? 'prototype' : 'own';
}

function convertStage1ToStage2(desc) {
if (desc.length === 3) {
// Class element decorator
let [, key, descriptor] = desc;

let kind = kindForDesc(desc);
let placement = placementForKind(kind);

let initializer = descriptor !== undefined ? descriptor.initializer : undefined;

return {
descriptor,
key,
kind,
placement,
initializer,
toString: () => '[object Descriptor]',
};
} else {
// Class decorator
return {
kind: 'class',
elements: [],
};
}
}

export function decorator(fn) {
// if (NEEDS_STAGE_1_DECORATORS) {
// return function(...params) {
// if (isStage2FieldDescriptor(params)) {
// let desc = params[0];

// return deprecateDirectDescriptorMutation(fn, desc);
// } else {
// let desc = convertStage1ToStage2(params);

// desc = deprecateDirectDescriptorMutation(fn, desc);

// if (typeof desc.finisher === 'function') {
// // Finishers are supposed to run at the end of class finalization,
// // but we don't get that with stage 1 transforms. We have to be careful
// // to make sure that we aren't doing any operations which would change
// // due to timing.
// let [target] = params;

// desc.finisher(target.prototype ? target : target.constructor);
// }

// if (typeof desc.initializer === 'function') {
// // Babel 6 / the legacy decorator transform needs the initializer back
// // on the property descriptor/ In case the user has set a new
// // initializer on the member descriptor, we transfer it back to
// // original descriptor.
// desc.descriptor.initializer = desc.initializer;
// }

// return desc.descriptor;
// }
// };
// } else {
return fn;
// }
}

/**
* A macro that takes a decorator function and allows it to optionally
* receive parameters
*
* ```js
* let foo = decoratorWithParams((target, desc, key, params) => {
* console.log(params);
* });
*
* class {
* @foo bar; // undefined
* @foo('bar') baz; // ['bar']
* }
* ```
*
* @param {Function} fn - decorator function
*/
export function decoratorWithParams(fn) {
return function(...params) {
// determine if user called as @computed('blah', 'blah') or @computed
if (isFieldDescriptor(params)) {
return decorator(fn)(...params);
} else {
return decorator(desc => fn(desc, params));
}
};
}

/**
* A macro that takes a decorator function and requires it to receive
* parameters:
*
* ```js
* let foo = decoratorWithRequiredParams((target, desc, key, params) => {
* console.log(params);
* });
*
* class {
* @foo('bar') baz; // ['bar']
* @foo bar; // Error
* }
* ```
*
* @param {Function} fn - decorator function
*/
export function decoratorWithRequiredParams(fn, name) {
return function(...params) {
assert(
`The @${name || fn.name} decorator requires parameters`,
!isFieldDescriptor(params) && params.length > 0
);

return decorator(desc => {
return fn(desc, params);
});
};
}
Loading