Skip to content

Commit b2b79bd

Browse files
author
Fatme
authored
Merge pull request #4445 from NativeScript/fatme/merge-release
chore: merge release into master
2 parents dea9938 + 61be7c6 commit b2b79bd

File tree

4 files changed

+94
-38
lines changed

4 files changed

+94
-38
lines changed

lib/common/http-client.ts

+26-25
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,30 @@ export class HttpClient implements Server.IHttpClient {
5656
};
5757
}
5858

59-
const unmodifiedOptions = _.clone(options);
59+
const clonedOptions = _.cloneDeep(options);
6060

61-
if (options.url) {
62-
const urlParts = url.parse(options.url);
61+
if (clonedOptions.url) {
62+
const urlParts = url.parse(clonedOptions.url);
6363
if (urlParts.protocol) {
64-
options.proto = urlParts.protocol.slice(0, -1);
64+
clonedOptions.proto = urlParts.protocol.slice(0, -1);
6565
}
66-
options.host = urlParts.hostname;
67-
options.port = urlParts.port;
68-
options.path = urlParts.path;
66+
clonedOptions.host = urlParts.hostname;
67+
clonedOptions.port = urlParts.port;
68+
clonedOptions.path = urlParts.path;
6969
}
7070

71-
const requestProto = options.proto || "http";
72-
const body = options.body;
73-
delete options.body;
74-
let pipeTo = options.pipeTo;
75-
delete options.pipeTo;
71+
const requestProto = clonedOptions.proto || "http";
72+
const body = clonedOptions.body;
73+
delete clonedOptions.body;
74+
let pipeTo = options.pipeTo; // Use the real stream because the _.cloneDeep can't clone the internal state of a stream.
75+
delete clonedOptions.pipeTo;
7676

7777
const cliProxySettings = await this.$proxyService.getCache();
7878

79-
options.headers = options.headers || {};
80-
const headers = options.headers;
79+
clonedOptions.headers = clonedOptions.headers || {};
80+
const headers = clonedOptions.headers;
8181

82-
await this.useProxySettings(proxySettings, cliProxySettings, options, headers, requestProto);
82+
await this.useProxySettings(proxySettings, cliProxySettings, clonedOptions, headers, requestProto);
8383

8484
if (!headers.Accept || headers.Accept.indexOf("application/json") < 0) {
8585
if (headers.Accept) {
@@ -115,21 +115,21 @@ export class HttpClient implements Server.IHttpClient {
115115
isResolved: () => false
116116
};
117117

118-
if (options.timeout) {
118+
clonedOptions.url = clonedOptions.url || `${clonedOptions.proto}://${clonedOptions.host}${clonedOptions.path}`;
119+
if (clonedOptions.timeout) {
119120
timerId = setTimeout(() => {
120-
this.setResponseResult(promiseActions, cleanupRequestData, { err: new Error(`Request to ${unmodifiedOptions.url} timed out.`) });
121-
}, options.timeout);
121+
this.setResponseResult(promiseActions, cleanupRequestData, { err: new Error(`Request to ${clonedOptions.url} timed out.`) });
122+
}, clonedOptions.timeout);
122123
cleanupRequestData.timers.push(timerId);
123124

124-
delete options.timeout;
125+
delete clonedOptions.timeout;
125126
}
126127

127-
options.url = options.url || `${options.proto}://${options.host}${options.path}`;
128-
options.encoding = null;
129-
options.followAllRedirects = true;
128+
clonedOptions.encoding = null;
129+
clonedOptions.followAllRedirects = true;
130130

131-
this.$logger.trace("httpRequest: %s", util.inspect(options));
132-
const requestObj = request(options);
131+
this.$logger.trace("httpRequest: %s", util.inspect(clonedOptions));
132+
const requestObj = request(clonedOptions);
133133
cleanupRequestData.req = requestObj;
134134

135135
requestObj
@@ -151,7 +151,7 @@ export class HttpClient implements Server.IHttpClient {
151151

152152
stuckRequestTimerId = setTimeout(() => {
153153
this.setResponseResult(promiseActions, cleanupRequestData, { err: new Error(HttpClient.STUCK_REQUEST_ERROR_MESSAGE) });
154-
}, options.timeout || HttpClient.STUCK_REQUEST_TIMEOUT);
154+
}, clonedOptions.timeout || HttpClient.STUCK_REQUEST_TIMEOUT);
155155

156156
cleanupRequestData.timers.push(stuckRequestTimerId);
157157

@@ -230,6 +230,7 @@ export class HttpClient implements Server.IHttpClient {
230230
const response = await result;
231231

232232
if (helpers.isResponseRedirect(response.response)) {
233+
const unmodifiedOptions = _.cloneDeep(options);
233234
if (response.response.statusCode === HttpStatusCodes.SEE_OTHER) {
234235
unmodifiedOptions.method = "GET";
235236
}

lib/services/doctor-service.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,10 @@ export class DoctorService implements IDoctorService {
141141
for (const file of files) {
142142
const fileContent = this.$fs.readText(file);
143143
const strippedComments = helpers.stripComments(fileContent);
144-
const linesWithRequireStatements = strippedComments
144+
const linesToCheck = _.flatten(strippedComments
145145
.split(/\r?\n/)
146+
.map(line => line.split(";")));
147+
const linesWithRequireStatements = linesToCheck
146148
.filter(line => /\btns-core-modules\b/.exec(line) === null && (/\bimport\b/.exec(line) || /\brequire\b/.exec(line)));
147149

148150
for (const line of linesWithRequireStatements) {

lib/services/project-data-service.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import * as path from "path";
22
import { ProjectData } from "../project-data";
33
import { exported } from "../common/decorators";
4-
import { NATIVESCRIPT_PROPS_INTERNAL_DELIMITER, AssetConstants, SRC_DIR, RESOURCES_DIR, MAIN_DIR, CLI_RESOURCES_DIR_NAME, ProjectTypes } from "../constants";
4+
import {
5+
NATIVESCRIPT_PROPS_INTERNAL_DELIMITER,
6+
AssetConstants, SRC_DIR,
7+
RESOURCES_DIR,
8+
MAIN_DIR,
9+
CLI_RESOURCES_DIR_NAME,
10+
ProjectTypes,
11+
NODE_MODULES_FOLDER_NAME
12+
} from "../constants";
513

614
interface IProjectFileData {
715
projectData: any;
@@ -124,6 +132,7 @@ export class ProjectDataService implements IProjectDataService {
124132
supportedFileExtension = ".ts";
125133
}
126134

135+
const pathToProjectNodeModules = path.join(projectDir, NODE_MODULES_FOLDER_NAME);
127136
const files = this.$fs.enumerateFilesInDirectorySync(
128137
projectData.appDirectoryPath,
129138
(filePath, fstat) => {
@@ -132,6 +141,12 @@ export class ProjectDataService implements IProjectDataService {
132141
}
133142

134143
if (fstat.isDirectory()) {
144+
if (filePath === pathToProjectNodeModules) {
145+
// we do not want to get the files from node_modules directory of the project.
146+
// We'll get here only when you have nsconfig.json with appDirectoryPath set to "."
147+
return false;
148+
}
149+
135150
return true;
136151
}
137152

test/services/doctor-service.ts

+49-11
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe("doctorService", () => {
4949
filesContents: {
5050
file1: 'const application = require("application");'
5151
},
52-
expectedShortImports: [{ file: "file1", line: 'const application = require("application");' }]
52+
expectedShortImports: [{ file: "file1", line: 'const application = require("application")' }]
5353
},
5454
{
5555
filesContents: {
@@ -61,7 +61,7 @@ describe("doctorService", () => {
6161
filesContents: {
6262
file1: 'const Observable = require("data/observable").Observable;'
6363
},
64-
expectedShortImports: [{ file: "file1", line: 'const Observable = require("data/observable").Observable;' }]
64+
expectedShortImports: [{ file: "file1", line: 'const Observable = require("data/observable").Observable' }]
6565
},
6666
{
6767
filesContents: {
@@ -73,7 +73,7 @@ describe("doctorService", () => {
7373
filesContents: {
7474
file1: 'import * as application from "application";'
7575
},
76-
expectedShortImports: [{ file: "file1", line: 'import * as application from "application";' }]
76+
expectedShortImports: [{ file: "file1", line: 'import * as application from "application"' }]
7777
},
7878
{
7979
filesContents: {
@@ -85,7 +85,7 @@ describe("doctorService", () => {
8585
filesContents: {
8686
file1: 'import { run } from "application";'
8787
},
88-
expectedShortImports: [{ file: "file1", line: 'import { run } from "application";' }]
88+
expectedShortImports: [{ file: "file1", line: 'import { run } from "application"' }]
8989
},
9090
{
9191
filesContents: {
@@ -98,7 +98,7 @@ describe("doctorService", () => {
9898
filesContents: {
9999
file1: "import { run } from 'application';"
100100
},
101-
expectedShortImports: [{ file: "file1", line: "import { run } from 'application';" }]
101+
expectedShortImports: [{ file: "file1", line: "import { run } from 'application'" }]
102102
},
103103
{
104104
// Using single quotes
@@ -114,8 +114,8 @@ const Observable = require("data/observable").Observable;
114114
`
115115
},
116116
expectedShortImports: [
117-
{ file: "file1", line: 'const application = require("application");' },
118-
{ file: "file1", line: 'const Observable = require("data/observable").Observable;' },
117+
{ file: "file1", line: 'const application = require("application")' },
118+
{ file: "file1", line: 'const Observable = require("data/observable").Observable' },
119119
]
120120
},
121121
{
@@ -125,7 +125,7 @@ const Observable = require("tns-core-modules/data/observable").Observable;
125125
`
126126
},
127127
expectedShortImports: [
128-
{ file: "file1", line: 'const application = require("application");' },
128+
{ file: "file1", line: 'const application = require("application")' },
129129
]
130130
},
131131
{
@@ -137,8 +137,8 @@ const Observable = require("tns-core-modules/data/observable").Observable;
137137
const Observable = require("data/observable").Observable;`
138138
},
139139
expectedShortImports: [
140-
{ file: "file1", line: 'const application = require("application");' },
141-
{ file: "file2", line: 'const Observable = require("data/observable").Observable;' },
140+
{ file: "file1", line: 'const application = require("application")' },
141+
{ file: "file2", line: 'const Observable = require("data/observable").Observable' },
142142
]
143143
},
144144
{
@@ -150,7 +150,45 @@ const Observable = require("tns-core-modules/data/observable").Observable;
150150
file2: `const application = require("some-name-tns-core-modules-widgets/application");
151151
const Observable = require("tns-core-modules-widgets/data/observable").Observable;`
152152
},
153-
expectedShortImports: [ ]
153+
expectedShortImports: []
154+
},
155+
{
156+
filesContents: {
157+
// several statements on one line
158+
file1: 'const _ = require("lodash");console.log("application");'
159+
},
160+
expectedShortImports: []
161+
},
162+
{
163+
filesContents: {
164+
// several statements on one line with actual short imports
165+
file1: 'const _ = require("lodash");const application = require("application");console.log("application");',
166+
file2: 'const _ = require("lodash");const application = require("application");const Observable = require("data/observable").Observable;'
167+
},
168+
expectedShortImports: [
169+
{ file: "file1", line: 'const application = require("application")' },
170+
{ file: "file2", line: 'const application = require("application")' },
171+
{ file: "file2", line: 'const Observable = require("data/observable").Observable' },
172+
]
173+
},
174+
{
175+
filesContents: {
176+
// several statements on one line withoutshort imports
177+
file1: 'const _ = require("lodash");const application = require("tns-core-modules/application");console.log("application");',
178+
file2: 'const _ = require("lodash");const application = require("tns-core-modules/application");const Observable = require("tns-core-modules/data/observable").Observable;'
179+
},
180+
expectedShortImports: []
181+
},
182+
{
183+
// Incorrect behavior, currently by design
184+
// In case you have a multiline string and one of the lines matches our RegExp we'll detect it as short import
185+
filesContents: {
186+
file1: 'const _ = require("lodash");const application = require("application");console.log("application");console.log(`this is line\nyou should import some long words here "application" module and other words here`)',
187+
},
188+
expectedShortImports: [
189+
{ file: "file1", line: 'const application = require("application")' },
190+
{ file: "file1", line: 'you should import some long words here "application" module and other words here`)' },
191+
]
154192
},
155193
];
156194

0 commit comments

Comments
 (0)