Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gts

Package Overview
Dependencies
Maintainers
6
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gts - npm Package Compare versions

Comparing version 0.8.0 to 0.9.0

build/src/watch.d.ts

1

build/src/cli.d.ts

@@ -0,1 +1,2 @@

#!/usr/bin/env node
export interface Logger {

@@ -2,0 +3,0 @@ log: (...args: Array<{}>) => void;

import { Options } from './cli';
/**
* Object that contains the position of formatting issue within the file, the
* length of it, and the string to replace it with.
*/
export interface Replacement {
offset: number;
length: number;
fix: string;
}
export declare const clangFormat: any;

@@ -10,1 +19,8 @@ /**

export declare function format(options: Options, files?: string[], fix?: boolean): Promise<boolean>;
/**
* Parses through xml string for the replacement string, offset, length and
* returns all of the necessary replacements for the file
*
* @param output xml string from clangFormat
*/
export declare function getReplacements(fileXML: string): Replacement[];

@@ -26,5 +26,9 @@ "use strict";

*/
const chalk_1 = require("chalk");
const jsdiff = require("diff");
const entities = require("entities");
const fs = require("fs");
const path = require("path");
const lint_1 = require("./lint");
const util_1 = require("./util");
// Exported for testing purposes.

@@ -65,3 +69,3 @@ exports.clangFormat = require('clang-format');

else {
const result = yield checkFormat(srcFiles, baseClangFormatArgs);
const result = yield checkFormat(options, srcFiles, baseClangFormatArgs);
if (!result) {

@@ -99,3 +103,3 @@ options.logger.log('clang-format reported errors... run `gts fix` to address.');

*/
function checkFormat(srcFiles, baseArgs) {
function checkFormat(options, srcFiles, baseArgs) {
return new Promise((resolve, reject) => {

@@ -115,7 +119,122 @@ let output = '';

});
out.on('end', () => {
out.on('end', () => __awaiter(this, void 0, void 0, function* () {
const files = output.split('<?xml version=\'1.0\'?>\n').slice(1);
for (let i = 0; i < files.length; i++) {
if (files[i].indexOf('<replacement ') === -1) {
continue;
}
const replacements = getReplacements(files[i]);
if (replacements.length > 0) {
const diff = yield getDiffObj(srcFiles[i], replacements);
printDiffs(diff, options);
}
}
resolve(output.indexOf('<replacement ') === -1 ? true : false);
}));
});
}
/**
* Parses through xml string for the replacement string, offset, length and
* returns all of the necessary replacements for the file
*
* @param output xml string from clangFormat
*/
function getReplacements(fileXML) {
const replacements = [];
let xmlLines = fileXML.trim().split('\n');
// first and last elements are outer 'replacements' tags
xmlLines = xmlLines.slice(1, xmlLines.length - 1);
for (let i = 0; i < xmlLines.length; i++) {
// Uses regex to capture the xml attributes and element
// XML format:
// <replacement offset='OFFSET' length='LENGTH'>FIX</replacement>
const offset = (/offset=\'(\d+)\'/g).exec(xmlLines[i]);
const length = (/length=\'(\d+)\'/g).exec(xmlLines[i]);
const fix = (/length=\'\d+\'>(.*)<\/replacement>/g).exec(xmlLines[i]);
if (length === null || offset === null || fix === null) {
throw new Error('Unable to get replacement');
}
replacements[i] = {
offset: Number(offset[1]),
length: Number(length[1]),
fix: entities.decodeXML(fix[1])
};
}
return replacements;
}
exports.getReplacements = getReplacements;
/**
* Gets an object containing the differences between the original file and after
* changes have been made
*
* @param file
* @param replacements array of all the formatting issues within in the file
*/
function getDiffObj(file, replacements) {
return __awaiter(this, void 0, void 0, function* () {
const text = yield util_1.readFilep(file, 'utf8');
const fixed = performFixes(text, replacements);
const diff = jsdiff.structuredPatch(file, '', text, fixed, '', '', { context: 3 });
jsdiff.applyPatch('diff', diff);
return diff;
});
}
/**
* Performs formatting fixes to the original string
*
* @param data original string
* @param errOffset start index of the formatting issue
* @param errLength length of formatting issue
* @param replacements string that resolves formatting issue
*/
function performFixes(data, replacements) {
const replaced = [];
replaced.push(substring(data, 0, replacements[0].offset));
for (let i = 0; i < replacements.length - 1; i++) {
replaced.push(replacements[i].fix);
replaced.push(substring(data, replacements[i].offset + replacements[i].length, replacements[i + 1].offset));
}
replaced.push(replacements[replacements.length - 1].fix);
replaced.push(substring(data, replacements[replacements.length - 1].offset +
replacements[replacements.length - 1].length, Buffer.byteLength(data, 'utf8')));
return replaced.join('');
}
/**
* Prints the lines with formatting issues
*
* @param diffs contains all information about the formatting changes
* @param options
*/
function printDiffs(diffs, options) {
options.logger.log(chalk_1.default.inverse.bold(diffs.oldFileName));
diffs.hunks.forEach((diff) => {
const log = ` Lines: ${diff.oldStart}-${diff.oldStart + diff.oldLines}`;
options.logger.log(chalk_1.default.bold(log));
diff.lines.forEach((line) => {
if (line[0] === '-') {
options.logger.log(` ${chalk_1.default.red(line)}`);
}
else if (line[0] === '+') {
options.logger.log(` ${chalk_1.default.green(line)}`);
}
else {
options.logger.log(` ${chalk_1.default.gray(line)}`);
}
});
options.logger.log('\n');
});
}
/**
* Substring using bytes
*
* @param str original string
* @param indexStart position where to start extraction
* @param indexEnd position (up to, but not including) where to end extraction
* @param encoding
*/
function substring(str, indexStart, indexEnd, encoding = 'utf8') {
return Buffer.from(str, encoding)
.slice(indexStart, indexEnd)
.toString(encoding);
}
//# sourceMappingURL=format.js.map

5

build/src/init.js

@@ -96,3 +96,6 @@ "use strict";

let edits = false;
const deps = { 'gts': `^${pkg.version}`, 'typescript': '~2.8.0' };
const deps = {
gts: `^${pkg.version}`,
typescript: pkg.devDependencies.typescript
};
if (!packageJson.devDependencies) {

@@ -99,0 +102,0 @@ packageJson.devDependencies = {};

@@ -0,1 +1,16 @@

/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export declare const readFilep: (...args: any[]) => Promise<any>;

@@ -12,3 +27,16 @@ export declare const rimrafp: (...args: any[]) => Promise<any>;

* @param rootDir Directory where the tsconfig.json should be found.
* If the tsconfig.json file has an "extends" field hop down the dependency tree
* until it ends or a circular reference is found in which case an error will be
* thrown
*/
export declare function getTSConfig(rootDir: string, customReadFilep?: ReadFileP): Promise<{}>;
export declare function getTSConfig(rootDir: string, customReadFilep?: ReadFileP): Promise<ConfigFile>;
/**
* An interface containing the top level data fields present in Config Files
*/
export interface ConfigFile {
files?: string[];
compilerOptions?: {};
include?: string[];
exclude?: string[];
extends?: string[];
}

@@ -46,13 +46,59 @@ "use strict";

* @param rootDir Directory where the tsconfig.json should be found.
* If the tsconfig.json file has an "extends" field hop down the dependency tree
* until it ends or a circular reference is found in which case an error will be
* thrown
*/
function getTSConfig(rootDir, customReadFilep) {
return __awaiter(this, void 0, void 0, function* () {
const tsconfigPath = path.join(rootDir, 'tsconfig.json');
customReadFilep = customReadFilep || exports.readFilep;
const json = yield customReadFilep(tsconfigPath, 'utf8');
const contents = JSON.parse(json);
return contents;
const readArr = new Set();
return yield getBase('tsconfig.json', customReadFilep, readArr, rootDir);
});
}
exports.getTSConfig = getTSConfig;
/**
* Recursively iterate through the dependency chain until we reach the end of
* the dependency chain or encounter a circular reference
* @param filePath Filepath of file currently being read
* @param customReadFilep The file reading function being used
* @param readFiles an array of the previously read files so we can check for
* circular references
* returns a ConfigFile object containing the data from all the dependencies
*/
function getBase(filePath, customReadFilep, readFiles, currentDir) {
return __awaiter(this, void 0, void 0, function* () {
customReadFilep = customReadFilep || exports.readFilep;
filePath = path.resolve(currentDir, filePath);
// An error is thrown if there is a circular reference as specified by the
// TypeScript doc
if (readFiles.has(filePath)) {
throw new Error(`Circular reference in ${filePath}`);
}
readFiles.add(filePath);
try {
const json = yield customReadFilep(filePath, 'utf8');
let contents = JSON.parse(json);
if (contents.extends) {
const nextFile = yield getBase(contents.extends, customReadFilep, readFiles, path.dirname(filePath));
contents = combineTSConfig(nextFile, contents);
}
return contents;
}
catch (err) {
throw new Error(`${filePath} Not Found`);
}
});
}
/**
* Takes in 2 config files
* @param base is loaded first
* @param inherited is then loaded and overwrites base
*/
function combineTSConfig(base, inherited) {
const result = { compilerOptions: {} };
Object.assign(result, base, inherited);
Object.assign(result.compilerOptions, base.compilerOptions, inherited.compilerOptions);
delete result.extends;
return result;
}
//# sourceMappingURL=util.js.map
{
"name": "gts",
"version": "0.8.0",
"version": "0.9.0",
"description": "Google TypeScript Style",

@@ -45,5 +45,7 @@ "repository": "google/ts-style",

"clang-format": "1.2.3",
"diff": "^3.5.0",
"entities": "^1.1.1",
"inquirer": "^6.0.0",
"meow": "^5.0.0",
"pify": "^3.0.0",
"pify": "^4.0.0",
"rimraf": "^2.6.2",

@@ -55,6 +57,8 @@ "tslint": "^5.9.1",

"devDependencies": {
"@types/glob": "^5.0.35",
"@types/inquirer": "^0.0.42",
"@types/diff": "^3.5.1",
"@types/entities": "^1.1.0",
"@types/glob": "^7.0.0",
"@types/inquirer": "^0.0.43",
"@types/make-dir": "^1.0.3",
"@types/meow": "^4.0.1",
"@types/meow": "^5.0.0",
"@types/ncp": "^2.0.1",

@@ -71,9 +75,9 @@ "@types/node": "^10.0.3",

"ncp": "^2.0.0",
"nyc": "^12.0.2",
"nyc": "^13.0.0",
"source-map-support": "^0.5.5",
"tmp": "0.0.33",
"typescript": "~2.8.3"
"typescript": "~3.1.0"
},
"peerDependencies": {
"typescript": "^2.7.1"
"typescript": "^2.7.1 || ^3.0.0"
},

@@ -80,0 +84,0 @@ "ava": {

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc