From c2007ef2a48d841ab6ac94849b766a34ecf2f411 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Tue, 23 May 2017 12:33:54 -0700 Subject: [PATCH] Handle TypeScript decorators (#1678) See this issue for creating the same decorators as babylon ( https://github.com/eslint/typescript-eslint-parser/issues/250 ), in the meantime, this is making a ton of files in vscode crash. So we can just support both :) --- src/printer.js | 30 ++++++++++++++----- .../__snapshots__/jsfmt.spec.js.snap | 22 ++++++++++++++ tests/typescript_decorators/decorators.js | 10 +++++++ tests/typescript_decorators/jsfmt.spec.js | 1 + 4 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 tests/typescript_decorators/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/typescript_decorators/decorators.js create mode 100644 tests/typescript_decorators/jsfmt.spec.js diff --git a/src/printer.js b/src/printer.js index 25891403a..58bd2be68 100644 --- a/src/printer.js +++ b/src/printer.js @@ -87,13 +87,24 @@ function genericPrint(path, options, printPath, args) { // responsible for printing node.decorators. !util.getParentExportDeclaration(path) ) { - const separator = node.decorators.length === 1 && - (node.decorators[0].expression.type === "Identifier" || - node.decorators[0].expression.type === "MemberExpression") - ? " " - : hardline; + let separator = hardline; path.each(decoratorPath => { - parts.push(printPath(decoratorPath), separator); + let prefix = "@"; + let decorator = decoratorPath.getValue(); + if (decorator.expression) { + decorator = decorator.expression; + prefix = ""; + } + + if ( + node.decorators.length === 1 && + (decorator.type === "Identifier" || + decorator.type === "MemberExpression") + ) { + separator = " "; + } + + parts.push(prefix, printPath(decoratorPath), separator); }, "decorators"); } else if ( util.isExportDeclaration(node) && @@ -104,7 +115,12 @@ function genericPrint(path, options, printPath, args) { // that logically apply to node.declaration. path.each( decoratorPath => { - parts.push(printPath(decoratorPath), line); + const decorator = decoratorPath.getValue(); + const prefix = decorator.type === "Decorator" || + decorator.type === "TSDecorator" + ? "" + : "@"; + parts.push(prefix, printPath(decoratorPath), line); }, "declaration", "decorators" diff --git a/tests/typescript_decorators/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_decorators/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..f039eca5a --- /dev/null +++ b/tests/typescript_decorators/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,22 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`decorators.js 1`] = ` +export class TestTextFileService { + constructor( + @ILifecycleService lifecycleService, + ) { + } +} + +@commonEditorContribution +export class TabCompletionController { +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +export class TestTextFileService { + constructor(@ILifecycleService lifecycleService) {} +} + +@commonEditorContribution +export class TabCompletionController {} + +`; diff --git a/tests/typescript_decorators/decorators.js b/tests/typescript_decorators/decorators.js new file mode 100644 index 000000000..f8210c53e --- /dev/null +++ b/tests/typescript_decorators/decorators.js @@ -0,0 +1,10 @@ +export class TestTextFileService { + constructor( + @ILifecycleService lifecycleService, + ) { + } +} + +@commonEditorContribution +export class TabCompletionController { +} diff --git a/tests/typescript_decorators/jsfmt.spec.js b/tests/typescript_decorators/jsfmt.spec.js new file mode 100644 index 000000000..bc085c48c --- /dev/null +++ b/tests/typescript_decorators/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "typescript" });