Skip to content

fix(ng-update): do not always use double quotes for generated imports #16131

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

Merged
merged 1 commit into from
May 28, 2019
Merged
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
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { c as c2 } from "@angular/material/c";
import { a } from "@angular/material/a";
import { b } from "@angular/material/b";
import { c } from "@angular/material/c";
import { a as a2 } from "@angular/material/a";
import { c as c2 } from '@angular/material/c';
import { a } from '@angular/material/a';
import { b } from '@angular/material/b';
import { c } from '@angular/material/c';
import { a as a2 } from '@angular/material/a';

// unsorted
import { a } from "@angular/material/a";
import { b } from "@angular/material/b";
import { c } from "@angular/material/c";
import { a } from '@angular/material/a';
import { b } from '@angular/material/b';
import { c } from '@angular/material/c';

import { /* comment */ a as a3 } from "@angular/material/a";
import { /* comment */ a as a3 } from '@angular/material/a';

// primary entry-point export
import { VERSION } from "@angular/material/core";
import { VERSION } from '@angular/material/core';

// type import
import { SomeInterface } from "@angular/material/types";
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ import {/* comment */ a as a3} from '@angular/material';
import {VERSION} from '@angular/material';

// type import
import {SomeInterface} from '@angular/material';
import {SomeInterface} from "@angular/material";
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ function walk(ctx: Lint.WalkContext<boolean>, checker: ts.TypeChecker): void {
return ctx.addFailureAtNode(declaration, Rule.NO_IMPORT_NAMED_SYMBOLS_FAILURE_STR);
}

// Whether the existing import declaration is using a single quote module specifier.
const singleQuoteImport = declaration.moduleSpecifier.getText()[0] === `'`;

// Map which consists of secondary entry-points and import specifiers which are used
// within the current import declaration.
const importMap = new Map<string, ts.ImportSpecifier[]>();
Expand Down Expand Up @@ -144,7 +147,7 @@ function walk(ctx: Lint.WalkContext<boolean>, checker: ts.TypeChecker): void {
const newImport = ts.createImportDeclaration(
undefined, undefined,
ts.createImportClause(undefined, ts.createNamedImports(elements)),
ts.createStringLiteral(`${materialModuleSpecifier}/${name}`));
createStringLiteral(`${materialModuleSpecifier}/${name}`, singleQuoteImport));
return printer.printNode(ts.EmitHint.Unspecified, newImport, sf);
})
.join('\n');
Expand All @@ -166,6 +169,18 @@ function walk(ctx: Lint.WalkContext<boolean>, checker: ts.TypeChecker): void {
sf.statements.forEach(cb);
}

/**
* Creates a string literal from the specified text.
* @param text Text of the string literal.
* @param singleQuotes Whether single quotes should be used when printing the literal node.
*/
function createStringLiteral(text: string, singleQuotes: boolean): ts.StringLiteral {
const literal = ts.createStringLiteral(text);
// See: https://github.com/microsoft/TypeScript/blob/master/src/compiler/utilities.ts#L584-L590
literal['singleQuote'] = singleQuotes;
return literal;
}

/** Gets the symbol that contains the value declaration of the given node. */
function getDeclarationSymbolOfNode(node: ts.Node, checker: ts.TypeChecker): ts.Symbol|undefined {
const symbol = checker.getSymbolAtLocation(node);
Expand Down