@wixc3/typescript
Advanced tools
Comparing version 7.0.3 to 7.1.0
@@ -7,10 +7,2 @@ import * as ts from 'typescript'; | ||
export declare function compileCode(code: string, fakePath?: string): ts.SourceFile; | ||
/** | ||
* Compiles a single file to typescript AST | ||
*/ | ||
export declare function compileFile(sourceFilePath: string): ts.SourceFile | undefined; | ||
/** | ||
* Calculates the effective tsconfig compiler options | ||
*/ | ||
export declare function getTsConfigCompilerOptions(tsConfigJsonPath?: string): ts.CompilerOptions; | ||
//# sourceMappingURL=compile.d.ts.map |
@@ -26,6 +26,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getTsConfigCompilerOptions = exports.compileFile = exports.compileCode = void 0; | ||
exports.compileCode = void 0; | ||
/* eslint-disable @typescript-eslint/unbound-method */ | ||
const common_1 = require("@wixc3/common"); | ||
const path_1 = require("path"); | ||
const ts = __importStar(require("typescript")); | ||
@@ -37,53 +35,5 @@ /** | ||
function compileCode(code, fakePath = 'index.ts') { | ||
const configFile = ts.findConfigFile(fakePath, ts.sys.fileExists.bind(ts.sys)); | ||
const options = getTsConfigCompilerOptions(configFile); | ||
const host = ts.createCompilerHost(options, true); | ||
host.readFile = (path) => { | ||
if (path === fakePath) { | ||
return code; | ||
} | ||
else { | ||
return ''; | ||
} | ||
}; | ||
const program = ts.createProgram({ | ||
rootNames: [fakePath], | ||
options, | ||
host, | ||
}); | ||
return program.getSourceFile(fakePath); | ||
return ts.createSourceFile(fakePath, code, ts.ScriptTarget.Latest, true); | ||
} | ||
exports.compileCode = compileCode; | ||
/** | ||
* Compiles a single file to typescript AST | ||
*/ | ||
function compileFile(sourceFilePath) { | ||
const configFile = ts.findConfigFile(sourceFilePath, ts.sys.fileExists.bind(ts.sys)); | ||
const options = getTsConfigCompilerOptions(configFile); | ||
const host = ts.createCompilerHost(options, true); | ||
const program = ts.createProgram({ | ||
rootNames: [sourceFilePath], | ||
options, | ||
host, | ||
}); | ||
return program.getSourceFile(sourceFilePath); | ||
} | ||
exports.compileFile = compileFile; | ||
/** | ||
* Calculates the effective tsconfig compiler options | ||
*/ | ||
function getTsConfigCompilerOptions(tsConfigJsonPath) { | ||
if (tsConfigJsonPath) { | ||
const { config } = ts.readConfigFile(tsConfigJsonPath, ts.sys.readFile); | ||
const { options } = ts.parseJsonConfigFileContent(config, ts.sys, '.'); | ||
const ext = (0, common_1.isString)(config.extends) ? (0, path_1.join)((0, path_1.dirname)(tsConfigJsonPath), config.extends) : undefined; | ||
return { | ||
...getTsConfigCompilerOptions(ext), | ||
...options, | ||
}; | ||
} | ||
else | ||
return {}; | ||
} | ||
exports.getTsConfigCompilerOptions = getTsConfigCompilerOptions; | ||
//# sourceMappingURL=compile.js.map |
@@ -14,5 +14,10 @@ "use strict"; | ||
const findInNodes = (nodes) => nodes.reduce((found, n) => (0, common_1.concat)(found, findAllNodes(n, predicate)), []); | ||
if (source instanceof Array) { | ||
if (Array.isArray(source)) { | ||
return findInNodes(source); | ||
} | ||
else { | ||
if (!source.parent && !typescript_1.default.isSourceFile(source)) { | ||
throw new Error('AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile'); | ||
} | ||
} | ||
if (typescript_1.default.isSourceFile(source) || typescript_1.default.isBlock(source)) { | ||
@@ -35,2 +40,7 @@ return findInNodes(source.statements); | ||
} | ||
else { | ||
if (!source.parent) { | ||
throw new Error('AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile'); | ||
} | ||
} | ||
return predicate(source) ? source : findInNodes(source.getChildren()); | ||
@@ -37,0 +47,0 @@ } |
@@ -1,3 +0,3 @@ | ||
import type * as ts from 'typescript'; | ||
import type { Predicate } from '@wixc3/common'; | ||
import * as ts from 'typescript'; | ||
/** | ||
@@ -4,0 +4,0 @@ * Finds a node matching the pattern. |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isSame = exports.match = void 0; | ||
const ts = __importStar(require("typescript")); | ||
const compile_1 = require("./compile"); | ||
@@ -51,2 +75,8 @@ const find_1 = require("./find"); | ||
} | ||
if (a && !(a === null || a === void 0 ? void 0 : a.parent) && !ts.isSourceFile(a)) { | ||
throw new Error('AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile'); | ||
} | ||
if (b && !(b === null || b === void 0 ? void 0 : b.parent) && !ts.isSourceFile(b)) { | ||
throw new Error('AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile'); | ||
} | ||
if (a && b && a.kind === b.kind) { | ||
@@ -53,0 +83,0 @@ const aChildren = a.getChildren(); |
@@ -7,10 +7,2 @@ import * as ts from 'typescript'; | ||
export declare function compileCode(code: string, fakePath?: string): ts.SourceFile; | ||
/** | ||
* Compiles a single file to typescript AST | ||
*/ | ||
export declare function compileFile(sourceFilePath: string): ts.SourceFile | undefined; | ||
/** | ||
* Calculates the effective tsconfig compiler options | ||
*/ | ||
export declare function getTsConfigCompilerOptions(tsConfigJsonPath?: string): ts.CompilerOptions; | ||
//# sourceMappingURL=compile.d.ts.map |
/* eslint-disable @typescript-eslint/unbound-method */ | ||
import { isString } from '@wixc3/common'; | ||
import { dirname, join } from 'path'; | ||
import * as ts from 'typescript'; | ||
@@ -10,50 +8,4 @@ /** | ||
export function compileCode(code, fakePath = 'index.ts') { | ||
const configFile = ts.findConfigFile(fakePath, ts.sys.fileExists.bind(ts.sys)); | ||
const options = getTsConfigCompilerOptions(configFile); | ||
const host = ts.createCompilerHost(options, true); | ||
host.readFile = (path) => { | ||
if (path === fakePath) { | ||
return code; | ||
} | ||
else { | ||
return ''; | ||
} | ||
}; | ||
const program = ts.createProgram({ | ||
rootNames: [fakePath], | ||
options, | ||
host, | ||
}); | ||
return program.getSourceFile(fakePath); | ||
return ts.createSourceFile(fakePath, code, ts.ScriptTarget.Latest, true); | ||
} | ||
/** | ||
* Compiles a single file to typescript AST | ||
*/ | ||
export function compileFile(sourceFilePath) { | ||
const configFile = ts.findConfigFile(sourceFilePath, ts.sys.fileExists.bind(ts.sys)); | ||
const options = getTsConfigCompilerOptions(configFile); | ||
const host = ts.createCompilerHost(options, true); | ||
const program = ts.createProgram({ | ||
rootNames: [sourceFilePath], | ||
options, | ||
host, | ||
}); | ||
return program.getSourceFile(sourceFilePath); | ||
} | ||
/** | ||
* Calculates the effective tsconfig compiler options | ||
*/ | ||
export function getTsConfigCompilerOptions(tsConfigJsonPath) { | ||
if (tsConfigJsonPath) { | ||
const { config } = ts.readConfigFile(tsConfigJsonPath, ts.sys.readFile); | ||
const { options } = ts.parseJsonConfigFileContent(config, ts.sys, '.'); | ||
const ext = isString(config.extends) ? join(dirname(tsConfigJsonPath), config.extends) : undefined; | ||
return { | ||
...getTsConfigCompilerOptions(ext), | ||
...options, | ||
}; | ||
} | ||
else | ||
return {}; | ||
} | ||
//# sourceMappingURL=compile.js.map |
@@ -8,5 +8,10 @@ import { chain, concat, find, isString, map, noWhiteSpace } from '@wixc3/common'; | ||
const findInNodes = (nodes) => nodes.reduce((found, n) => concat(found, findAllNodes(n, predicate)), []); | ||
if (source instanceof Array) { | ||
if (Array.isArray(source)) { | ||
return findInNodes(source); | ||
} | ||
else { | ||
if (!source.parent && !ts.isSourceFile(source)) { | ||
throw new Error('AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile'); | ||
} | ||
} | ||
if (ts.isSourceFile(source) || ts.isBlock(source)) { | ||
@@ -28,2 +33,7 @@ return findInNodes(source.statements); | ||
} | ||
else { | ||
if (!source.parent) { | ||
throw new Error('AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile'); | ||
} | ||
} | ||
return predicate(source) ? source : findInNodes(source.getChildren()); | ||
@@ -30,0 +40,0 @@ } |
@@ -1,3 +0,3 @@ | ||
import type * as ts from 'typescript'; | ||
import type { Predicate } from '@wixc3/common'; | ||
import * as ts from 'typescript'; | ||
/** | ||
@@ -4,0 +4,0 @@ * Finds a node matching the pattern. |
@@ -0,1 +1,2 @@ | ||
import * as ts from 'typescript'; | ||
import { compileCode } from './compile'; | ||
@@ -47,2 +48,8 @@ import { findNode, findNodeAfterComment } from './find'; | ||
} | ||
if (a && !(a === null || a === void 0 ? void 0 : a.parent) && !ts.isSourceFile(a)) { | ||
throw new Error('AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile'); | ||
} | ||
if (b && !(b === null || b === void 0 ? void 0 : b.parent) && !ts.isSourceFile(b)) { | ||
throw new Error('AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile'); | ||
} | ||
if (a && b && a.kind === b.kind) { | ||
@@ -49,0 +56,0 @@ const aChildren = a.getChildren(); |
{ | ||
"name": "@wixc3/typescript", | ||
"version": "7.0.3", | ||
"version": "7.1.0", | ||
"description": "API ", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
/* eslint-disable @typescript-eslint/unbound-method */ | ||
import { isString } from '@wixc3/common'; | ||
import { dirname, join } from 'path'; | ||
import * as ts from 'typescript'; | ||
@@ -11,49 +9,3 @@ | ||
export function compileCode(code: string, fakePath = 'index.ts') { | ||
const configFile = ts.findConfigFile(fakePath, ts.sys.fileExists.bind(ts.sys)); | ||
const options = getTsConfigCompilerOptions(configFile); | ||
const host = ts.createCompilerHost(options, true); | ||
host.readFile = (path) => { | ||
if (path === fakePath) { | ||
return code; | ||
} else { | ||
return ''; | ||
} | ||
}; | ||
const program = ts.createProgram({ | ||
rootNames: [fakePath], | ||
options, | ||
host, | ||
}); | ||
return program.getSourceFile(fakePath)!; | ||
return ts.createSourceFile(fakePath, code, ts.ScriptTarget.Latest, true); | ||
} | ||
/** | ||
* Compiles a single file to typescript AST | ||
*/ | ||
export function compileFile(sourceFilePath: string) { | ||
const configFile = ts.findConfigFile(sourceFilePath, ts.sys.fileExists.bind(ts.sys)); | ||
const options = getTsConfigCompilerOptions(configFile); | ||
const host = ts.createCompilerHost(options, true); | ||
const program = ts.createProgram({ | ||
rootNames: [sourceFilePath], | ||
options, | ||
host, | ||
}); | ||
return program.getSourceFile(sourceFilePath); | ||
} | ||
/** | ||
* Calculates the effective tsconfig compiler options | ||
*/ | ||
export function getTsConfigCompilerOptions(tsConfigJsonPath?: string): ts.CompilerOptions { | ||
if (tsConfigJsonPath) { | ||
const { config } = ts.readConfigFile(tsConfigJsonPath, ts.sys.readFile) as { config: { extends?: string } }; | ||
const { options } = ts.parseJsonConfigFileContent(config, ts.sys, '.'); | ||
const ext = isString(config.extends) ? join(dirname(tsConfigJsonPath), config.extends) : undefined; | ||
return { | ||
...getTsConfigCompilerOptions(ext), | ||
...options, | ||
}; | ||
} else return {}; | ||
} |
@@ -14,4 +14,10 @@ import { chain, concat, find, isString, map, noWhiteSpace, Predicate } from '@wixc3/common'; | ||
if (source instanceof Array) { | ||
if (Array.isArray(source)) { | ||
return findInNodes(source); | ||
} else { | ||
if (!source.parent && !ts.isSourceFile(source)) { | ||
throw new Error( | ||
'AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile' | ||
); | ||
} | ||
} | ||
@@ -40,2 +46,8 @@ if (ts.isSourceFile(source) || ts.isBlock(source)) { | ||
return findInNodes(source.statements); | ||
} else { | ||
if (!source.parent) { | ||
throw new Error( | ||
'AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile' | ||
); | ||
} | ||
} | ||
@@ -42,0 +54,0 @@ |
@@ -1,3 +0,3 @@ | ||
import type * as ts from 'typescript'; | ||
import type { Predicate } from '@wixc3/common'; | ||
import * as ts from 'typescript'; | ||
import { compileCode } from './compile'; | ||
@@ -55,2 +55,13 @@ import { findNode, findNodeAfterComment } from './find'; | ||
} | ||
if (a && !a?.parent && !ts.isSourceFile(a)) { | ||
throw new Error( | ||
'AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile' | ||
); | ||
} | ||
if (b && !b?.parent && !ts.isSourceFile(b)) { | ||
throw new Error( | ||
'AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile' | ||
); | ||
} | ||
if (a && b && a.kind === b.kind) { | ||
@@ -57,0 +68,0 @@ const aChildren = a.getChildren(); |
@@ -27,2 +27,8 @@ import { expect } from 'chai'; | ||
}); | ||
it('Throws a helpful error when parents are not set', () => { | ||
const code = ts.createSourceFile('a.tsx', `const c=false`, ts.ScriptTarget.Latest); | ||
expect(() => findNode(code, () => true)).to.throw( | ||
'AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile' | ||
); | ||
}); | ||
}); | ||
@@ -40,2 +46,8 @@ | ||
}); | ||
it('Throws a helpful error when parents are not set', () => { | ||
const code = ts.createSourceFile('a.tsx', `const c=false`, ts.ScriptTarget.Latest); | ||
expect(() => findAllNodes(code, () => true)).to.throw( | ||
'AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile' | ||
); | ||
}); | ||
}); | ||
@@ -111,2 +123,8 @@ | ||
}); | ||
it('Throws a helpful error when parents are not set', () => { | ||
const code = ts.createSourceFile('a.tsx', `const c=false`, ts.ScriptTarget.Latest); | ||
expect(() => findNodeAfterComment(code, 'test')).to.throw( | ||
'AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile' | ||
); | ||
}); | ||
}); |
import { expect } from 'chai'; | ||
import ts from 'typescript'; | ||
import { compileCode } from '../compile'; | ||
@@ -48,2 +49,8 @@ import { isSame, match } from '../match'; | ||
}); | ||
it('Throws a helpful error when parents are not set', () => { | ||
const code = ts.createSourceFile('a.tsx', `const c=false`, ts.ScriptTarget.Latest); | ||
expect(() => match(code, `const a=1;`)).to.throw( | ||
'AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile' | ||
); | ||
}); | ||
}); | ||
@@ -77,2 +84,8 @@ | ||
}); | ||
it('Throws a helpful error when parents are not set', () => { | ||
const code = ts.createSourceFile('a.tsx', `const c=false`, ts.ScriptTarget.Latest); | ||
expect(() => isSame(code, code)).to.throw( | ||
'AST Node has no parent. use compileCode or make sure the "setParentNodes" (3rd argument) is set to true in ts.createSourceFile' | ||
); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60836
997