Skip to content

Commit

Permalink
fix(core): change require typescript to enstureTypescript
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Feb 23, 2023
1 parent 3f34a67 commit fae661b
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { Tree } from '@nrwl/devkit';
import type { NormalizedSchema } from './normalized-schema';

import { insertImport } from '@nrwl/workspace/src/utilities/ast-utils';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';
import {
addImportToTestBed,
replaceIntoToTestBed,
} from '../../../../utils/nx-devkit/ast-utils';
import type { NormalizedSchema } from './normalized-schema';

let tsModule: typeof import('typescript');

export function updateComponentSpec(host: Tree, options: NormalizedSchema) {
if (!tsModule) {
tsModule = require('typescript');
tsModule = ensureTypescript();
}
if (options.skipTests !== true) {
const componentSpecPath = `${options.appProjectRoot}/src/app/app.component.spec.ts`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Tree } from '@nrwl/devkit';
import { tsquery } from '@phenomnomnominal/tsquery';
import { coerce, lt, major } from 'semver';
import {
getInstalledAngularVersionInfo,
Expand Down Expand Up @@ -36,6 +35,7 @@ export function validateOptions(
if (lt(angularVersionInfo.version, '14.1.0') || ngrxMajorVersion < 15) {
const parentPath = options.parent ?? options.module;
const parentContent = tree.read(parentPath, 'utf-8');
const { tsquery } = require('@phenomnomnominal/tsquery');
const ast = tsquery.ast(parentContent);

const NG_MODULE_DECORATOR_SELECTOR =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Node, SourceFile } from 'typescript';
import { Tree } from 'nx/src/generators/tree';
import { tsquery } from '@phenomnomnominal/tsquery';
import { parse } from 'path';
import { joinPathFragments } from 'nx/src/utils/path';

Expand All @@ -17,6 +16,7 @@ export function convertScamToStandalone(
let newComponentContents = '';
const COMPONENT_PROPERTY_SELECTOR =
'ClassDeclaration > Decorator > CallExpression:has(Identifier[name=Component]) ObjectLiteralExpression';
const { tsquery } = require('@phenomnomnominal/tsquery');
const componentDecoratorMetadataNode = tsquery(
componentAST,
COMPONENT_PROPERTY_SELECTOR,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Tree } from 'nx/src/generators/tree';
import { tsquery } from '@phenomnomnominal/tsquery';

export function getComponentDataFromAST(
tree: Tree,
Expand All @@ -11,6 +10,7 @@ export function getComponentDataFromAST(
'ClassDeclaration:has(Decorator > CallExpression:has(Identifier[name=Component])) > Identifier';

const componentFileContents = tree.read(normalizedComponentPath, 'utf-8');
const { tsquery } = require('@phenomnomnominal/tsquery');
const componentAST = tsquery.ast(componentFileContents);

const componentNode = tsquery(componentAST, COMPONENT_CONTENT_SELECTOR, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { SourceFile } from 'typescript';
import { tsquery } from '@phenomnomnominal/tsquery';
import type { SourceFile } from 'typescript';

export function getModuleMetadataFromAST(
componentAST: SourceFile,
componentFileContents: string
) {
const NGMODULE_CONTENT_SELECTOR =
'ClassDeclaration:has(Decorator > CallExpression:has(Identifier[name=NgModule]))';
const { tsquery } = require('@phenomnomnominal/tsquery');
const moduleNodes = tsquery(componentAST, NGMODULE_CONTENT_SELECTOR, {
visitAllChildren: true,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SourceFile } from 'typescript';
import { tsquery } from '@phenomnomnominal/tsquery';
import type { SourceFile } from 'typescript';

export function selectorExistsInAST(selector: string, ast: SourceFile) {
const { tsquery } = require('@phenomnomnominal/tsquery');
return tsquery(ast, selector, { visitAllChildren: true }).length > 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
updateJson,
} from '@nrwl/devkit';
import type { Schema } from '../schema';
import { tsquery } from '@phenomnomnominal/tsquery';
import type * as ts from 'typescript';
import { ArrayLiteralExpression } from 'typescript';
import { insertImport } from '@nrwl/workspace/src/utilities/ast-utils';
import { addRoute } from '../../../utils/nx-devkit/route-utils';
Expand Down Expand Up @@ -85,6 +83,7 @@ function addRemoteToStaticHost(
}

const hostMFConfig = tree.read(hostMFConfigPath, 'utf-8');
const { tsquery } = require('@phenomnomnominal/tsquery');
const webpackAst = tsquery.ast(hostMFConfig);
const mfRemotesNode = tsquery(
webpackAst,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Tree } from 'nx/src/generators/tree';
import { Schema } from '../schema';
import { readProjectConfiguration } from 'nx/src/generators/utils/project-configuration';
import { generateFiles, joinPathFragments, names } from '@nrwl/devkit';
import * as ts from 'typescript';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';
import { addRoute } from '../../../utils/nx-devkit/route-utils';
import { Schema } from '../schema';

let tsModule: typeof import('typescript');

export function updateHostAppRoutes(tree: Tree, options: Schema) {
if (!tsModule) {
tsModule = ensureTypescript();
}

const { sourceRoot } = readProjectConfiguration(tree, options.appName);

const remoteRoutes =
Expand Down Expand Up @@ -36,10 +42,10 @@ ${remoteRoutes}

const hostRootRoutingFile = tree.read(pathToHostRootRoutingFile, 'utf-8');

let sourceFile = ts.createSourceFile(
let sourceFile = tsModule.createSourceFile(
pathToHostRootRoutingFile,
hostRootRoutingFile,
ts.ScriptTarget.Latest,
tsModule.ScriptTarget.Latest,
true
);

Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/utils/get-mf-projects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Tree } from '@nrwl/devkit';
import { tsquery } from '@phenomnomnominal/tsquery';
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils';

export function getMFProjects(
Expand All @@ -22,6 +21,7 @@ export function getMFProjects(
return;
}
const webpackConfig = tree.read(webpackPath, 'utf-8');
const { tsquery } = require('@phenomnomnominal/tsquery');
const ast = tsquery.ast(webpackConfig);
const moduleFederationWebpackConfig = tsquery(
ast,
Expand Down
3 changes: 1 addition & 2 deletions packages/angular/src/utils/nx-devkit/route-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ export function addRoute(
);
}
if (!tsModule) {
tsModule = require('typescript');
tsModule = ensureTypescript();
}
ensureTypescript();
const { tsquery } = require('@phenomnomnominal/tsquery');

let routesFileContents = tree.read(routesFile, 'utf-8');
Expand Down
8 changes: 7 additions & 1 deletion packages/devkit/src/generators/to-js.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import type { Tree } from 'nx/src/generators/tree';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { typescriptVersion } from 'nx/src/utils/versions';
import { ensurePackage } from '../utils/package-json';

/**
* Rename and transpile any new typescript files created to javascript files
*/
export function toJS(tree: Tree): void {
const { JsxEmit, ScriptTarget, transpile } = require('typescript');
const { JsxEmit, ScriptTarget, transpile } = ensurePackage(
'typescript',
typescriptVersion
);

for (const c of tree.listChanges()) {
if (
Expand Down
1 change: 0 additions & 1 deletion packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
names,
offsetFromRoot,
ProjectConfiguration,
readJson,
toJS,
Tree,
updateJson,
Expand Down
10 changes: 7 additions & 3 deletions packages/react-native/src/generators/component/component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as ts from 'typescript';
import { Schema } from './schema';
import {
applyChangesToString,
Expand All @@ -12,6 +11,7 @@ import {
} from '@nrwl/devkit';
import { NormalizedSchema, normalizeOptions } from './lib/normalize-options';
import { addImport } from './lib/add-import';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';

export async function reactNativeComponentGenerator(
host: Tree,
Expand Down Expand Up @@ -53,7 +53,11 @@ function createComponentFiles(host: Tree, options: NormalizedSchema) {
}
}

let tsModule: typeof import('typescript');
function addExportsToBarrel(host: Tree, options: NormalizedSchema) {
if (!tsModule) {
tsModule = ensureTypescript();
}
const workspace = getProjects(host);
const isApp = workspace.get(options.project).projectType === 'application';

Expand All @@ -64,10 +68,10 @@ function addExportsToBarrel(host: Tree, options: NormalizedSchema) {
);
const indexSource = host.read(indexFilePath, 'utf-8');
if (indexSource !== null) {
const indexSourceFile = ts.createSourceFile(
const indexSourceFile = tsModule.createSourceFile(
indexFilePath,
indexSource,
ts.ScriptTarget.Latest,
tsModule.ScriptTarget.Latest,
true
);
const changes = applyChangesToString(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { findNodes } from 'nx/src/utils/typescript';
import * as ts from 'typescript';
import type * as ts from 'typescript';
import { ChangeType, StringChange } from '@nrwl/devkit';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';

let tsModule: typeof import('typescript');
export function addImport(
source: ts.SourceFile,
statement: string
): StringChange[] {
const allImports = findNodes(source, ts.SyntaxKind.ImportDeclaration);
if (!tsModule) {
tsModule = ensureTypescript();
}
const allImports = findNodes(source, tsModule.SyntaxKind.ImportDeclaration);
if (allImports.length > 0) {
const lastImport = allImports[allImports.length - 1];
return [
Expand Down

0 comments on commit fae661b

Please sign in to comment.